我正在尝试上传多个文件。但是它已经声明错误@DocData已经声明了。变量名在查询批处理或存储过程中必须是唯一的 [英] I am trying to upload multiple files .but it gives error @DocDatahas already been declared. Variable names must be unique within a query batch or stored procedure

查看:80
本文介绍了我正在尝试上传多个文件。但是它已经声明错误@DocData已经声明了。变量名在查询批处理或存储过程中必须是唯一的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (gvAttachments.Rows.Count > 0)
          {
              string[] Records = Session["FileDetail"].ToString().Split('#');
              for (int i = 0; i <Records.Length; i++)
              {
                  string[] arrRecords = Records[i].Split(',');
                  string[] fileExtension = arrRecords[0].Split('.');
                  string documentType = string.Empty;
                  switch (fileExtension[1])
                  {
                      case : documentType = "application/pdf";
                          break;
                      case : documentType = "application/vnd.ms-excel";
                          break;
                      case : documentType = "application/vnd.ms-excel";
                          break;
                      case : documentType = "application/vnd.ms-word";
                          break;
                      case "docx": documentType = "application/vnd.ms-word";
                          break;
                      case "gif": documentType = "image/gif";
                          break;
                      case "png": documentType = "image/png";
                          break;
                      case "jpg": documentType = "image/jpg";
                          break;
                  }
                  string size = arrRecords[2];
                  int fileSize = Convert.ToInt32(size);
                  byte[] documentbinary = new byte[fileSize];
                  fuUpload.PostedFile.InputStream.Read(documentbinary, 0, fileSize);
                  string file = arrRecords[0].ToString();
                  string format = documentType.ToString();
                  cmd.CommandText = "Insert into Attachment(File_Name,File_Format,File_Content,User_Notice_Mapping_ID) values('" + file + "','" + format + "',@DocData," + userNoticeID + ")";
                  cmd.CommandType = CommandType.Text;
                  cmd.Connection = con;
                  cmd.Parameters.Add("@DocData", SqlDbType.VarBinary, fileSize);
                  cmd.Parameters["@DocData"].Value = documentbinary;
                  cmd.ExecuteNonQuery();
                  String msg = "Your message has been Sent Successfully";
                  ShowAlert(msg, true);
              }
          }

推荐答案

1.您的问题是您使用相同的 SqlCommand 的所有项目的对象,因此命令对象在第一次迭代后已经有参数。



2.解决方案是在中为创建 SqlCommand 对象:





1.Your problem is that your are using the same SqlCommand object for all items of your for, so the command object already have the parameter after its first iteration.

2.The solution is to create the SqlCommand object local inside the for:


for (int i = 0; i <Records.Length; i++)
              {
                  string[] arrRecords = Records[i].Split(',');
                  string[] fileExtension = arrRecords[0].Split('.');
                  string documentType = string.Empty;
                  switch (fileExtension[1])
                  {
                      case : documentType = "application/pdf";
                          break;
                      case : documentType = "application/vnd.ms-excel";
                          break;
                      case : documentType = "application/vnd.ms-excel";
                          break;
                      case : documentType = "application/vnd.ms-word";
                          break;
                      case "docx": documentType = "application/vnd.ms-word";
                          break;
                      case "gif": documentType = "image/gif";
                          break;
                      case "png": documentType = "image/png";
                          break;
                      case "jpg": documentType = "image/jpg";
                          break;
                  }
                  string size = arrRecords[2];
                  int fileSize = Convert.ToInt32(size);
                  byte[] documentbinary = new byte[fileSize];
                  fuUpload.PostedFile.InputStream.Read(documentbinary, 0, fileSize);
                  string file = arrRecords[0].ToString();
                  string format = documentType.ToString();
                  //
                  cmd = new SqlCommand(); //Must be a new SqlCommand for each item! 
                  // 
                  cmd.CommandText = "Insert into Attachment(File_Name,File_Format,File_Content,User_Notice_Mapping_ID) values('" + file + "','" + format + "',@DocData," + userNoticeID + ")";
                  cmd.CommandType = CommandType.Text;
                  cmd.Connection = con;
                  cmd.Parameters.Add("@DocData", SqlDbType.VarBinary, fileSize);
                  cmd.Parameters["@DocData"].Value = documentbinary;
                  cmd.ExecuteNonQuery();
                  String msg = "Your message has been Sent Successfully";
                  ShowAlert(msg, true);
              }


这篇关于我正在尝试上传多个文件。但是它已经声明错误@DocData已经声明了。变量名在查询批处理或存储过程中必须是唯一的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