简体   繁体   中英

SQL bulk updates not working

This is my first attempt at using a SqlAdapter to perform a batch update to a SQL. I'm trying to follow the demo on this page however the line adapter.Fill(ds, "ZipCodeTerritory"); keeps giving me the error

Must declare the scalar variable \\"@ChannelCode\\"

I'm trying to load the contents of a List into a DataSet object and then use the .Update() method on the SqlAdapter to perform a batch update (around 14k records).

    private static string updateCommand = "UPDATE ZipCodeTerritory SET ChannelCode = @ChannelCode, DrmTerrDesc = @DrmTerrDesc, IndDistrnId = @IndDistrnId," +
                                                "StateCode = @StateCode, ZipCode = @ZipCode, EndDate = @EndDate, EffectiveDate = @EffectiveDate," +
                                                "LastUpdateId = @LastUpdateId, LastUpdateDate = @LastUpdateDate, ErrorCodes = @ErrorCodes," + 
                                                "Status = @Status " +
                                          "WHERE Id = @Id";

    public static void Update(List<ZipCodeTerritory> updates, Dictionary<ZipCodeTerritory, string> errorList)
    {
        using (SqlConnection connection = new SqlConnection(connString))
        {
            using (SqlDataAdapter adapter = new SqlDataAdapter(updateCommand, connection))
            {
                try
                {
                    DataTable dataTable = LoadData(updates);

                    SqlCommand updateCmd = connection.CreateCommand();
                    updateCmd.CommandText = updateCommand;
                    updateCmd.Parameters.Add(new SqlParameter("@ChannelCode", SqlDbType.Char, 1, "ChannelCode"));
                    updateCmd.Parameters.Add(new SqlParameter("@DrmTerrDesc", SqlDbType.Char, 1, "DrmTerrDesc"));
                    updateCmd.Parameters.Add(new SqlParameter("@IndDistrnId", SqlDbType.Char, 1, "IndistrnId"));
                    updateCmd.Parameters.Add(new SqlParameter("@StateCode", SqlDbType.Char, 1, "StateCode"));
                    updateCmd.Parameters.Add(new SqlParameter("@ZipCode", SqlDbType.Char, 1, "ZipCode"));
                    updateCmd.Parameters.Add(new SqlParameter("@EndDate", SqlDbType.Char, 1, "EndDate"));
                    updateCmd.Parameters.Add(new SqlParameter("@EffectiveDate", SqlDbType.Char, 1, "EffectiveDate"));
                    updateCmd.Parameters.Add(new SqlParameter("@LastUpdateId", SqlDbType.Char, 1, "LastUpdateId"));
                    updateCmd.Parameters.Add(new SqlParameter("@LastUpdateDate", SqlDbType.Char, 1, "LastUpdateDate"));
                    updateCmd.Parameters.Add(new SqlParameter("@Id", SqlDbType.Char, 1, "Id"));
                    updateCmd.Parameters.Add(new SqlParameter("@ErrorCodes", SqlDbType.Char, 1, "ErrorCodes"));
                    updateCmd.Parameters.Add(new SqlParameter("@Status", SqlDbType.Char, 1, "Status"));
                    updateCmd.UpdatedRowSource = UpdateRowSource.None;

                    adapter.UpdateCommand = updateCmd;
                    adapter.AcceptChangesDuringUpdate = true;

                    DataSet ds = LoadDataSet(updates);

                    adapter.Fill(ds, "ZipCodeTerritory");

                    connection.Open();

                    adapter.Update(ds, "ZipCodeTerritory");

                    connection.Close();
                }
                catch (Exception ex)
                {
                    string msg = ex.Message;
                }                    
            }
        }
    }

    private static DataSet LoadDataSet(List<ZipCodeTerritory> zipcodeList)
    {
        DataSet ds = new DataSet();

        DataTable data = LoadData(zipcodeList);

        ds.Tables.Add(data);

        return ds;
    }

    private static DataTable LoadData(List<ZipCodeTerritory>zipCodeList)
    {
        DataTable dataTable = InitializeStructure();

        foreach (var zipcode in zipCodeList)
        {
            DataRow row = dataTable.NewRow();

            try
            {
                row[0] = zipcode.ChannelCode.Trim();
                row[1] = zipcode.DrmTerrDesc.Trim();
                row[2] = zipcode.IndDistrnId.Trim();
                row[3] = zipcode.StateCode.Trim();
                row[4] = zipcode.ZipCode.Trim();
                row[5] = zipcode.EndDate.Date;
                row[6] = zipcode.EffectiveDate.Date;
                row[7] = zipcode.LastUpdateId;
                row[8] = DateTime.Now.Date;
                row[10] = zipcode.ErrorCodes;
                row[11] = zipcode.Status;
            }
            catch (Exception ex)
            {

            }

            dataTable.Rows.Add(row);
        }

        return dataTable;
    }

    private static DataTable InitializeStructure()
    {
        DataTable dt = new DataTable();

        dt.Columns.Add("ChannelCode", typeof (string));
        dt.Columns.Add("DrmTerrDesc", typeof (string));
        dt.Columns.Add("IndDistrnId", typeof (string));
        dt.Columns.Add("StateCode", typeof (string));
        dt.Columns.Add("ZipCode", typeof (string));
        dt.Columns.Add("EndDate", typeof (DateTime));
        dt.Columns.Add("EffectiveDate", typeof (DateTime));
        dt.Columns.Add("LastUpdateId", typeof (string));
        dt.Columns.Add("LastUpdateDate", typeof (DateTime));
        dt.Columns.Add("Id", typeof (int));
        dt.Columns.Add("ErrorCodes", typeof (string));
        dt.Columns.Add("Status", typeof (string));

        return dt;
    }

The problem is this line here:

        using (SqlDataAdapter adapter = new SqlDataAdapter(updateCommand, connection))

The SqlDataAdapter constructor needs a Select command, an Update command will not work. When you try to execute the Fill method it fails because it has no .SelectCommand parameters to apply.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM