DataSet参数构建
时间:2011-05-27 来源:xcanel
#region Method for InsertSqLBuilder //构建InsertSqLBuilder
/// <summary>
/// 构建InsertSqLBuilder
/// </summary>
/// <param name="dataTable">数据表</param>
/// <param name="dataConnParm">数据连接参数</param>
/// <returns></returns>
public string InsertSqLBuilder(DataTable dataTable, string dataConnParm)
{
string insertCommandSql = "Insert into " + dataTable.TableName + " ("; //Exp: commandSql="UPDATE Customers SET Name = @Name WHERE CustomerId = @CustomerId";
string insertCommandValue = ") values (";
foreach (DataColumn column in dataTable.Columns)
{
insertCommandSql = insertCommandSql + column.ColumnName + ",";
insertCommandValue = insertCommandValue + dataConnParm + column.ColumnName + ",";
}
insertCommandSql = insertCommandSql.Substring(0, insertCommandSql.Length - 1);
insertCommandValue = insertCommandValue.Substring(0, insertCommandValue.Length - 1) + ")";
insertCommandSql = insertCommandSql + insertCommandValue;
return insertCommandSql;
}
#endregion
#region Method for UpdateSqLBuilder //构建UpdateSqLBuilder
/// <summary>
/// 构建UpdateSqLBuilder
/// </summary>
/// <param name="dataTable">数据表</param>
/// <param name="dataConnParm">数据连接参数</param>
/// <returns></returns>
public string UpdateSqLBuilder(DataTable dataTable, string dataConnParm)
{
//需要获取主键列 根据主键列更新 update tablename set column=:column where
string updateCommandSql = "Update " + dataTable.TableName + " set "; //Exp: commandSql="UPDATE Customers SET Name = @Name WHERE CustomerId = @CustomerId";
string updateCommandUnique = " where ";
//判断主键列
foreach (DataColumn column in dataTable.Columns)
{
if (column.Unique)
updateCommandUnique = updateCommandUnique + column.ColumnName + "=" + dataConnParm + column.ColumnName + ",";
else
updateCommandSql = updateCommandSql + column.ColumnName + "=" + dataConnParm + column.ColumnName + ",";
}
updateCommandSql = updateCommandSql.Substring(0, updateCommandSql.Length - 1);
updateCommandUnique = updateCommandUnique.Substring(0, updateCommandUnique.Length - 1) + ")";
updateCommandSql = updateCommandSql + updateCommandUnique;
return updateCommandSql;
}
#endregion
#region Method for DeleteSqLBuilder //构建DeleteSqLBuilder
/// <summary>
/// 构建DeleteSqLBuilder
/// </summary>
/// <param name="dataTable">数据表</param>
/// <param name="dataConnParm">数据连接参数</param>
/// <returns></returns>
public string DeleteSqLBuilder(DataTable dataTable, string dataConnParm)
{
//需要获取主键列 根据主键列删除 delete
string deleteCommandSql = "Delete " + dataTable.TableName + " where "; //Exp: commandSql="UPDATE Customers SET Name = @Name WHERE CustomerId = @CustomerId";
foreach (DataColumn column in dataTable.Columns)
{
if (column.Unique)
deleteCommandSql = deleteCommandSql + column.ColumnName + "=" + dataConnParm + column.ColumnName + ",";
}
deleteCommandSql = deleteCommandSql.Substring(0, deleteCommandSql.Length - 1);
return deleteCommandSql;
}
#endregion