VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之C#更新SQLServer中TimeStamp字段(时间戳)的方

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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#程序设计有所帮助。


相关教程