2012-06-20 87 views
-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 [email protected]   
    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 
+0

什么是'img'的类型?查询或存储过程的源代码是什么? –

+1

_“创建一个新行”_ O_o什么是您的命令查询? – Otiel

+0

如果你使用.NET 4.0,您可以用'的string.join( “” IMG)'(如果'img'类型为IEnumerable的'') –

回答

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(); 
    } 
} 

FYI:我是能够插入到SQL Server。在我的情况col1的类型是nvarchar

+0

这正是我所做的!任何其他想法? –

+3

@MayankPahuja:不,这不完全是你所做的,s_nair使用''CommandType''(http://msdn.microsoft.com/en-us/library/system.data.commandtype。 aspx)'Text',而你(很可能)使用了'StoredProcedure'。 –

+0

在表上定义了一个插入触发器,它将文本分隔在逗号位置? –

相关问题