Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SQL批量更新不工作_C#_Sql_.net - Fatal编程技术网

C# SQL批量更新不工作

C# SQL批量更新不工作,c#,sql,.net,C#,Sql,.net,这是我第一次尝试使用SqlAdapter对SQL执行批更新。我试图跟随演示,但是行adapter.Fill(ds,“ZipCodeTerritory”)一直告诉我错误 必须声明标量变量\“@ChannelCode\” 我试图将列表的内容加载到数据集对象中,然后使用SqlAdapter上的.Update()方法执行批更新(大约14k条记录) private static string updateCommand=“UPDATE zipcodertritory SET ChannelCode=@Ch

这是我第一次尝试使用
SqlAdapter
对SQL执行批更新。我试图跟随演示,但是行
adapter.Fill(ds,“ZipCodeTerritory”)一直告诉我错误

必须声明标量变量\“@ChannelCode\”

我试图将
列表的内容加载到
数据集
对象中,然后使用
SqlAdapter
上的
.Update()
方法执行批更新(大约14k条记录)

private static string updateCommand=“UPDATE zipcodertritory SET ChannelCode=@ChannelCode,DrmTerrDesc=@DrmTerrDesc,inddistrind=@inddistrind,”+
StateCode=@StateCode,ZipCode=@ZipCode,EndDate=@EndDate,EffectiveDate=@EffectiveDate+
“LastUpdateId=@LastUpdateId,LastUpdateDate=@LastUpdateDate,ErrorCodes=@ErrorCodes,”+
“状态=@Status”+
“其中Id=@Id”;
公共静态无效更新(列表更新、字典错误列表)
{
使用(SqlConnection连接=新的SqlConnection(connString))
{
使用(SqlDataAdapter=newsqldataadapter(updateCommand,connection))
{
尝试
{
DataTable=加载数据(更新);
SqlCommand updateCmd=connection.CreateCommand();
updateCmd.CommandText=updateCommand;
Add(新的SqlParameter(“@ChannelCode”,SqlDbType.Char,1,“ChannelCode”);
updateCmd.Parameters.Add(新的SqlParameter(“@DrmTerrDesc”,SqlDbType.Char,1,“DrmTerrDesc”);
updateCmd.Parameters.Add(新的SqlParameter(“@inddistrind”,SqlDbType.Char,1,“IndistrnId”);
Add(新的SqlParameter(“@StateCode”,SqlDbType.Char,1,“StateCode”);
Add(新的SqlParameter(“@ZipCode”,SqlDbType.Char,1,“ZipCode”);
Add(新的SqlParameter(“@EndDate”,SqlDbType.Char,1,“EndDate”);
updateCmd.Parameters.Add(新的SqlParameter(“@EffectiveDate”,SqlDbType.Char,1,“EffectiveDate”);
Add(新的SqlParameter(“@LastUpdateId”,SqlDbType.Char,1,“LastUpdateId”);
Add(新的SqlParameter(“@LastUpdateDate”,SqlDbType.Char,1,“LastUpdateDate”);
Add(新的SqlParameter(“@Id”,SqlDbType.Char,1,“Id”);
updateCmd.Parameters.Add(新的SqlParameter(“@ErrorCodes”,SqlDbType.Char,1,“ErrorCodes”);
updateCmd.Parameters.Add(新的SqlParameter(“@Status”,SqlDbType.Char,1,“Status”);
updateCmd.UpdatedRowSource=UpdateRowSource.None;
adapter.UpdateCommand=updateCmd;
adapter.AcceptChangesDuringUpdate=true;
数据集ds=加载数据集(更新);
适配器填充(ds,“ZipCodeTerritory”);
connection.Open();
适配器更新(ds,“ZipCodeTerritory”);
connection.Close();
}
捕获(例外情况除外)
{
字符串msg=例如消息;
}                    
}
}
}
私有静态数据集LoadDataSet(列表zipcodeList)
{
数据集ds=新数据集();
DataTable数据=加载数据(zipcodeList);
ds.Tables.Add(数据);
返回ds;
}
专用静态数据表LoadData(ListzipCodeList)
{
DataTable DataTable=InitializeStructure();
foreach(zipCodeList中的变量zipcode)
{
DataRow行=dataTable.NewRow();
尝试
{
行[0]=zipcode.ChannelCode.Trim();
行[1]=zipcode.DrmTerrDesc.Trim();
行[2]=zipcode.indDistrind.Trim();
行[3]=zipcode.StateCode.Trim();
行[4]=zipcode.zipcode.Trim();
行[5]=zipcode.EndDate.Date;
第[6]行=zipcode.EffectiveDate.Date;
行[7]=zipcode.LastUpdateId;
行[8]=DateTime.Now.Date;
行[10]=zipcode.ErrorCodes;
行[11]=zipcode.Status;
}
捕获(例外情况除外)
{
}
dataTable.Rows.Add(行);
}
返回数据表;
}
私有静态数据表初始化结构()
{
DataTable dt=新的DataTable();
dt.Columns.Add(“ChannelCode”,typeof(string));
添加(“DrmTerrDesc”,typeof(string));
添加(“inddistrind”,typeof(string));
添加(“状态代码”,类型(字符串));
添加(“ZipCode”,typeof(string));
添加(“EndDate”,typeof(DateTime));
添加(“生效日期”,类型(日期时间));
Add(“LastUpdateId”,typeof(string));
添加(“LastUpdateDate”,typeof(DateTime));
添加(“Id”,typeof(int));
添加(“错误代码”,类型(字符串));
添加(“状态”,类型(字符串));
返回dt;
}

这里的问题是这一行:

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

SqlDataAdapter构造函数需要Select命令,Update命令将不起作用。尝试执行
Fill
方法时失败,因为它没有要应用的
.SelectCommand
参数。

您尚未为
适配器定义
SelectCommand
(您正在构造函数中使用
updateCommand
)。尝试创建命令,然后使用
adapter.Fill
@Romoku-(只是好奇)
        using (SqlDataAdapter adapter = new SqlDataAdapter(updateCommand, connection))