程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#更新SQLServer中的TimeStamp字段(時間戳)

C#更新SQLServer中的TimeStamp字段(時間戳)

編輯:C#入門知識

  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("更新成功!");                                   
            }
        }
    }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved