一个存储过程

2009年6月15日

用的是SQL SERVER2K
现在有一张表,
大概字段
id(自增长), SNumber(站号, 001, 002, etc.), SName(站名),GTime(datetime,获取测站数据时间),Level(float, 测站数据)

现在大概有7,80个站, 每隔6分钟就会同时向数据库里添加一条记录.

现在想实现把同一天内相同时间过来的数据先按照SNumber排列, 再按gtime倒序排列,然后对数据重新编号(1,2,3,etc) 请问如何实现?

例如原始记录
id snumber sname gtime level
66 023 测站1 2009-12-17 10:00:00 50.12
65 058 测站2 2009-12-17 10:00:00 30.24
64 018 测站3 2009-12-17 10:00:00 83.01
63 018 测站3 2009-12-17 09:54:00 50.12
62 058 测站2 2009-12-17 09:54:00 30.24
61 023 测站1 2009-12-17 09:54:00 83.01

按照要求排序后结果为
newid snumber sname gtime level
1 018 测站3 2009-12-17 10:00:00 83.01
2 023 测站1 2009-12-17 10:00:00 50.12
3 058 测站2 2009-12-17 10:00:00 30.24
1 018 测站3 2009-12-17 09:54:00 83.01
2 023 测站1 2009-12-17 09:54:00 50.12
3 058 测站2 2009-12-17 09:54:00 30.24

正解:

CREATE TABLE taoistong 
(id int identity(1,1)
,snumber char(3)
,sname nvarchar(4)
,gtime smalldatetime
,level float )
INSERT INTO taoistong SELECT 
'023',  N'测站1', '2009-12-17 10:00:00', 50.12 
INSERT INTO taoistong SELECT 
 '058', N'测站2', '2009-12-17 10:00:00', 30.24 
INSERT INTO taoistong SELECT 
 '018', N'测站3', '2009-12-17 10:00:00', 83.01 
INSERT INTO taoistong SELECT 
 '018', N'测站3', '2009-12-17 09:54:00', 50.12 
INSERT INTO taoistong SELECT 
 '058', N'测站2', '2009-12-17 09:54:00', 30.24 
INSERT INTO taoistong SELECT 
 '023', N'测站1', '2009-12-17 09:54:00', 83.01 
 
SELECT (SELECT count(1) FROM taoistong b WHERE a.gtime=b.gtime AND a.snumber>=b.snumber)
,snumber,sname,gtime,level  
FROM taoistong a ORDER BY gtime DESC,snumber

admin CSharp

PHP拾遗捡漏(一)

2009年5月6日

PHP拾遗捡漏(一)

最近在看<PHP和MySQL WEB开发>第四版, 把一些基础知识又复习了一遍, 还是很有收获的.
边看边记录一些文字吧.

1. echo 输出字符串
双引号和单引号的区别

$foo = 'hello';
echo $foo.' world!<br />';
echo "$foo world!<br />";
echo '$foo world!<br />';

运行后, 可以看出第一第二两个语句是等价的, 而第三个语句则原样输出.

结论: 对于任何简单类型的变量, 都可以将变量写入到一个由双引号引起来的字符串中, 在双引号中, 变量名称将被变量值所代替.而在单引号中, 变量名称,或者任何其他文本都会不经修改而直接发送到浏览器端.

Heredoc字符串也是插补的, 就像双引号字符串.

2.正确理解标识符
PHP中, 标识符是区分大小写的, $foo和$Foo是不同的, 而函数名称则不区分大小写.

$foo = 'foo';
$Foo = 'Foo';
echo "$foo<br />";
echo "$Foo<br />";
echo bar()."<br />";
echo Bar()."<br />";
 
function bar() {
	echo 'bar';
}

3.引用
PHP中引用操作符&可以在关联赋值时使用. 通常, 在将一个变量的值赋给另外一个变量的时候, 将先产生原变量的一个副本, 然后再将它保存在内存的其它的一个地方.
例如

$a = 5;
$b = $a;
$a = 7; // 此时$a=7, 而$b仍然等于5

这两行代码首先将产生一个$a的副本, 然后再将它保存到$b中, 如果随后改变$a的值, $b的值不会有任何变化.

可以通过引用操作符&来避免产生$a的副本, 此时$a和$b都指向了内存中的相同地址.

$a = 5;
$b = &$a;
$a = 7; // 此时$a和$b都等于7

可以通过unset($a)来重置它们来改变所指向的地址, 重置并不会改变$b(7)的值.

4. “执行操作符, 它是一对反向单引号, PHP将试着将其间的命令当做服务器端的命令行来执行. 表达式的值就是命令的执行结果.

$out = `dir c:` // windows, Linux为 `ls -la`
echo $out;

5. isset()和empty()
bool isset(mixed var)
该函数需要一个变量名称作为参数, 如果这个变量存在返回true,否则返回false. 也可以传递一个由逗号间隔的变量列表, 如果所有变量都被设置了. 返回true.
可以使用unset()来销毁一个变量.

