运行多个sqlcommands

时间:2018-06-12 07:24:20

标签: c# sql asp.net visual-studio sqlcommand

执行多个sql命令时遇到问题。我需要多次更新数据。 frist sql查询是否考虑两种情况,否则;但是第二个和第三个sqlcommand将在if或else中相应。 问题是只有一个sqlcommand正常工作,那些位于循环内部。

public void Button_Submit_Onclick(object sender, EventArgs e)
    {

        for (int i = 0; i < GridView2.Rows.Count; i++)
        {
            con.ConnectionString = ConfigurationManager.ConnectionStrings["TestDeductionsConnectionString2"].ToString();
            int recordid = Convert.ToInt32(GridView2.DataKeys[i].Values[0]);

            CheckBox cbox = (CheckBox)GridView2.Rows[i].FindControl("CheckBox1");
            bool private1 = Convert.ToBoolean(cbox.Checked); 

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "Update DetailCosts set private='" + private1 + "' where recordid=" + recordid;

            con.Open();
            if (private1==true)
            {
                cmd.CommandText = "Update DetailCosts set privateCost=Costs where recordid=" + recordid;
                cmd.Parameters.AddWithValue("@private1", SqlDbType.Bit).Value = private1;
                cmd.Parameters.AddWithValue("@recordid", SqlDbType.Int).Value = recordid.ToString();

            }
            else
            {
                cmd.CommandText = "Update DetailCosts set privateCost=0 where recordid=" + recordid;
                cmd.Parameters.AddWithValue("@recordid", SqlDbType.Int).Value = recordid.ToString();
                cmd.Parameters.Add("@private1", SqlDbType.Bit).Value = private1;

            }
            cmd.ExecuteNonQuery();
            con.Close();
        }


    }

2 个答案:

答案 0 :(得分:2)

您需要执行要执行的每个命令,因此您应该添加

cmd.ExecuteNonQuery();
在第一次查询之后

也要执行它。

如果你想要两者都被执行而不是一个失败你应该实现transactions并在事务中包装两个查询。

答案 1 :(得分:-1)

这是一个有效的代码:

public void Button_Submit_Onclick(object sender, EventArgs e)
    {

        for (int i = 0; i < GridView2.Rows.Count; i++)
        {
            con.ConnectionString = ConfigurationManager.ConnectionStrings["TestDeductionsConnectionString2"].ToString();
            int recordid = Convert.ToInt32(GridView2.DataKeys[i].Values[0]);
            //bool private1 = Convert.ToBoolean(GridView2.FindControl("CheckBox1"));
            CheckBox cbox = (CheckBox)GridView2.Rows[i].FindControl("CheckBox1");
            bool private1 = Convert.ToBoolean(cbox.Checked); 

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            con.Open();
            cmd.CommandText = "Update DetailCosts set private='" + private1 + "' where recordid=" + recordid;

            cmd.Parameters.AddWithValue("@private1", SqlDbType.Bit).Value = private1;
            cmd.Parameters.AddWithValue("@recordid", SqlDbType.Int).Value = recordid.ToString();
            cmd.ExecuteNonQuery();

            if (private1==true)
            {
                //DateTime date = DateTime.Now;
                //cmd.CommandText = "Update AprovedCosts set AprovedCosts.AprovalUser = ";

                cmd.CommandText = "Update DetailCosts set privateCost=Costs where recordid=" + recordid;
                cmd.ExecuteNonQuery();
            }
            else
            {

                cmd.CommandText = "Update DetailCosts set privateCost=0 where recordid=" + recordid;
                cmd.ExecuteNonQuery();
            }
            con.Close();
        }