随机生成@param名称以插入sql查询

Vis*_*pse 0 c# sql

您好我正在编写ac#console应用程序以将数据从一个数据库迁移到另一个数据库.数据库的行中有图像,我需要使用@param将这些图像包含到SQL语句中,并使用ADO.NET执行这些语句.

我需要生成随机字符串,可以用作@param的名称,我正在做这样的事情.

    While(blah blah)
    {
    if (bVisitorPhoto != null)
            {
                string Picture = RandomString();
                SqlParameter picparameter = new SqlParameter();
                picparameter.SqlDbType = SqlDbType.Image;
                picparameter.ParameterName = Picture;
                picparameter.Value = bVisitorPhoto;
                command.Parameters.Add(picparameter);
                VisitorPhoto = "@"+Picture;
             }
         string query = "insert into table Values (Picture, "PersonName");       
    }

 public static string RandomString()
    {
        Random rand = new Random();
        string Random_String = "str" + rand.Next(9999999).ToString();
        return Random_String;
    }
Run Code Online (Sandbox Code Playgroud)

问题是这个错误

"SqlClient.SqlException"变量名'@ str5440049'已经声明.



怎么会发生这种情况?但是,一旦我按调试模式运行程序,按F11就像一百万次我面对这个错误

Ted*_*Ted 5

为什么使用随机而不仅仅是列名+计数器值?

int counter=0;
While(blah blah)
{
  if (bVisitorPhoto != null)
  {
    string Picture = photo+counter.ToString();
    SqlParameter picparameter = new SqlParameter();
    picparameter.SqlDbType = SqlDbType.Image;
    picparameter.ParameterName = Picture;
    picparameter.Value = bVisitorPhoto;
    command.Parameters.Add(picparameter);
    VisitorPhoto = "@"+Picture;
  }
  string query = "insert into table Values (Picture, "PersonName");       
  counter++;
}
Run Code Online (Sandbox Code Playgroud)