2012-03-27 131 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