构建插入语句错误已声明Https

时间:2015-02-07 12:13:52

标签: c# asp.net-mvc-5 sql-insert

我有一个从UI获取多个图像并将它们存储在缓存中的方法,当它完成时调用一个方法然后引用缓存将图像拉出然后将它们传递给数据库,此时此insert语句当前正在执行一个,因此对于一个用户来说,它可能是数据库的7次点击。

我想要实现的是这样的,在SQL中你会做的(一次插入数据库的多个项目)

insert into table1 (First,Last) 
values 
     ('Fred','Smith'),
     ('John','Smith'),
     ('Michael','Smith'),
     ('Robert','Smith');

我的代码目前看起来像这样

public void SaveImages(List<Images> Images, Int64 userId)
{
  var extension = Images.Aggregate(string.Empty, (current, item) => current + string.Format("({0},{1},{2},{3},{4},{5}),", userId, item.ImageName, item.ImageUrl, 1, DateTime.Now, null));
}

一旦完成扩展就像这样

(1253,xkvkvy4ldmfbhlmftqsc,https://res.cloudinary.com/dncu6pqpm/image/upload/v1423309462/rfefef.jpg,1,07/02/2015 11:45:29,),
(1253,pa0niomwvyzl47qjlxna,https://res.cloudinary.com/dncu6pqpm/image/upload/v1423309471/iuytre.jpg,1,07/02/2015 11:45:29,),
(1253,mymvektwddgco9nvbaap,https://res.cloudinary.com/dncu6pqpm/image/upload/v1423309479/sdfgh.jpg,1,07/02/2015 11:45:29,),
(1253,fxlftcx9jgrs9c7sjnv4,https://res.cloudinary.com/dncu6pqpm/image/upload/v1423309489/98iuiu.jpg,1,07/02/2015 11:45:29,),

我调用insert语句并传入扩展名

var query = @"
           INSERT INTO [User].Images
                  (UserId, FileName, ImageUrl, Active, CreatedDate, DeletedDate)

            VALUES
                  " + extension;

sqlCon.Query(query);

但由于以下原因导致失败

The label 'https' has already been declared. Label names must be unique within a query batch or stored procedure.

我已经搜索了这个错误,但我没有收到任何好的解决方案,因此我在这里问。

1 个答案:

答案 0 :(得分:-1)

尝试将url(和其他字符串值)放在引号之间:

(1253,'xkvkvy4ldmfbhlmftqsc','https://res.cloudinary.com/dncu6pqpm/image/upload/v1423309462/rfefef.jpg',1,07/02/2015 11:45:29,)