开发者

Problem with ADO.NET UPDATE code

Could somebody take a quick peek at my ado.net code? I am trying to update the row from a dataset, but it just isn't working. I am missing some elemental piece of the code, and it is just eluding me. I have verified that the DataRow actually has the correct data in it, so the row itself is accurate.

Many thanks in advance.

 try
            {
                //basic ado.net objects
                SqlDataAdapter dbAdapter = null;
                DataSet returnDS2 = new DataSet();

                //a new sql connection
                SqlConnection myConn = new SqlConnection();
                myConn.ConnectionString = "Server=myserver.mydomain.com;"
                     + "Database=mydatabase;"
                     + "User ID=myuserid;"
                     + "Password=mypassword;"
                     + "Trusted_Connection=True;";

                //the sqlQuery
                string sqlQuery = "select * from AVLUpdateMessages WHERE ID = 21";

                //another ado.net object for the command
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = myConn;
                cmd.CommandText = sqlQuery;

                //open the connection, execute the SQL statement and then close the connection.
                myConn.Open();

                //instantiate and fill the sqldataadapter
                dbAdapter = new SqlDataAdapter(cmd);
                dbAdapter.Fill(returnDS2, @"AVLUpdateMessages");

                //loop through all of the rows; I have verified that the rows are correct and returns the correct data from the db
                for (int i = 0; i <= returnDS2.Tables[0].Rows.Count - 1; i++)
                {
                    DataRow row = returnDS2.Tables[0].Rows[i];
                    ro开发者_运维知识库w.BeginEdit();
                    row["UpdatedText"] = @"This is a test...";
                    row.EndEdit();
                }

                //let's accept the changes
                dbAdapter.Update(returnDS2, "AVLUpdateMessages");
                returnDS2.AcceptChanges();

                myConn.Close();

            }  


I think you need an update query in your data adapter. I know, this sucks... Alternatively you can use CommandBuilder class to automatically generate queries for CRUD operations.

example at: http://www.programmersheaven.com/2/FAQ-ADONET-CommandBuilder-Prepare-Dataset


You might be able to use SqlCommandBuilder to help out. After the Fill call, add the following statement. That will associate a command builder with the data adapter and (if there is a primary key available) it should generate the update statement for you. Note that there is some expense behind the command builder. It may not be much relative to everything else, but it does involve looking at schema information (to get primary key information, field names, field types, etc.) for the table and generating INSERT, DELETE, and UPDATE statements involving all fields in the table.

SqlCommandBuilder cb = new SqlCommandBuilder(dbAdapter);


Wait, why not something like

update AVLUpdateMessages set UpdatedText = 'This is a test...' where id = 21

If you're picking through all the rows of a table to update one at a time, you're probably doing it wrong. SQL is your friend.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