问题
I need to know what the size parameter should be for a DateTime value using the following syntax:
adapter.InsertCommand.Parameters.Add("@deliveryDateAndTime", SqlDbType.DateTime,10,"deliveryDateAndTime");
I cannot use the following syntax because I'm batching the updates in a datatable.
adapter.InsertCommand.Parameters.Add("@deliveryDateAndTime", SqlDbType.DateTime);
adapter.InsertCommand.Parameters["@deliveryDateAndTime"] = *variable*.value;
Below is the code I need to use (abbreviated for clarity). Note that inside the adapter.InsertCommand.Parameters.Add("@deliveryDateAndTime", SqlDbType.DateTime,10,"deliveryDateAndTime");
statement, the first parameter refers to a corresponding SQL parameter and the 4th parameter corresponds to the data for that value inside the datatable.
DataTable dt = new DataTable();
foreach (FuelDelivery fd in fuelDelivery.FuelDeliveries)
{
DataRow dr = dt.NewRow();
dr["deliveryDateAndTime"] = fd.DeliveryDateAndTime;
dt.Rows.Add(dr);
}
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HHSqlConnection"].ToString()))
{
// Create a SqlDataAdapter.
SqlDataAdapter adapter = new SqlDataAdapter();
// Set the INSERT command and parameter.
adapter.InsertCommand.Parameters.Add("@deliveryDateAndTime", SqlDbType.DateTime,10,"deliveryDateAndTime");
adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None;
// Set the batch size. Zero represents the maximum amt of rows to update at once.
adapter.UpdateBatchSize = 0;
// Execute the update.
int rowsUpdated = adapter.Update(dt);
}
Based on this overload for the adapter.InsertCommand.Parameters.Add
method, how can I set the date accordingly?
回答1:
A datetime is 8 bytes according to the easily searchable documentation.
Note that you could also use your preferred overload and set the source column separately:
adapter.InsertCommand.Parameters.Add("@deliveryDateAndTime", SqlDbType.DateTime)
.SourceColumn = "deliveryDateAndTime";
来源:https://stackoverflow.com/questions/21467825/insertcommand-parameters-add-size-parameter-for-datetime