C# 禁止使用SqlDataAdapter仅更新某些列
我需要的是阻止某些列的更新。我需要这些字段,但我必须确保这些字段即使出现错误也不会被更新。 那么,是否有一个属性要设置为使这些字段仅处于某种只读状态? 更清楚地说,我的查询结果由四个字段组成: 追踪号码, 跟踪状态, 发货代码, 装运日期 前两个将被更新,但后两个不会被修改,我想确保即使出现错误,它们也不能被更改 有办法吗C# 禁止使用SqlDataAdapter仅更新某些列,c#,datatable,sqldataadapter,C#,Datatable,Sqldataadapter,我需要的是阻止某些列的更新。我需要这些字段,但我必须确保这些字段即使出现错误也不会被更新。 那么,是否有一个属性要设置为使这些字段仅处于某种只读状态? 更清楚地说,我的查询结果由四个字段组成: 追踪号码, 跟踪状态, 发货代码, 装运日期 前两个将被更新,但后两个不会被修改,我想确保即使出现错误,它们也不能被更改 有办法吗 public static Dictionary<string, object> GetTrackingShippingInfo(string carrierCo
public static Dictionary<string, object> GetTrackingShippingInfo(string carrierCode, DateTime date) {
SqlCommand cmd = new SqlCommand(String.Format(@"
( SELECT salesShipment.[Shipping Agent Ref_] as trackingNumber,
salesShipment.[Tracking Status] as trackingStatus,
salesShipment.[No_] as shippingCode,
salesShipment.[Posting Date] as shippingDate
FROM {0}.[{1}$Sales Shipment Header] as salesShipment
WHERE salesShipment.[Shipping Agent Code] = @carrierCode AND
salesShipment.[Posting Date] = @date AND
salesShipment.[Export to Carrier] > 0 AND
salesShipment.[Location Code] = @departureWarehouse
)
UNION ALL
( SELECT transferShipment.[Shipping Agent Ref_] as trackingNumber,
transferShipment.[Tracking Status] as trackingStatus,
transferShipment.[No_] as shippingCode,
transferShipment.[Posting Date] as shippingDate
FROM {0}.[{1}$Transfer Shipment Header] as transferShipment
WHERE transferShipment.[Shipping Agent Code] = @carrierCode AND
transferShipment.[Tipo Soggetto 1] = 1 AND
transferShipment.[Posting Date] = @date AND
transferShipment.[Export to Carrier] > 0 AND
transferShipment.[Transfer-from Code] = @departureWarehouse
)", navDbPrefix, navCompany));
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@carrierCode", carrierCode);
cmd.Parameters.AddWithValue("@date", String.Format("{0:yyyy-MM-dd}", date));
cmd.Parameters.AddWithValue("@departureWarehouse", "MP");
Dictionary<string, object> selectResult = DbHelper.ExecuteSelectForUpdateQuery(cmd);
return selectResult;
}
public static void UpdateTrackingShippingInfo(DataTable dataTable, SqlDataAdapter adapter) {
try
{
DbHelper.BeginTransaction();
DbHelper.ExecuteUpdate(dataTable, adapter);
DbHelper.CommmitTransaction();
}
catch (Exception e)
{
DbHelper.RollbackTransaction();
log.Error("Failed update query. Rollback executed");
log.Error(e.ToString());
}
}
public static void ExecuteUpdate(DataTable data, SqlDataAdapter adapter)
{
SqlCommandBuilder cmdBuilder;
cmdBuilder = new SqlCommandBuilder(adapter);
log.Debug(cmdBuilder.GetUpdateCommand().CommandText);
adapter.Update(data);
}
以下是microsoft示例,用于在SQLDataAdapter中为所有CRUD操作设置更新参数:
public static SqlDataAdapter CreateCustomerAdapter(
SqlConnection connection)
{
SqlDataAdapter adapter = new SqlDataAdapter();
// Create the SelectCommand.
SqlCommand command = new SqlCommand("SELECT * FROM Customers " +
"WHERE Country = @Country AND City = @City", connection);
// Add the parameters for the SelectCommand.
command.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
command.Parameters.Add("@City", SqlDbType.NVarChar, 15);
adapter.SelectCommand = command;
// Create the InsertCommand.
command = new SqlCommand(
"INSERT INTO Customers (CustomerID, CompanyName) " +
"VALUES (@CustomerID, @CompanyName)", connection);
// Add the parameters for the InsertCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
adapter.InsertCommand = command;
// Create the UpdateCommand.
command = new SqlCommand(
"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
"WHERE CustomerID = @oldCustomerID", connection);
// Add the parameters for the UpdateCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
SqlParameter parameter = command.Parameters.Add(
"@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original;
adapter.UpdateCommand = command;
// Create the DeleteCommand.
command = new SqlCommand(
"DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);
// Add the parameters for the DeleteCommand.
parameter = command.Parameters.Add(
"@CustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original;
adapter.DeleteCommand = command;
return adapter;
}
因此,对于您需要的更新,只需将所需的列与所需的参数一起应用。您能否向我们展示您的代码,以便我们了解您的操作方法\n您是如何创建commandText的?只是不要将它们添加到SqlDataAdapter中的命令文本中,您将找到包含update语句的SqlDataAdapter.UpdateCommand。。只包括你需要的update@SimonPrice抱歉,我刚刚更新了帖子。@HesamFaridmehr我没有修改update命令,我只是使用了与新值相同的dataTable和来自select查询的相同适配器