-2

我正在使用以下行将字符串存储在数据库中,但目前无法将其存储为单个字符串。每次在字符串中遇到逗号时,它都会在数据库表中创建一个新行。

cmd.Parameters.AddWithValue("@ImageURL", String.Join(",", img.ToArray()));

有什么办法呢?

这就是我定义表格的方式:

@ImageURL VARCHAR(max)

AS
BEGIN
  SET NOCOUNT ON added to prevent extra result sets from
  interfering with SELECT statements.
  SET NOCOUNT ON;


  Insert statements for procedure here
  insert into 
  DECLARE @String    VARCHAR(100) 

  SET @String =''            

  IF(LEN(@Images)>0)        
  BEGIN        
       DELETE FROM Business_images WHERE BusinessId=@BusinessId        
  END        

  WHILE LEN(@ImageURL) > 0              
  BEGIN           
    SET @String = LEFT(@ImageURL, ISNULL(NULLIF(CHARINDEX(',', @ImageURL) - 1, -1),  LEN(@ImageURL)))              
    SET @ImageURL = SUBSTRING(@ImageURL, ISNULL(NULLIF(CHARINDEX(',', @ImageURL), 0), LEN(@ImageURL)) + 1, LEN(@ImageURL)) 

    insert into Images(ImageURL) values (@String)
  END
END
4

1 回答 1

3
string specialString = "this is silly, string,,not so special''special";
using (SqlConnection conn = new SqlConnection(connectionString))
{
    using (SqlCommand command = new SqlCommand("Insert into t1 (col1) VALUES (@parame)"))
    {
        command.Connection = conn;
        command.Parameters.AddWithValue("@parame",specialString);
        command.ExecuteNonQuery();
    }
}

仅供参考:我能够插入到 sql server 中。在我的情况下col1是类型nvarchar

于 2012-06-20T16:14:13.107 回答