DataTable的序列化与反序列化.

September 22nd, 2008 Arale No comments
private static string SerializeDataTableXml(DataTable pDt)
{
   // 序列化DataTable
   StringBuilder sb = new StringBuilder();
   XmlWriter writer = XmlWriter.Create(sb);
   XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
   serializer.Serialize(writer, pDt);
   writer.Close();
   return sb.ToString();
}
/// <summary>
/// Serialized DataTable
/// </summary>
/// <param name="pXml">Descrialized DataTable</param>
/// <returns>DataTable</returns>
public static DataTable DeserializeDataTable(string pXml)
{
    StringReader strReader = new StringReader(pXml);
    XmlReader xmlReader = XmlReader.Create(strReader);
    XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
    DataTable dt = serializer.Deserialize(xmlReader) as DataTable;
    return dt;
}
Read more...

Ultra Chart 的用法.

July 1st, 2008 Arale No comments
private void chart()
{
	object[] Data1 = new object[]
	{
		new object[] {10,15,30,10},
		new object[] {20,50,20,5},
		new object[] {45,25,05,20},
		new object[] {35,10,40,30},
	};
 
	DataTable table = new DataTable();
	table.Columns.Add("a", typeof(double));
	table.Columns.Add("b", typeof(double));
	table.Columns.Add("c", typeof(double));
	table.Columns.Add("d", typeof(double));
 
	for (int i = 0; i < Data1.Length; i++)
	{
		DataRow row = table.NewRow();
		row.ItemArray = (object[])Data1[i];
		table.Rows.Add(row);
	}
	this.ultraChart1.Data.DataSource = table;
	this.ultraChart1.Data.DataBind();
	this.ultraChart1.ChartType = Infragistics.UltraChart.Shared.Styles.ChartType.ColumnChart;
	this.ultraChart1.TitleTop.Text = "TEXT";
}
Read more...

MSSQL中的数组类型-Table

June 12th, 2008 Arale No comments

table
一种特殊的数据类型,用于存储结果集以供后续处理。该数据类型主要用于临时存储一组行,这些行将作为表值函数的结果集返回。
语法
说明 使用 DECLARE @local_variable 声明 table 类型的变量。
table_type_definition ::=
TABLE ( { column_definition | table_constraint } [ ,...n ] )
column_definition ::=
column_name scalar_data_type
[ COLLATE collation_definition ]
[ [ DEFAULT constant_expression ] | IDENTITY [ ( seed , increment ) ] ]
[ ROWGUIDCOL ]
[ column_constraint ] [ ...n ]
column_constraint ::=
{ [ NULL | NOT NULL ]
| [ PRIMARY KEY | UNIQUE ]
| CHECK ( logical_expression )
}
table_constraint ::=
{ { PRIMARY KEY | UNIQUE } ( column_name [ ,...n ] )
| CHECK ( search_condition )
}

参数
table_type_definition
与 CREATE TABLE 中定义表所用的信息子集相同的信息子集。表声明包括列定义、名称、数据类型和约束。允许的约束类型仅为 PRIMARY KEY、UNIQUE KEY 和 NULL。

有关语法的更多信息,请参见 CREATE TABLE、CREATE FUNCTION 和 DECLARE @local_variable。
collation_definition
是由 Microsoft® Windows™ 区域设置和比较风格、Windows 区域设置和二进制表示法或 Microsoft SQL Server™ 排序规则组成的列的排序规则。

注释
可将函数和变量声明为 table 类型。table 变量可用于函数、存储过程和批处理中。
尽可能使用表变量而不使用临时表。table 变量有以下优点:
table 变量的行为类似于局部变量,有明确定义的作用域。该作用域为声明该变量的函数、存储过程或批处理。
在其作用域内,table 变量可像常规表那样使用。该变量可应用于 SELECT、INSERT、UPDATE 和 DELETE 语句中用到表或表的表达式的地方。但是,table 不能用在下列语句中:

INSERT INTO table_variable EXEC 存储过程。
SELECT select_list INTO table_variable 语句。

在定义 table 变量的函数、存储过程或批处理结束时,自动清除 table 变量。
在存储过程中使用表变量与使用临时表相比,减少了存储过程的重新编译量。

