欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#更新SQLServer中TimeStamp字段(时间戳)的方法

程序员文章站 2022-08-03 21:37:13
本文实例讲述了c#更新sqlserver中timestamp字段(时间戳)的方法。分享给大家供大家参考。具体实现方法如下: public partial clas...

本文实例讲述了c#更新sqlserver中timestamp字段(时间戳)的方法。分享给大家供大家参考。具体实现方法如下:

public partial class form1 : form
{
    private sqlconnection mcnn = null;
    private long timestampvalue;
    public form1()
    {
      initializecomponent();
      mcnn = new sqlconnection();
      mcnn.connectionstring = "data source=192.168.18.205;database=" +
              "test;uid=sa;pwd=kicpassword";
      mcnn.open();
    }
    //读取
    private void btnreadtimestamp_click(object sender, eventargs e)
    {
      //使用convert(bigint,ftimestamp) mytimestamp把ftimestamp转换为bigint整数类型
      string strsql = "select top 1 fcaption,convert(bigint,ftimestamp) mytimestamp,ftimestamp from t_timestamp";
      sqlcommand cmd = new sqlcommand(strsql, mcnn);
      sqldatareader reader = cmd.executereader();
      while (reader.read())
      {
        long value = (long)reader["mytimestamp"];
        timestampvalue = value;
        //在此处把timestamp的值赋值给变量,用于更新时判断
        txttimestamp.text = value.tostring();
        txttimestamphex.text=value.tostring("x16");
      }
      reader.close();
      reader = null;
    }
    //更新
    private void btnupdatetimestamp_click(object sender, eventargs e)
    {
      //where条件加上timestamp的判断,必须和读取的一致,用于并发操作
      string strsql = "update t_timestamp set fcaption = '修改记录'" +
        " where ftimestamp =" + timestampvalue.tostring();
      sqlcommand cmd = new sqlcommand(strsql, mcnn);
      int updatecount = cmd.executenonquery();
      if (updatecount <= 0)
      {
        messagebox.show("更新失败!");
      }
      else
      {
        messagebox.show("更新成功!");                  
      }
    }
}

希望本文所述对大家的c#程序设计有所帮助。