标签:
出处http://support.microsoft.com/zh-cn/kb/317095
CREATE TABLE [tblQ317095] ( [ID] int IDENTITY (1,1) NOT NULL, [CharData] varchar (10) NOT NULL, [TimeStampCol] timestamp NULL) ALTER TABLE [tblQ317095] WITH NOCHECK ADD CONSTRAINT [PK_tblQ317095] PRIMARY KEY ([ID]) INSERT INTO tblQ317095 ([CharData]) VALUES (‘AAA‘) INSERT INTO tblQ317095 ([CharData]) VALUES (‘BBB‘) INSERT INTO tblQ317095 ([CharData]) VALUES (‘CCC‘)
using System.Data; using System.Data.SqlClient;
SqlConnection con = new SqlConnection(); SqlDataAdapter da; DataSet ds = new DataSet(); SqlCommand daUpdateCommand; DataTable dt;
con.ConnectionString = "Server=(local);Database=pubs;Trusted_Connection=yes;"; con.Open(); da = new SqlDataAdapter("SELECT * FROM tblQ317095 ORDER BY ID", con); daUpdateCommand = new SqlCommand("UPDATE tblQ317095 SET CharData = @pCharData WHERE TimeStampCol = @pTimeStamp",da.SelectCommand.Connection); //This is the field that you are updating. daUpdateCommand.Parameters.Add(new SqlParameter("@pCharData", SqlDbType.VarChar, 10)); daUpdateCommand.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)); daUpdateCommand.Parameters["@pTimeStamp"].SourceVersion=DataRowVersion.Original; daUpdateCommand.Parameters["@pTimeStamp"].SourceColumn = "TimeStampCol"; da.UpdateCommand = daUpdateCommand; //Fetch the data. da.FillSchema(ds, SchemaType.Source, "tblQ317095"); da.Fill(ds, "tblQ317095"); dt=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();
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()); DataSet DsModified; DsModified = ds.GetChanges(DataRowState.Modified); dataGrid1.DataSource = DsModified.Tables[0]; dataGrid1.CaptionText = "Modified Rows"; ds.RejectChanges(); } catch (Exception genException) { MessageBox.Show(genException.Message); } con.Close();
标签:
原文地址:http://www.cnblogs.com/hedongsong/p/4412900.html