很多时候,尤其是C/S程序,我们需要在内存中拼DataTable,然后希望直接传递都数据库更新,并且能够返回更新后的DataTable。
使用下列方法表中必须有且只有一个主键,自增,int,并且在首行
/// <summary>
/// 批量更新
/// </summary>
/// <param name="dt">待更新的DataTable</param>
/// <returns>更新后的DataTable</returns>
public DataTable UpdateDataTable(DataTable dt)
{
if (dt.Rows.Count == 0)
{
return dt;
}
string sSqlInsert = "";
string sSqlUpdate = "";
string sSqlDelete = "";
for (int iRows = 0; iRows < dt.Rows.Count; iRows++)
{
if (dt.Rows[iRows].RowState == DataRowState.Added)
{
sSqlInsert = "INSERT INTO [" + dt.TableName + "] (";
//遍历DataTable列 第一列是主键
for (int i = 1; i < dt.Columns.Count; i++)
{
sSqlInsert += "[" + dt.Columns[i].ColumnName + "],";
}
sSqlInsert = sSqlInsert.Substring(0, sSqlInsert.Length - 1);
sSqlInsert += ") VALUES (";
//第一列主键 不处理
for (int i = 1; i < dt.Columns.Count; i++)
{
if (dt.Columns[i].DataType == typeof(System.DateTime))
{
//如果日期没有赋值
if (dt.Rows[iRows][i].ToString() == "")
{
if (i == 1)
{
sSqlInsert += "getdate()";
}
else
{
sSqlInsert += ",getdate()";
}
}
else
{
if (i == 1)
{
sSqlInsert += "'" + dt.Rows[iRows][i].ToString() + "'";
}
else
{
sSqlInsert += ",'" + dt.Rows[iRows][i].ToString() + "'";
}
}
}
else if (dt.Columns[i].DataType == typeof(System.Int32))
{
if (i == 1)
{
sSqlInsert += "" + dt.Rows[iRows][i].ToString() + " ";
}
else
{
sSqlInsert += "," + dt.Rows[iRows][i].ToString() + " ";
}
}
else
{
if (i == 1)
{
sSqlInsert += "'" + dt.Rows[iRows][i].ToString() + "'";
}
else
{
sSqlInsert += ",'" + dt.Rows[iRows][i].ToString() + "'";
}
}
}
sSqlInsert += ")";
//返回刚刚插入的记录
sSqlInsert += ";Select * from " + dt.TableName + " Where [" + dt.Columns[0].ColumnName + "]= SCOPE_IDENTITY()";
//赋值给主键
dt.Rows[iRows][0] = GetDataTable(sSqlInsert).Rows[0][0];
}
if (dt.Rows[iRows].RowState == DataRowState.Modified)
{
sSqlUpdate += "UPDATE [" + dt.TableName + "] SET ";
//第一列是主键 不处理
for (int i = 1; i < dt.Columns.Count; i++)
{
if (dt.Columns[i].DataType == typeof(System.Int32))
{
if (i == 1)
{
sSqlUpdate += "[" + dt.Columns[i].ColumnName + "] = " + dt.Rows[iRows][i].ToString() + "";
}
else
{
sSqlUpdate += ", [" + dt.Columns[i].ColumnName + "] = " + dt.Rows[iRows][i].ToString() + "";
}
}
else if (dt.Columns[i].DataType == typeof(System.DateTime))
{
//如果日期没有赋值
if (dt.Rows[iRows][i].ToString() == "")
{
if (i == 1)
{
sSqlUpdate += "[" + dt.Columns[i].ColumnName + "] = getdate()";
}
else
{
sSqlUpdate += ", [" + dt.Columns[i].ColumnName + "] = getdate()";
}
}
else
{
if (i == 1)
{
sSqlUpdate += "[" + dt.Columns[i].ColumnName + "] = '" + dt.Rows[iRows][i].ToString() + "'";
}
else
{
sSqlUpdate += ", [" + dt.Columns[i].ColumnName + "] = '" + dt.Rows[iRows][i].ToString() + "'";
}
}
}
else
{
if (i == 1)
{
sSqlUpdate += "[" + dt.Columns[i].ColumnName + "] = '" + dt.Rows[iRows][i].ToString() + "'";
}
else
{
sSqlUpdate += ", [" + dt.Columns[i].ColumnName + "] = '" + dt.Rows[iRows][i].ToString() + "'";
}
}
}
//第一列是主键
sSqlUpdate += " Where [" + dt.Columns[0].ColumnName + "] = " + dt.Rows[iRows][0].ToString() + ";";
}
if (dt.Rows[iRows].RowState == DataRowState.Deleted)
{
//删除时取原始记录
sSqlDelete += "DELETE [" + dt.TableName + "] WHERE [" + dt.Columns[0].ColumnName + "] = " + dt.Rows[iRows][0, DataRowVersion.Original].ToString() + ";";
}
}
string strSql = sSqlUpdate + sSqlDelete;
if (strSql != "")
{
ExecuteNonQuery(strSql);
}
//重置所有行状态
dt.AcceptChanges();
return dt;
}