涉及表变量的事务只在表变量更新期间存在。这样就减少了表变量对锁定和记录资源的需求。
不支持在表变量之间进行赋值操作。另外,由于表变量作用域有限,并且不是持久数据库的一部分,因而不受事务回滚的影响。

Read more...

DateTime转换为整型时的格式参考

March 1st, 2008 Arale No comments
DateTime dt = DateTime.Now;
Label1.Text = dt.ToString();//2005-11-5 13:21:25
Label2.Text = dt.ToFileTime().ToString();//127756416859912816
Label3.Text = dt.ToFileTimeUtc().ToString();//127756704859912816
Label4.Text = dt.ToLocalTime().ToString();//2005-11-5 21:21:25
Label5.Text = dt.ToLongDateString().ToString();//2005年11月5日
Label6.Text = dt.ToLongTimeString().ToString();//13:21:25
Label7.Text = dt.ToOADate().ToString();//38661.5565508218
Label8.Text = dt.ToShortDateString().ToString();//2005-11-5
Label9.Text = dt.ToShortTimeString().ToString();//13:21
Label10.Text = dt.ToUniversalTime().ToString();//2005-11-5 5:21:25
?2005-11-5 13:30:28.4412864
Label1.Text = dt.Year.ToString();//2005
Label2.Text = dt.Date.ToString();//2005-11-5 0:00:00
Label3.Text = dt.DayOfWeek.ToString();//Saturday
Label4.Text = dt.DayOfYear.ToString();//309
Label5.Text = dt.Hour.ToString();//13
Label6.Text = dt.Millisecond.ToString();//441
Label7.Text = dt.Minute.ToString();//30
Label8.Text = dt.Month.ToString();//11
Label9.Text = dt.Second.ToString();//28
Label10.Text = dt.Ticks.ToString();//632667942284412864
Label11.Text = dt.TimeOfDay.ToString();//13:30:28.4412864
Label1.Text = dt.ToString();//2005-11-5 13:47:04
Label2.Text = dt.AddYears(1).ToString();//2006-11-5 13:47:04
Label3.Text = dt.AddDays(1.1).ToString();//2005-11-6 16:11:04
Label4.Text = dt.AddHours(1.1).ToString();//2005-11-5 14:53:04
Label5.Text = dt.AddMilliseconds(1.1).ToString();//2005-11-5 13:47:04
Label6.Text = dt.AddMonths(1).ToString();//2005-12-5 13:47:04
Label7.Text = dt.AddSeconds(1.1).ToString();//2005-11-5 13:47:05
Label8.Text = dt.AddMinutes(1.1).ToString();//2005-11-5 13:48:10
Label9.Text = dt.AddTicks(1000).ToString();//2005-11-5 13:47:04
Label10.Text = dt.CompareTo(dt).ToString();//0
//Label11.Text = dt.Add(?).ToString();//问号为一个时间段
Label1.Text = dt.Equals("2005-11-6 16:11:04").ToString();//False
Label2.Text = dt.Equals(dt).ToString();//True
Label3.Text = dt.GetHashCode().ToString();//1474088234
Label4.Text = dt.GetType().ToString();//System.DateTime
Label5.Text = dt.GetTypeCode().ToString();//DateTime
 
Label1.Text = dt.GetDateTimeFormats('s')[0].ToString();//2005-11-05T14:06:25
Label2.Text = dt.GetDateTimeFormats('t')[0].ToString();//14:06
Label3.Text = dt.GetDateTimeFormats('y')[0].ToString();//2005年11月
Label4.Text = dt.GetDateTimeFormats('D')[0].ToString();//2005年11月5日
Label5.Text = dt.GetDateTimeFormats('D')[1].ToString();//2005 11 05
Label6.Text = dt.GetDateTimeFormats('D')[2].ToString();//星期六 2005 11 05
Label7.Text = dt.GetDateTimeFormats('D')[3].ToString();//星期六 2005年11月5日
Label8.Text = dt.GetDateTimeFormats('M')[0].ToString();//11月5日
Label9.Text = dt.GetDateTimeFormats('f')[0].ToString();//2005年11月5日 14:06
Label10.Text = dt.GetDateTimeFormats('g')[0].ToString();//2005-11-5 14:06
Label11.Text = dt.GetDateTimeFormats('r')[0].ToString();//Sat, 05 Nov 2005 14:06:25 GMT
 
