SQL如何实现更新多笔记录?
如何实现更新多条语句?下面代码只适用于所有表的单笔记录更新操作/// <summary>
/// 更新表的字段值
/// </summary>
/// <param name="Tablename">表名</param>
/// <param name="keyname">Key数组</param>
/// <param name="keyvalue">Keyvalue数组</param>
/// <param name="column">字段名数组</param>
/// <param name="value">字段名对应值</param>
/// <returns>成功与否</returns>
public virtual bool UpdateTableRow(string Tablename, string[] keyname, string[] keyvalue, string[] column, string[] value)
{
if(InteropHelper.HasEmsInterfaces) //如果要求跨平台数据集成
{
//这是一个示例,跨平台操作可能不能传递DataTable,那么需要修改接口
return InteropHelper.UpdateTableRow(Tablename, keyname, keyvalue, column, value);
}
#if DEBUG
try
{
#endif
//没有传入数组或数组对应字段不等
if (column.Length < 1 || column.Length != value.Length || keyname.Length != keyvalue.Length) return false;
string sqlString = Tablename + " set ";
for(int i = 0;i < column.Length; i++ )
{
sqlString = sqlString + column[i] + " = '" + value[i] + "'," ;
}
//去掉逗号
sqlString = sqlString.Substring(0, sqlString.Length - 1);
if(keyname.Length > 0)
{
sqlString += " where ";
for(int j = 0; j < keyname.Length; j++)
{
sqlString = sqlString + keyname[j] + "= '" + keyvalue[j] + "' and";
}
//去掉" and"
sqlString = sqlString.Substring(0, sqlString.Length - 4);
}
sqlConnect.Open();
SqlCommand updateCmd = new SqlCommand("Update " + sqlString,sqlConnect);
updateCmd.ExecuteNonQuery();
sqlConnect.Close();
return true; //更新成功
#if DEBUG
}
catch(Exception e)
{
sqlConnect.Close();
ProcessLog("Error at UpdateTableRow:" + e);
return false; //更新失败
}
#endif
}
要如何实现多笔记录的更新操作呢?