数据未写入数据库

发布于 2024-11-19 16:34:32 字数 2265 浏览 1 评论 0原文

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

I'm writing a bit value to a database in the CheckboxProcess_CheckedChanged event. However, nothing is actually being written out. I had some code in there filling an adapter, but I took it out. Can anybody see where this is going wrong?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人生戏 2024-11-26 16:34:32

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

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

For your update statements you have:

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

I'm wondering if you need to remove the space after the last % mark:

string checkedString = "UPDATE SecureOrders SET processed = 1 WHERE fName LIKE '%" + DefaultGrid.SelectedRow.Cells[2].Text + "%' AND lName LIKE '%" + DefaultGrid.SelectedRow.Cells[3].Text + "%'";
北方的韩爷 2024-11-26 16:34:32

我建议您尝试以下步骤:

  • 将 UI 代码(读取和写入文本框、网格等)与实际数据库代码分开 - 在某些时候,您可能希望将它们分离为单独的程序集,甚至< /p>

  • 使用参数化查询/strong> 用于更新您的数据!防止 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();
    }
}

现在我不知道这是否真的能解决你的问题 - 我第一眼看不到任何东西......但是尝试一下 - 也许那样将使您在隔离问题方面领先一步!

I would suggest you try the following steps:

  • separate the UI code (reading and writing the textboxes and grid and stuff) from the actual database code - at some point, you might want to separate those out to separate assemblies, even

  • use parametrized queries for updating your data! Prevents SQL injection attacks, and makes your stuff go faster, too! Use them - always - no excuses

Your code would then look something like this:

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

Now I don't know if that's really going to solve your problem - I couldn't see anything at first sight.... but try it - maybe that'll give you a head start towards isolating your issues!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文