C#更新SQLServer中TimeStamp字段(时间戳)的方法
本文实例讲述了C#更新SQLServer中TimeStamp字段(时间戳)的方法。分享给大家供大家参考。具体实现方法如下:
publicpartialclassForm1:Form
{
privateSqlConnectionmCnn=null;
privatelongTimeStampValue;
publicForm1()
{
InitializeComponent();
mCnn=newSqlConnection();
mCnn.ConnectionString="DataSource=192.168.18.205;Database="+
"Test;Uid=sa;Pwd=kicpassword";
mCnn.Open();
}
//读取
privatevoidbtnReadTimeStamp_Click(objectsender,EventArgse)
{
//使用convert(bigint,FTimeStamp)MyTimeStamp把FTimeStamp转换为bigint整数类型
stringstrSQL="SelectTop1FCaption,convert(bigint,FTimeStamp)MyTimeStamp,FTimeStampFromt_TimeStamp";
SqlCommandCmd=newSqlCommand(strSQL,mCnn);
SqlDataReaderReader=Cmd.ExecuteReader();
while(Reader.Read())
{
longvalue=(long)Reader["MyTimeStamp"];
TimeStampValue=value;
//在此处把TimeStamp的值赋值给变量,用于更新时判断
txtTimeStamp.Text=value.ToString();
txtTimeStampHex.Text=value.ToString("X16");
}
Reader.Close();
Reader=null;
}
//更新
privatevoidbtnUpdateTimeStamp_Click(objectsender,EventArgse)
{
//Where条件加上TimeStamp的判断,必须和读取的一致,用于并发操作
stringstrSQL="Updatet_TimeStampSetFCaption='修改记录'"+
"WhereFTimeStamp="+TimeStampValue.ToString();
SqlCommandcmd=newSqlCommand(strSQL,mCnn);
intUpdateCount=cmd.ExecuteNonQuery();
if(UpdateCount<=0)
{
MessageBox.Show("更新失败!");
}
else
{
MessageBox.Show("更新成功!");
}
}
}
希望本文所述对大家的C#程序设计有所帮助。