我無法理解我在這裏做錯了什麼。我已經能夠使用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;
顯然,我的更新命令可以正常工作,但很快就被上面的代碼取代了原來的文本框內容。
乾杯。
您應該提供**編輯2 **作爲答案。添加一些具體問題的細節,這將有助於未來的用戶。 :) – IAbstract 2013-04-07 14:56:01