Label1.Text = string.Format("{0:d}",dt);//2005-11-5
Label2.Text = string.Format("{0:D}",dt);//2005年11月5日
Label3.Text = string.Format("{0:f}",dt);//2005年11月5日 14:23
Label4.Text = string.Format("{0:F}",dt);//2005年11月5日 14:23:23
Label5.Text = string.Format("{0:g}",dt);//2005-11-5 14:23
Label6.Text = string.Format("{0:G}",dt);//2005-11-5 14:23:23
Label7.Text = string.Format("{0:M}",dt);//11月5日
Label8.Text = string.Format("{0:R}",dt);//Sat, 05 Nov 2005 14:23:23 GMT
Label9.Text = string.Format("{0:s}",dt);//2005-11-05T14:23:23
Label10.Text = string.Format("{0:t}",dt);//14:23
Label11.Text = string.Format("{0:T}",dt);//14:23:23
Label12.Text = string.Format("{0:u}",dt);//2005-11-05 14:23:23Z
Label13.Text = string.Format("{0:U}",dt);//2005年11月5日 6:23:23
Label14.Text = string.Format("{0:Y}",dt);//2005年11月
Label15.Text = string.Format("{0}",dt);//2005-11-5 14:23:23?
Label16.Text = string.Format("{0:yyyyMMddHHmmssffff}",dt); //yyyymm等可以设置,比如Label16.Text = string.Format("{0:yyyyMMdd}",dt);
Read more...

窗体间控件的拖拽

February 1st, 2008 Arale No comments
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Text;
 using System.Windows.Forms;
 
namespace dragControl
{
	 public partial class Form1 : Form
	 {
		 private System.Windows.Forms.Button button1;
		 public Form1()
		 {
			InitializeComponent();
		 }
		 protected override void OnLoad(EventArgs e)
		 {
			 base.OnLoad(e);
			 this.button1 = new System.Windows.Forms.Button();
			 //
			 // button1
			 //
			 this.button1.Location = new System.Drawing.Point(116, 65);
			 this.button1.Name = "button1";
			 this.button1.Size = new System.Drawing.Size(75, 23);
			 this.button1.TabIndex = 0;
			 this.button1.Text = "button1";
			 this.button1.UseVisualStyleBackColor = true;
			 this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1_MouseDown);
			 this.Controls.Add(this.button1);
		}
		private void button1_MouseDown(object sender, MouseEventArgs e)
		{
			this.button1.DoDragDrop(sender, DragDropEffects.Move);
		}
		protected override void OnDoubleClick(EventArgs e)
		{
			 base.OnDoubleClick(e);
			 Form2 frm = new Form2();
			 frm.Show();
		}
	}
}

代码Form2.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace dragControl
{    
   public partial class Form2 : Form    
   {        
           public Form2()        
           {
                 InitializeComponent();
            }
             protected override void OnLoad(EventArgs e)
             {
                 base.OnLoad(e);
                 this.AllowDrop = true;
                 this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form2_DragDrop);
                 this.DragOver += new System.Windows.Forms.DragEventHandler(this.Form2_DragOver);
             }
             private void Form2_DragOver(object sender, DragEventArgs e)
             {
                if (e.AllowedEffect == DragDropEffects.Move)
                 {
                     e.Effect = DragDropEffects.Move;
                 }
            }
             private void Form2_DragDrop(object sender, DragEventArgs e)
             {
                 Button btn = e.Data.GetData("System.Windows.Forms.Button") as Button;
                 if (btn != null)
                {
                     if (btn.Parent != null)
                     {
                         btn.Parent.Controls.Remove(btn);
                         btn.Location = this.PointToClient(new Point(e.X, e.Y));
                         this.Controls.Add(btn);
                     }
                 }
             }
         }
   }
Read more...

文件读入内存

January 1st, 2008 Arale No comments
byte[] byteTimeSheet = Encoding.Default.GetBytes("");
//change this file to byte in order to save into database.
FileStream fs = new FileStream(timesheet, FileMode.Open);
byteTimeSheet = new byte[fs.Length];
fs.Read(byteTimeSheet, 0, byteTimeSheet.Length);
fs.Close();
Read more...

