2012-03-27 132 views
1

我无法理解我在这里做错了什么。我已经能够使用SQL Server数据库中的数据选择并填充表单,而不会出现任何问题。现在,当我尝试通过修改的数据集写回数据库时,没有任何反应。显然,更新命令不起作用,我试图找出原因。以下是代码。C#数据集无法更新SQL Server数据库表

[我是一个新手,C#和SQL,所以我会很感激,如果你能解释一下像我5 :)]

编辑:我100%肯定它连接到数据库,检索数据并填充数据集。

if (!(String.IsNullOrEmpty(Request.QueryString["newsID"]))) 
    {    

     SqlDataAdapter UpdateNewsSDA = new SqlDataAdapter("SELECT newsID, newsTitle, newsAuthor, newsDate, shortContent, mainContent FROM news_Table WHERE newsID = @newsID", connectObj); 
     UpdateNewsSDA.SelectCommand.Parameters.Add("@newsID", SqlDbType.Int).Value = Convert.ToInt32(Request.QueryString["newsID"]); 

     UpdateNewsSDA.UpdateCommand = new SqlCommand("UPDATE news_table SET [email protected], [email protected], [email protected], [email protected], [email protected] WHERE [email protected]", connectObj); 

     UpdateNewsSDA.UpdateCommand.Parameters.Add("@newsID", SqlDbType.Int).Value = Convert.ToInt32(Request.QueryString["newsID"]); 
     UpdateNewsSDA.UpdateCommand.Parameters.Add("@newsTitle", SqlDbType.Text).Value = title_Textbox.Text; 
     UpdateNewsSDA.UpdateCommand.Parameters.Add("@newsAuthor", SqlDbType.Text).Value = author_Textbox.Text; 
     UpdateNewsSDA.UpdateCommand.Parameters.Add("@newsDate", SqlDbType.DateTime).Value = Convert.ToDateTime(date_Textbox.Text); 
     UpdateNewsSDA.UpdateCommand.Parameters.Add("@shortContent", SqlDbType.Text).Value = shortContent_Textbox.Text; 
     UpdateNewsSDA.UpdateCommand.Parameters.Add("@mainContent", SqlDbType.Text).Value = mainContent_Textbox.Text;      

     DataSet UpdateNewsDS = new DataSet(); 

     SqlCommandBuilder UpdateNewsCommandBuilder = new SqlCommandBuilder(UpdateNewsSDA); 
     UpdateNewsSDA.MissingSchemaAction = MissingSchemaAction.AddWithKey; 

     UpdateNewsSDA.FillSchema(UpdateNewsDS, SchemaType.Source); 
     UpdateNewsSDA.Fill(UpdateNewsDS); 

     DataTable UpdateNewsTable = new DataTable(); 
     UpdateNewsTable = UpdateNewsDS.Tables[0]; 

     DataRow CurrentDR; 

     CurrentDR = UpdateNewsTable.Rows.Find(Convert.ToInt32(Request.QueryString["newsID"])); 
     CurrentDR.BeginEdit(); 
     CurrentDR["newsAuthor"] = "Ron Weasely"; 
     CurrentDR.AcceptChanges(); 
     CurrentDR.EndEdit(); 


     UpdateNewsSDA.Update(UpdateNewsDS);    

    } 

编辑2:我发现了这个问题,它的下面这个块!

UpdateNewsSDA.UpdateCommand = new SqlCommand("UPDATE news_table SET [email protected], [email protected], [email protected], [email protected], [email protected] WHERE [email protected]", connectObj); 

     UpdateNewsSDA.UpdateCommand.Parameters.Add("@newsID", SqlDbType.Int).Value = Convert.ToInt32(Request.QueryString["newsID"]); 
     UpdateNewsSDA.UpdateCommand.Parameters.Add("@newsTitle", SqlDbType.Text).Value = title_Textbox.Text; 
     UpdateNewsSDA.UpdateCommand.Parameters.Add("@newsAuthor", SqlDbType.Text).Value = author_Textbox.Text; 
     UpdateNewsSDA.UpdateCommand.Parameters.Add("@newsDate", SqlDbType.DateTime).Value = Convert.ToDateTime(date_Textbox.Text); 
     UpdateNewsSDA.UpdateCommand.Parameters.Add("@shortContent", SqlDbType.Text).Value = shortContent_Textbox.Text; 
     UpdateNewsSDA.UpdateCommand.Parameters.Add("@mainContent", SqlDbType.Text).Value = mainContent_Textbox.Text; 

显然,我的更新命令可以正常工作,但很快就被上面的代码取代了原来的文本框内容。

干杯。

+0

您应该提供**编辑2 **作为答案。添加一些具体问题的细节,这将有助于未来的用户。 :) – IAbstract 2013-04-07 14:56:01

回答

0

删除CurrentDR.AcceptChanges();因为它会将您的DataRow设置为未修改,并且更改将不会保留在数据库中。

+0

我已经这样做了,它似乎没有改变任何东西。我也试过评论CurrentDR.BeginEdit(); CurrentDR.AcceptChanges();和CurrentDR.EndEdit();仍然没有快乐:( – user1293440 2012-03-27 10:14:04

+1

啊!我发现了这个问题并且排序了它,干杯(见编辑2) – user1293440 2012-03-27 11:33:00