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 ();
}
// Read
Private void btnReadTimeStamp_Click (object sender, EventArgs e)
{
// Use convert (bigint, FTimeStamp) MyTimeStamp to convert FTimeStamp to bigint Integer type
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; // assign the value of TimeStamp to the variable for judgment during update.
TxtTimeStamp. Text = value. ToString ();
TxtTimeStampHex. Text = value. ToString ("X16 ");
}
Reader. Close ();
Reader = null;
}
// Update
Private void btnUpdateTimeStamp_Click (object sender, EventArgs e)
{
// When the Where condition is added with TimeStamp, it must be consistent with the read condition for concurrent operations.
String strSQL = "Update t_TimeStamp Set FCaption = 'modify records'" +
"Where FTimeStamp =" + TimeStampValue. ToString ();
SqlCommand cmd = new SqlCommand (strSQL, mCnn );
Int UpdateCount = cmd. ExecuteNonQuery ();
If (UpdateCount <= 0)
{
MessageBox. Show ("update failed! ");
}
Else
{
MessageBox. Show ("Update successful! ");
}
}
}