InsertCommand.Parameters.Add size parameter for datetime

百般思念 提交于 2019-12-11 00:46:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!