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