SQL批量更新不工作

SQL批量更新不工作

本文介绍了SQL批量更新不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在使用 SqlAdapter 来执行批量更新到SQL的第一次尝试。我想,不过按照这一页演示 adapter.Fill(DS,邮编codeTerritory); 一直给我的错误

必须声明标量变量\@通道code \

我试图加载列表数据集对象,然后使用<$的内容C $ C> .Update()在 SqlAdapter 来执行批量更新(约14K的记录)方法。

 私有静态字符串的UpdateCommand =UPDATE邮编codeTerritory设置通道code = @通道code,DrmTerrDesc = @DrmTerrDesc,IndDistrnId = @IndDistrnId,+
                                                国家code = @国code,邮编code = @邮编code,结束日期= @EndDate,EFFECTIVEDATE = @EffectiveDate,+
                                                LastUpdateId = @LastUpdateId,LastUpdateDate = @LastUpdateDate,错误codeS = @错误codeS,+
                                                状态= @Status+
                                          其中id = @Id;

    公共静态无效更新(名单&LT;邮编codeTerritory&GT;更新,字典&LT;邮编codeTerritory,串&GT; errorList)
    {
        使用(SqlConnection的连接=新的SqlConnection(CONNSTRING))
        {
            使用(SqlDataAdapter的适配器=新的SqlDataAdapter(更新命令,连接))
            {
                尝试
                {
                    DataTable中的dataTable = LoadData(更新);

                    SqlCommand的updateCmd = connection.CreateCommand();
                    updateCmd.CommandText =更新命令;
                    updateCmd.Parameters.Add(新的SqlParameter(@通道code,SqlDbType.Char,1,通道code));
                    updateCmd.Parameters.Add(新的SqlParameter(@ DrmTerrDesc,SqlDbType.Char,1,DrmTerrDesc));
                    updateCmd.Parameters.Add(新的SqlParameter(@ IndDistrnId,SqlDbType.Char,1,IndistrnId));
                    updateCmd.Parameters.Add(新的SqlParameter(@国code,SqlDbType.Char,1,国家code));
                    updateCmd.Parameters.Add(新的SqlParameter(@邮编code,SqlDbType.Char,1,邮编code));
                    updateCmd.Parameters.Add(新的SqlParameter(@结束日期,SqlDbType.Char,1,结束日期));
                    updateCmd.Parameters.Add(新的SqlParameter(@ EFFECTIVEDATE,SqlDbType.Char,1,EFFECTIVEDATE));
                    updateCmd.Parameters.Add(新的SqlParameter(@ LastUpdateId,SqlDbType.Char,1,LastUpdateId));
                    updateCmd.Parameters.Add(新的SqlParameter(@ LastUpdateDate,SqlDbType.Char,1,LastUpdateDate));
                    updateCmd.Parameters.Add(新的SqlParameter(@ ID,SqlDbType.Char,1,ID));
                    updateCmd.Parameters.Add(新的SqlParameter(@错误codeS,SqlDbType.Char,1,错误codeS));
                    updateCmd.Parameters.Add(新的SqlParameter(@状态,SqlDbType.Char,1,状态));
                    updateCmd.UpdatedRowSource = UpdateRowSource.None;

                    adapter.UpdateCommand = updateCmd;
                    adapter.AcceptChangesDuringUpdate = TRUE;

                    的DataSet ds为LoadDataSet(更新);

                    adapter.Fill(DS,邮编codeTerritory);

                    connection.Open();

                    adapter.Update(DS,邮编codeTerritory);

                    的Connection.close();
                }
                赶上(例外前)
                {
                    弦乐味精= ex.Message;
                }
            }
        }
    }

    私有静态数据集LoadDataSet(名单&LT;邮编codeTerritory&GT;拉链codeLIST)
    {
        的DataSet ds为新的DataSet();

        数据表数据= LoadData(邮政编码codeLIST);

        ds.Tables.Add(数据);

        返回DS;
    }

    私有静态数据表LoadData(名单&LT;邮编codeTerritory&GT;拉链codeLIST)
    {
        DataTable中的dataTable = InitializeStructure();

        的foreach(拉链codeLIST VAR拉链code)
        {
            DataRow的行= dataTable.NewRow();

            尝试
            {
                行[0] =拉链code.Channel code.Trim();
                行[1] =拉链code.DrmTerrDesc.Trim();
                行[2] =拉链code.IndDistrnId.Trim();
                行[3] =拉链code.State code.Trim();
                行[4] =拉链code.Zip code.Trim();
                行[5] =拉链code.EndDate.Date;
                行[6] =拉链code.EffectiveDate.Date;
                行[7] =拉链code.LastUpdateId;
                行[8] = DateTime.Now.Date;
                行[10] =拉链code.Error codeS;
                行[11] =拉链code.Status;
            }
            赶上(例外前)
            {

            }

            dataTable.Rows.Add(行);
        }

        返回的dataTable;
    }

    私有静态数据表InitializeStructure()
    {
        数据表DT =新的DataTable();

        dt.Columns.Add(通道code的typeof(字符串));
        dt.Columns.Add(DrmTerrDesc的typeof(字符串));
        dt.Columns.Add(IndDistrnId的typeof(字符串));
        dt.Columns.Add(国家code的typeof(字符串));
        dt.Columns.Add(ZIP code的typeof(字符串));
        dt.Columns.Add(结束日期的typeof(DateTime的));
        dt.Columns.Add(EFFECTIVEDATE的typeof(DateTime的));
        dt.Columns.Add(LastUpdateId的typeof(字符串));
        dt.Columns.Add(LastUpdateDate的typeof(DateTime的));
        dt.Columns.Add(ID的typeof(INT));
        dt.Columns.Add(错误codeS的typeof(字符串));
        dt.Columns.Add(状态,typeof运算(字符串));

        返回DT;
    }
 

现在的问题是这条线的位置:

 使用(SqlDataAdapter的适配器=新的SqlDataAdapter(更新命令,连接))
 

该SqlDataAdapter的构造需要的选择的命令,更新命令将无法正常工作。当您尝试执行填写方法,它失败,因为它没有 .SelectCommand 参数适用。

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.

这篇关于SQL批量更新不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 05:28