winforms读取Excel文件,并将其数据插入到数据库的问题

wxc137 2009-06-13 03:18:57
最近开发一个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个参数,前四个参数重复了)
...全文
66 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wxc137 2009-06-13
  • 打赏
  • 举报
回复
解决了.谢谢了.楼上的,分数全送给你了.
wxg22526451 2009-06-13
  • 打赏
  • 举报
回复
               com.Parameters.Clear();//先清空参数     
foreach (SqlParameter parame in param)
{
com.Parameters.Add(parame);
}
wjq 2009-06-13
  • 打赏
  • 举报
回复
sp_ADDCustemor
你的存储过程里,加上if((select (*) from xxx where 主键=@Cus_id)=0)

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