数据未写入数据库

时间:2011-07-12 15:58:49

标签: c# asp.net sql database sql-update

我正在为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();
        }

2 个答案:

答案 0 :(得分:3)

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

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 + "%'";

答案 1 :(得分:2)

我建议你尝试以下步骤:

  • 将UI代码(读取和写入文本框以及网格和内容)与实际数据库代码分开 - 在某些时候,您可能希望将这些代码分离到单独的程序集,甚至

  • 使用参数化查询来更新数据!防止SQL注入攻击,并使你的东西更快!使用它们 - 永远 - 没有借口

您的代码看起来像这样:

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();
    }
}

现在我不知道这是否真的能够解决你的问题 - 我一眼就看不到任何东西......但是试试看 - 也许这会让你在解决问题方面先行一步! / p>