C # update the TimeStamp field (TimeStamp) in SQLServer)

Source: Internet
Author: User

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! ");
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.