使用相同的SQLParameter更新多个表?

时间:2013-03-27 15:53:20

标签: c# asp.net .net

我需要更新几个共同具有相同字段的tabless。

现在我对每个表都有一个单独的更新语句,如下所示:

try
        {
            using (SqlConnection conn = new SqlConnection(cCon.getConn()))
            {

                using (SqlCommand cmd = conn.CreateCommand())
                {
conn.Open();
                    cmd.CommandText = "update table0 set active= 'N' where id=@id";
                    cmd.Parameters.Add(new SqlParameter("@id", id));
                    cmd.ExecuteNonQuery();

                    cmd.Parameters.Clear();
                    cmd.CommandText = "update table1 set active= 'N' where id= @id ";
                    cmd.Parameters.Add(new SqlParameter("@id", id));
                    cmd.ExecuteNonQuery();

                    cmd.Parameters.Clear();
                     cmd.CommandText = "update table2 set active= 'N' where id= @id "
                    cmd.Parameters.Add(new SqlParameter("@id", id));
                    cmd.ExecuteNonQuery();

                    cmd.Parameters.Clear();
                    cmd.CommandText = "update table4 set active= 'N' where id= @id "
                    cmd.Parameters.Add(new SqlParameter("@id", id));
                    cmd.ExecuteNonQuery();
                }
            }
        }

我可以优化代码以减少调用次数吗?也许将所有更新合并到1个命令文本中,只执行一次?

因为它是所有相同的参数,我认为它应该是可能的?或者我应该制作一个存储过程?

3 个答案:

答案 0 :(得分:1)

理想情况下,您将调用存储过程来处理此问题。但是在批处理语句中使用相同的参数也可以正常工作:

cmd.CommandText = @"update table0 set active= 'N' where id=@id;
      update table1 set active= 'N' where id= @id;
      update table2 set active= 'N' where id= @id;
      update table4 set active= 'N' where id= @id;";
cmd.Parameters.Add(new SqlParameter("@id", id));
cmd.ExecuteNonQuery();

答案 1 :(得分:1)

const string query = @"update {0} set active= 'N' where id=@id;";

public string GetCommandText(string table)
{
    return string.Format(query, table);
}

public IEnumerable<string> GetTables()
{
    yield return "table0";
    yield return "table1";
    yield return "table2";
    yield return "table4";
}

using (SqlCommand cmd = conn.CreateCommand())
{
    cmd.Parameters.Add(new SqlParameter("@id", id));
    cmd.CommandText = GetTables().Select(GetCommandText).Aggregate((s, s1) => s + s1);

    conn.Open();
    cmd.ExecuteNonQuery();
}

答案 2 :(得分:0)

删除

cmd.Parameters.Clear();

你只使用一次这段代码

 cmd.Parameters.Add(new SqlParameter("@id", id));

所以:

cmd.CommandText = "update table0 set active= 'N' where id=@id";
                    cmd.Parameters.Add(new SqlParameter("@id", id));
                    cmd.ExecuteNonQuery();


                    cmd.CommandText = "update table1 set active= 'N' where id= @id ";
                    cmd.ExecuteNonQuery();

                     cmd.CommandText = "update table2 set active= 'N' where id= @id "
                    cmd.ExecuteNonQuery();

                    cmd.CommandText = "update table4 set active= 'N' where id= @id "
                    cmd.ExecuteNonQuery();
                    cmd.Parameters.Clear();