Csharp/C#教程:数据未写入数据库分享


数据未写入数据库

我正在为CheckboxProcess_CheckedChanged事件中的数据库写一些值。 但是,实际上没有任何内容被写出来。 我有一些代码填充适配器,但我把它拿出来。 任何人都可以看到这出错的地方吗?

protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { using (SqlConnection connection = new SqlConnection(connectionString.ToString())) { connection.Open(); dataAdapter = new SqlDataAdapter("SELECT * FROM SecureOrders", connection); dataSet = new DataSet(); dataAdapter.Fill(dataSet, "SecureOrders"); DataView source = new DataView(dataSet.Tables[0]); DefaultGrid.DataSource = source; DefaultGrid.DataBind(); connection.Close(); } } } protected void CheckBoxProcess_CheckedChanged(object sender, EventArgs e) { bool update; string checkedString = "UPDATE SecureOrders SET processed = 1 WHERE fName LIKE '%" + DefaultGrid.SelectedRow.Cells[2].Text + "%' AND lName LIKE '% " + DefaultGrid.SelectedRow.Cells[3].Text + "%'"; string uncheckedString = "UPDATE SecureOrders SET processed = 0 WHERE fName LIKE '%" + DefaultGrid.SelectedRow.Cells[2].Text + "%' AND lName LIKE '% " + DefaultGrid.SelectedRow.Cells[3].Text + "%'"; CheckBox cb = (CheckBox)sender; GridViewRow gvr = (GridViewRow)cb.Parent.Parent; DefaultGrid.SelectedIndex = gvr.RowIndex; update = Convert.ToBoolean(DefaultGrid.SelectedValue); orderByString = orderByList.SelectedItem.Value; fieldString = searchTextBox.Text; connectionString = rootWebConfig.ConnectionStrings.ConnectionStrings["secureodb"]; using (SqlConnection connection = new SqlConnection(connectionString.ToString())) { connection.Open(); SqlCommand checkedCmd = new SqlCommand(checkedString, connection); SqlCommand uncheckedCmd = new SqlCommand(uncheckedString, connection); if (cb.Checked == true) { checkedCmd.ExecuteNonQuery(); } else { uncheckedCmd.ExecuteNonQuery(); } connection.Close(); } 

对于您的更新语句,您有:

 string checkedString = "UPDATE SecureOrders SET processed = 1 WHERE fName LIKE '%" + DefaultGrid.SelectedRow.Cells[2].Text + "%' AND lName LIKE '% " + DefaultGrid.SelectedRow.Cells[3].Text + "%'"; 

我想知道你是否需要删除最后一个%标记后的空格:

 string checkedString = "UPDATE SecureOrders SET processed = 1 WHERE fName LIKE '%" + DefaultGrid.SelectedRow.Cells[2].Text + "%' AND lName LIKE '%" + DefaultGrid.SelectedRow.Cells[3].Text + "%'"; 

我建议你尝试以下步骤:

您的代码看起来像这样:

 protected void CheckBoxProcess_CheckedChanged(object sender, EventArgs e) { bool update = Convert.ToBoolean(DefaultGrid.SelectedValue); // determine your first name and last name values string firstName = .......; string lastName = .......; UpdateYourData(update, firstName, lastName); } private void UpdateYourData(bool isProcessed, string firstName, string lastName) { Configuration rootWebConfig = WebConfigurationManager.OpenWebConfiguration("/Cabot3"); ConnectionStringSettings connectionString = rootWebConfig.ConnectionStrings.ConnectionStrings["secureodb"]; string updateStmt = "UPDATE dbo.SecureOrders SET processed = @Processed " + "WHERE fName LIKE @firstName AND lName LIKE @lastName"; using (SqlConnection connection = new SqlConnection(connectionString.ToString())) using (SqlCommand _update = new SqlCommand(updateStmt, connection)) { _upate.Parameters.Add("@Processed", SqlDbType.Bit).Value = isProcessed; _upate.Parameters.Add("@firstName", SqlDbType.VarChar, 100).Value = firstName; _upate.Parameters.Add("@lastName", SqlDbType.VarChar, 100).Value = lastName; connection.Open(); _update.ExecuteNonQuery(); connection.Close(); } } 

现在我不知道这是否真的能够解决你的问题 – 我一眼就看不到任何东西……但是试试看 – 也许这会让你在解决问题上先行一步!

上述就是C#学习教程:数据未写入数据库分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/989756.html

(0)
上一篇 2021年12月23日
下一篇 2021年12月23日

精彩推荐