数据库小技巧

求两个时间段的时间差:

DateTime starttime = DateTime.Now;

DateTime endtime
= DateTime.Now;

差值为:          

TimeSpan t = endtime - starttime;

获取表中的字段:             

select name from syscolumns where id=object_id('Student')

有选择的更新记录:

           

adapter.Update(ds.Tables[0].Select(null,null,DataViewRowState.Added));

在对 一张表进行操作时,有时候可能不会要求全部更新,或要求只更新添加的记录或删除的记录,这时候就可以用这条语句

Example:

 

//定义连个对象
DataSet ds = null;
SqlDataAdapter adapter
= null;

//这是一般的查询方法
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Test;Integrated Security=True");
con.Open();
SqlCommand cmd
= new SqlCommand("select * from Student where ID=406302", con);
adapter
= new SqlDataAdapter(cmd);
ds
= new DataSet();
adapter.Fill(ds);
dataGridView1.DataSource
= ds.Tables[0];
//经过一段操作,我分别对DataGridView进行了增删改查的操作,我现在只更新添加记录的操作,使得其他的操作都不更新到数据库
//更新按钮
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
//有选择的更新
adapter.Update(ds.Tables[0].Select(null,null,DataViewRowState.Deleted));

这样就可以啦,不信你试试......


进行下一问题..

在写存储过程的时候免不了有许多参数,在C#调用的时候,要为参数赋值的话,你就必须要知道这些参数,这就浪费了很多时间而且有时候参数写错的话就会出问题
现在进行的是自动发现SP的参数,这个我也是"研究"了一下SqlHelper后才发现的...

Example:

 

sqlcommand cmd=new sqlcommand("AddUser",con);
cmd.CommandType=commandType.StoredProcedure;
//瞪大眼睛
SqlCommandBuilder.DeriveParameters(cmd);
sqlparameter paras
=new sqlparameter[cmd.parameters.count];
cmd.Parameters.CopyTo(paras,
0)
//这样参数就会被 读到paras数组中去了

 

 

数组中的第一个元素是Return_value是默认的,具体我也没深究啊..

posted @ 2009-12-30 09:19  槑槑  阅读(273)  评论(0编辑  收藏  举报