当前位置: 移动技术网 > IT编程>开发语言>c# > C#更新SQLServer中TimeStamp字段(时间戳)的方法

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

2019年07月18日  | 移动技术网IT编程  | 我要评论
本文实例讲述了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#程序设计有所帮助。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网