object sender和EventArgs e含义

December 7th, 2007 Arale No comments

我也是今天才完全搞明白这两个到底是什么意思,以前只知道sender,只知道e是事件,具体怎么个事也不是很清楚.不救甚解真是害死人啊..从cnblogs里面转了一篇文章…引以为戒.

sender是事件源
//表示触发事件的那个控件
比如说你按下按钮,那么sender就是按钮
又如:textboxchange,sender就是该textbox,在事件处理中就可以用sender代替textbox.
如: (sender as TextBox).Text=”fdsaewfsda”;
如果同一类控件处理方法相同,可以只写一个事件处理,其他的用这一个就行了sender就会指代当前触发事件的控件

EventArgs是事件参数
//该事件传入的参数,比如说你用鼠标点击窗体 那么EventArgs是会包含点击的位置等等 它用来辅助你处理事件
有一个叫做EventHandler 的家伙,他会告诉你(主程序),有一些事情发生了:这个事情是谁导致的呢?是某个object类型对象导致的,它用Sender来表示。这个事情是什么事呢?e的内容就是事情的内容了。所以,我们在程序中的事件处理函数就是依赖于这个东西实现的:比方说你点了一个按钮,程序怎么知道应该用哪个函数来处理这个动作呢?那么EventHandler 这个家伙会告诉程序:”button1(sender)被点击(e)了,请调用对应的处理函数”。当然这个函数是谁,这个函数要做什么,是由你自己写的。

再深入一层,这个过程实际上就是:你的动作被windows捕获,windows把这个动作作为系统消息发送给程序(可以看message结构),程序从自己的消息队列中不断的取出消息,并在消息循环中寻找对应的处理方式,这时message结构中的类似于sender和e的东东就起到了引导程序使用正确的处理函数的作用。归根究底,这个sender和e及其一整套的处理方式,只不过是windows消息机制的另外一种表现罢了

Read more...

虚函数的一道试题.

December 7th, 2007 Arale No comments

一般情况下虚函数是这样执行的.
当调用对象函数是, 系统会检查该对象的父类, 看是不是虚函数.
如果不是虚函数,则执行该对象的方法.
而如果有virtual关键字, 那么这个时候它就不会立刻执行该函数了,而是转去检查对象的实例类.
实例类里, 会检查这个实例类的定义中是否有重新实现该虚函数(通过override关键字),如果是有马上执行.
如果没有的话, 系统就会不停地往上找实例类的父类, 并对父类重复刚才在实例类里的检查, 直到找到第一个重载了该虚函数的父类为止,然后执行该父类里重载后的函数。

这个例子稍有不同. 这个例子考的应该不是虚函数.而是类的实例化的过程.

public class Examine
{
    public Examine()
    {
       PrintFields(); 
    }
    public virtual void PrintFields() { } 
}
public class B:Examine
{
    int x = 1;
    int y;
    public B()
    {
       y = -1;
    }
    public override void PrintFields()
    {
       Console.WriteLine("x={0},y={1}", x, y);
       Console.ReadLine();
     }
}

new B()的时候, 由于B是继承于Examine类, 所以当B生成实例的时候,

  • 程序会先执行类中的成员的定义.即int x =1, int y这两句.
  • 程序在执行完这两句之后, 会执行到B()的构造函数, 但是在构造函数这前会先去执行Examine的构造函数.因为B继承于Examine.
  • 这样之后, 实际触发的是Examine的构造函数的PrintFields()方法, 然后,由于是虚函数,在B的实例中重载了, 按照上面提到的.重载返回来执行B中的重载之后的代码.

这里是关于虚函数的一些比较容易理解的说法.
例如有两个类人员和老师,老师是人员的子类。人员中有一个虚函数DisplayInfo用来显示信息。但是,老师类有自己的成员函数也叫 DisplayInfo,并且参数相同,用于显示老师的信息。在C++中如果有一个老师对象,它是由一个人员的指针指向的(父类指针可以指向子类对象),则当调用DisplayInfo函数时会直接调用老师的DisplayInfo,而不调用人员的DisplayInfo。如果不是DisplayInfo 虚函数,上面的情况就会调用人员的DisplayInfo。

Read more...