函数empty()可以用来检测一个变量是否存在,以及它的值是否为空和非0, 相应的返回true和false.

简单的来说empty()比isset()多检测值的非空和非0状态.

6. range()可以使用range()函数自动创建一个按升序排列的数字数组.

// 例如以下的代码将创建一个1~10的数字数组
$numbers = range(1, 10);
//range()函数具有一个可选的第三个参数, 这个参数允许设定值之间的步幅. 例如创建一个1~10之间的奇数数组
$odds = range(1, 10, 2);
//range()函数也可以对字符进行操作, 如下
$letters = range('a', 'z');

7. 数组排序
使用sort()函数, sort()函数可以将数组按字母或数字升序进行排序

 $products = array('Tires', 'Oil', 'Spark Plugs');
 sort($products);
 $prices = array(100, 10, 4);
 sort($prices);

sort()函数的第二个参数是可选的, 这个可选参数可以传递SORT_REGULAR(默认值), SORT_NUMERIC或SORT_STRING, sort()函数是区分大小写的.

使用asort()和ksort()函数对关联数组排序
函数asort()根据数组的每个元素值进行排序.
函数ksort()根据数组的每个元素进行排序.

以上三个函数都使数组按升序排序, 它们每个都对应有一个反向排序的函数, 分别是rsort(), arsort(), krsort().

用户自定义排序usort()
usort()函数的参数分别是希望排序的数组和用户比较的函数的名称

 // 这是一个普通的二维数组, 存储了3种产品的代码,说明和价格.
 $products = array(array('TLR', 'Tires', 100),
		array('OIL', 'Oil', 10),
		array('SPK', 'Spark Plugs', 4));
 
 // 以下代码对订单数组中的第二列(说明)按字母进行排序
 function compare($x, $y) {
	if ($x[1] == $y[1])
		return 0;
	else if ($x[1] < $y[1])
		return -1;
	else
		return 1;
 }
 
 usort($products, 'compare')
 
 // 同时实现反向排序
 function reverse_compare($x, $y) {
	if ($x[1] == $y[1])
		returnn 0;
	else if ($x[1] < $y[1])
		return 1;
	else
		return -1;
 }
 
 usort($products, 'reverse_compare');

转载请注明出处: http://www.suiyuan.org

admin PHP

使用C#调用外部命令重启IIS应用程序池

2009年5月4日

在这里我们通过.net的Process类调用外部命令, 通过重定向输入, 输出获取执行结果.
添加应用using System.Diagnostics;
核心代码如下

                                p.StartInfo.FileName = "cmd.exe";
                                p.StartInfo.UseShellExecute = false;
                                p.StartInfo.RedirectStandardInput = true;
                                p.StartInfo.RedirectStandardOutput = true;
                                p.StartInfo.RedirectStandardError = true;
                                p.StartInfo.CreateNoWindow = true;
                                p.Start();
                                p.StandardInput.WriteLine(_command);
                                p.StandardInput.WriteLine("exit");
                                //string command = "cscript.exe";
                                //p.StartInfo.Arguments = command + " c:\\windows\\system32\\iisapp.vbs /a \"www.xxx.net\" ";
 
                                //p.WaitForExit();
                                string s = p.StandardOutput.ReadToEnd();
                                p.Close();
                                LogEvent(s, LogType.normal);

_command写到配置文件中, 内容是

<add key="Command" value="cscript.exe c:\\windows\\system32\\iisapp.vbs /a www.xxx.net" />

这样就可以简单的实现重新启动指定的应用程序池了.
您可以通过命令iisapp -a来查看当前服务器上的所有应用程序.

最后可以根据自己的需要把这个代码写成windows服务, 安装到服务器上. 既可实现定时重新启动应用程序池功能.

admin CSharp

PHP中单件模式(SingletonPattern)的实现

2009年4月29日

