最近开发一个winforms程序,每天要将某部门的数据(excel格式),导入SQL数据库中(以前的数据不能够覆盖),在将数据写入数据库的时候出现一个问题:每次只能写入每一条数据,写入第二条数据的时候提示:存储过程传入的参数太多.代码如下:
for (int i = 1; i < MyTable.Rows.Count; i++)
{
try
{ //MessageBox.Show(""+MyTable .Rows [i][0].ToString ()+""); com.Connection = access.GetConnection();
com.CommandText = "sp_ADDCustemor";
com.CommandType = CommandType.StoredProcedure;
SqlParameter[] param ={
new SqlParameter("@Cus_id", SqlDbType.VarChar),
new SqlParameter("@Cus_name", SqlDbType.VarChar),
new SqlParameter("@phone", SqlDbType.VarChar),
new SqlParameter("@mobile", SqlDbType.VarChar),
new SqlParameter("@realname", SqlDbType.VarChar),
new SqlParameter("@paytype", SqlDbType.VarChar),
new SqlParameter("@grade", SqlDbType.VarChar),
new SqlParameter("@dalegate", SqlDbType.VarChar),
new SqlParameter("@email", SqlDbType.VarChar),
new SqlParameter("@fax", SqlDbType.VarChar),
new SqlParameter("@cushttp", SqlDbType.VarChar)
};
param[0].Value = MyTable.Rows[i][0].ToString();
param[1].Value = MyTable.Rows[i][1].ToString();
param[2].Value = MyTable.Rows[i][2].ToString();
param[3].Value = MyTable.Rows[i][3].ToString();
param[4].Value = MyTable.Rows[i][4].ToString();
param[5].Value = MyTable.Rows[i][5].ToString();
param[6].Value = MyTable.Rows[i][6].ToString();
param[7].Value = MyTable.Rows[i][7].ToString();
param[8].Value = MyTable.Rows[i][8].ToString();
param[9].Value = MyTable.Rows[i][9].ToString();
param[10].Value = MyTable.Rows[i][10].ToString();
foreach (SqlParameter parame in param)
{
com.Parameters.Add(parame);
}
access.GetConnection().Open();
com.ExecuteNonQuery();
}
//catch { }
finally
{
access.GetConnection().Close();
access.Dispose();
}
请高手指点(在调试的时候发现传了15个参数,前四个参数重复了)
for (int i = 1; i < MyTable.Rows.Count; i++)
{
try
{ //MessageBox.Show(""+MyTable .Rows [i][0].ToString ()+""); com.Connection = access.GetConnection();
com.CommandText = "sp_ADDCustemor";
com.CommandType = CommandType.StoredProcedure;
SqlParameter[] param ={
new SqlParameter("@Cus_id", SqlDbType.VarChar),
new SqlParameter("@Cus_name", SqlDbType.VarChar),
new SqlParameter("@phone", SqlDbType.VarChar),
new SqlParameter("@mobile", SqlDbType.VarChar),
new SqlParameter("@realname", SqlDbType.VarChar),
new SqlParameter("@paytype", SqlDbType.VarChar),
new SqlParameter("@grade", SqlDbType.VarChar),
new SqlParameter("@dalegate", SqlDbType.VarChar),
new SqlParameter("@email", SqlDbType.VarChar),
new SqlParameter("@fax", SqlDbType.VarChar),
new SqlParameter("@cushttp", SqlDbType.VarChar)
};
param[0].Value = MyTable.Rows[i][0].ToString();
param[1].Value = MyTable.Rows[i][1].ToString();
param[2].Value = MyTable.Rows[i][2].ToString();
param[3].Value = MyTable.Rows[i][3].ToString();
param[4].Value = MyTable.Rows[i][4].ToString();
param[5].Value = MyTable.Rows[i][5].ToString();
param[6].Value = MyTable.Rows[i][6].ToString();
param[7].Value = MyTable.Rows[i][7].ToString();
param[8].Value = MyTable.Rows[i][8].ToString();
param[9].Value = MyTable.Rows[i][9].ToString();
param[10].Value = MyTable.Rows[i][10].ToString();
foreach (SqlParameter parame in param)
{
com.Parameters.Add(parame);
}
access.GetConnection().Open();
com.ExecuteNonQuery();
}
//catch { }
finally
{
access.GetConnection().Close();
access.Dispose();
}
请高手指点(在调试的时候发现传了15个参数,前四个参数重复了)
解决方案 »
- C#串口接收程序
- datagridview自动编码问题
- 如果将RichEdit包含格式的内容(RTF)保存到数据库中?(分下次补上,不好意思,没分了。)
- 用SQLDMO备份SQL2005数据库碰到的几个问题
- 用c# 操作WORD文档,使用Microsoft Word 11.0 Object Library,请问能在word2000上运行吗
- 问问makefile文件的使用
- 各位专家,请问word的bookmarks没有get_item方法怎么解决的啊?谢谢
- c#.net里static的作用是什么
- 能不能把命令行跟窗口结合到一起啊
- namespace是什么东西?
- c# 表格如何制作
- 怎样把按条件按查询出的数据绑定到另外一个窗口的listbox中?
你的存储过程里,加上if((select (*) from xxx where 主键=@Cus_id)=0)
foreach (SqlParameter parame in param)
{
com.Parameters.Add(parame);
}