Ado. NET timestamp use

Source: Internet
Author: User

SOURCE http://support.microsoft.com/zh-cn/kb/317095

Profile

This step-by-step article uses the Microsoft SQL Server timestamp field to reduce the amount of data that is sent to the server to detect update conflicts. By default, the CommandBuilder objects ( sqlclientcommandbuilder and oledbcommandbuilder) Generates a collision detection based on all field values. by using custom UpdateCommand, you can specify which columns to use.

Create a SQL Server tableto create a test table and insert some test records, use the following script:
CREATE TABLE [tblQ317095] ([ID] int IDENTITY () not NULL, [chardata] varchar (TEN) not Null,[timestampcol] Timestamp NUL L) ALTER TABLE [tblQ317095] with NOCHECK ADD CONSTRAINT [pk_tblq317095] PRIMARY KEY ([ID]) INSERT into tblQ317095 ([Chardata ] VALUES (' AAA ') insert into tblQ317095 ([chardata]) The values (' BBB ') insert into tblQ317095 ([chardata]) VALUES (' CCC ')
Create a Visual c#.net application
  1. Create a new Visual c#.net Windows application.
  2. From the Windows Forms tab in the Toolbox, add the following controls to the form:
    • Adds a button control as cmdupdate.
    • txtID, and txtchardata,txttimestamp add three text box controls. (Set txtID and txttimestamp as read-only to show that these fields cannot be updated).
    • Add a DataGrid control.
  3. Use statements are used on the system and System.Data.SqlClient namespaces so that you do not have to qualify them for later declarations in these namespaces in your code. The following code is added to the General Declarations section of Form1:
    Using system.data;using System.Data.SqlClient;
  4. Add the following declaration to the class:
    SqlConnection con = new SqlConnection (); SqlDataAdapter da;dataset ds = new DataSet (); SqlCommand daupdatecommand;datatable DT;
  5. Form_Load event:
    Con. ConnectionString = "server= (local);D atabase=pubs; Trusted_connection=yes; "; Con. Open ();d a = new SqlDataAdapter ("select * from tblQ317095 ORDER by ID", con);d Aupdatecommand = new SqlCommand ("UPDATE tblQ3 17095 SET chardata = @pCharData WHERE Timestampcol = @pTimeStamp ", Da. SelectCommand.Connection);//this is the field, which is Updating.daUpdateCommand.Parameters.Add (new SqlParameter ("@ Pchardata ", SqlDbType.VarChar, ten);d aupdatecommand.parameters[" @pCharData "]. SourceVersion = datarowversion.current;daupdatecommand.parameters["@pCharData"]. SourceColumn = "Chardata";//use the TimeStamp to locate your ROW.DAUPDATECOMMAND.PARAMETERS.ADD (new SqlParameter ("@ Ptimestamp ", sqldbtype.binary));d aupdatecommand.parameters[" @pTimeStamp "]. sourceversion=datarowversion.original;daupdatecommand.parameters["@pTimeStamp"]. SourceColumn = "Timestampcol";d A. UpdateCommand = Daupdatecommand;//fetch The Data.da.FillSchema (ds, SchemaType.Source, "tblQ317095");d A. Fill (ds, "tblQ317095");d T=ds. tables["tblQ317095"];//show the data in the Textboxes.txtID.Text = dt. Rows[0][0]. ToString (); Txtchardata.text =dt. ROWS[0][1]. ToString (); txttimestamp.text= dt. ROWS[0][2]. ToString ();
  6. Modify the connection string property (in the first line of the code in step 5) to correspond to your SQL Server connection information. Make sure that you connect to the database that is running the SQL script to create the test table.
  7. Add the following code to the Cmdupdate_click event:
    Dt. rows[0]["Chardata"] = Txtchardata.text;try{da. Update (DT); MessageBox.Show ("Update was successful"); catch (System.Data.DBConcurrencyException dbexception) {MessageBox.Show (dbException.Message.ToString ()));D Ataset Dsmodified;dsmodified = ds. GetChanges (datarowstate.modified);d Atagrid1.datasource = dsmodified.tables[0];d atagrid1.captiontext = "Modified Rows ";d S. RejectChanges ();} catch (Exception genexception) {MessageBox.Show (genexception.message);} Con. Close ();
c#.net Application for testing
    1. To run the application (extract data in the Form_Load event), on the Debug menu, click Start .
    2. In Query Analyzer (or Enterprise Manager), update the chardata field in the record #1 to the new value.
    3. Return to the application and change the chardata to a different value.
    4. Click the button on the form. be aware that you receive an exception.

Ado. NET timestamp use

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.