单件(singleton)模式作为设计模式的一个典型而且简单的构造型模式, 已经在很多项目的实际开发中被广泛使用, 单件模式要求一个类有且仅有一个实例,然后提供一个全局的访问点.也就是其所有的相关操作都是基于同一个实例的引用.
单件模式的特点:

  • 单件类只能有一个实例
  • 单件类必须自己创建自己的唯一实例
  • 单件类必须给所有其它对象提供这一实例
  • 单件模式的优点:

  • 从逻辑上来讲, 仅有一个实例保证了逻辑的正确性
  • 通过共享减少的使用
  • 减少应为频繁的构造而带来的应用上的损失
  • 下面通过一个简单的例子来说一些单件模式
    在我们实际的应用中, 大家都知道数据库的连接资源是非常宝贵的, 通过数据库句柄到数据库的连接是独占的,每次的连接和关闭数据库都是一笔非常大的开销, 那么我们如何在应用程序中来共享数据库句柄呢? 我们可以通过单件模式实现.
    以下程序用到三个PHP文件, 程序只是为了展示单件模式,本身不具备任何价值.
    MySQL.php

    class MySQL {
    	private $dbhost = 'localhost';
    	private $dbuser = 'root';
    	private $dbpass = '123';
    	private $db	    = 'test';
     
    	public $guid;
     
    	//======================================
    	// 函数: connect()
    	// 功能: 连接数据库
    	// 返回:
    	//======================================
    	public function __construct()
    	{
    		$this->link_id = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass);
    		$this->guid = md5(uniqid());
    		if (!$this->link_id)
    		{
    			$this->halt("数据库连接失败.请检查连接参数.");
    			return;
    		}
     
    		if (!mysql_select_db($this->db, $this->link_id))
    		{
    			$this->halt("选择数据库".$this->db."失败.");
    			return;
    		}
    		mysql_query("SET NAMES 'utf8'");
    	}
    }

    Singleton.php

    <?php
    class Singleton {
    	private $_db;
    	private static $_single = null;
     
    	public static function getInstance() {
    		if (self::$_single === null)
    			self::$_single = new Singleton();
    		return self::$_single;
    	}
     
    	function __construct() {
    		$this->_db = new MySQL();
    	}
     
    	public function db() {
    		return $this->_db;
    	}
    }
     
    /* End of file */
    /* Location: */

    Test.php

    <?PHP
    require_once ('MySQL.php');
    require_once ('Singleton.php');
    header('content-type:text/html; charset=utf-8');
     
    echo '单件模式____________________________<br />';
    $s1 = Singleton::getInstance()->db();
    $s2 = Singleton::getInstance()->db();
    echo '$s1->guid:'.$s1->guid.'<br />';
    echo '$s1->guid:'.$s2->guid.'<br />';
    echo ($s1 === $s2) ? 'true' : 'false';
    echo '<br />';
     
    echo '直接实例化对象________________________________<br />';
    $d1 = new MySQL();
    echo '$d->guid:'.$d1->guid.'<br />';
    $d2 = new MySQL();
    echo '$d1->guid:'.$d2->guid.'<br />';
    echo ($d1 === $d2) ? 'true' : 'false';
    ?>

    将这三个文件放到一个目录中, 最后浏览test.php,可以看到
    通过单件模式获取的2个对象guid参数任何时候都是相同的,且这两个对象是恒等的.
    而直接实例出来的2个DB对象则刚好相反.
    通过比较, 相信你已经很轻松的掌握了单件模式的特点.

    转载请注明出处:http://www.suiyuan.org

    admin 生活

    The Rose

    2009年4月29日

    The Rose
    by 阿桑
    asang
    Some say love it is a river
    that drowns the tender reed
    some say love it is a razor
    that leaves your soul to bleed
    some say love it is a hunger
    and endless aching need
    i say love it is a flower
    and you its only seed
    it’s the heart afraid of breaking
    that never learns to dance
    it’s the dream afraid of waking
    that never takes the chance
    it’s the one who won’t be taken
    who can not seem to give
    and the soul afraid of dying
    that never learns to live
    when the night has been too lonely
    and the road has been too long
    and you think that love is only
    for the lucky and the strongjust remember in the winter
    far beneath the bitter snow
    lies the seed that with the sun’s love
    in the spring becomes the rose

    阅读全文…

    admin 音乐

    通过C#实现报警续

    2009年4月17日

    前一篇c#蜂鸣报警系列声音函数
    今天要说的是如何实现连续播放报警声音和停止报警.

            #region 报警
            [DllImport("winmm.dll", EntryPoint = "PlaySound")]
            private static extern bool Win32_PlaySound(string pszSound, IntPtr hmod, uint fdwSound);   
     
            /// <summary>
            /// 播放一个wav音频文件
            /// </summary>
            /// <param name="path"></param>
            /// <param name="asynchronous"></param>
            /// <param name="loop"></param>
            /// <param name="doNotStopPlay"></param>
            public static void PlaySound(string path, bool asynchronous, bool loop, bool doNotStopPlay)
            {
                Win32_PlaySound(path, IntPtr.Zero, (uint)((asynchronous ?
              PlaySoundMessage.SND_ASYNC : PlaySoundMessage.SND_SYNC) | (loop ?
              PlaySoundMessage.SND_LOOP : 0) | (doNotStopPlay ?
              PlaySoundMessage.SND_NOSTOP : 0) | PlaySoundMessage.SND_FILENAME));
            }
     
            /// <summary>
            /// 停止播放
            /// </summary> 
            public static void StopSound()
            {
                Win32_PlaySound(null, IntPtr.Zero, 0);
            }
     
            [Flags()]
            internal enum PlaySoundMessage
            {
                SND_SYNC = 0x0000,
                SND_ASYNC = 0x0001,
                SND_LOOP = 0x0008,
                SND_NOSTOP = 0x0010,
                SND_FILENAME = 0x00020000
            }
            #endregion

    当例如遥测水位中测量的水位大于预警水位, 通过PlaySound(”alarm8.wav”, true, true, false)方法实现连续报警, 你还可以同时弹出窗口提醒之类的信息.
    关闭报警通过方法StopSound()实现就可以了.

    admin CSharp