Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have value like following

strNm="'MyData','MySinger'"


while inserting or updating it give me error

my query is
insert into Table values('" + strNm + "')
Posted

Don't do it like that!
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. Your problem will magically disappear at the same time!
And always list your column names when inserting records!
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("INSERT INTO myTable (myColumn1) VALUES (@C1)", con))
        {
        com.Parameters.AddWithValue("@C1", strNm);
        com.ExecuteNonQuery();
        }
    }

Using con As New SqlConnection(strConnect)
    con.Open()
    Using com As New SqlCommand("INSERT INTO myTable (myColumn1) VALUES (@C1)", con)
        com.Parameters.AddWithValue("@C1", strNm)
        com.ExecuteNonQuery()
    End Using
End Using
 
Share this answer
 
Comments
hareshdgr8 13-Feb-13 4:25am    
what about for access sir.... OriginalGriff
OriginalGriff 13-Feb-13 4:41am    
Exactly the same, but you use ODBC Connection and Command objects, just as you have before. For MySql you would use MySql Command and Connection objects.
Hi ,

Check the following Script,

SQL
-- If Object Exists then Drop
IF OBJECT_ID('split') IS NOT NULL DROP FUNCTION dbo.split
IF OBJECT_ID('tempdb..#Test') IS NOT NULL DROP TABLE #Test
GO
-- Create Split Function
CREATE FUNCTION [dbo].[split](
@delimited NVARCHAR(MAX),
@delimiter NVARCHAR(100)
) RETURNS @t TABLE (id INT IDENTITY(1,1), val NVARCHAR(MAX))
AS
BEGIN
DECLARE @xml XML
    SET @xml = N'<t>' + REPLACE(@delimited,@delimiter,'</t><t>') + '</t>'

    INSERT INTO @t(val)
    SELECT  r.value('.','varchar(MAX)') as item
    FROM  @xml.nodes('/t') as records(r)
RETURN
END
GO
-- Create Temp Table 
CREATE TABLE #Test(Name VARCHAR(100))

DECLARE @DataString VARCHAR(1000)='MyData,MySinger'

INSERT INTO #Test(Name)
SELECT Val FROM dbo.split(@DataString, ',')

-- Actual Output
SELECT Name FROM #Test


Regards,
GVPrabu
 
Share this answer
 
Hi,

try this query
strNm="(SELECT 'MyData' UNION ALL SELECT 'MySinger')"
SQL
insert into Table(column1) + strNm +   

you are using single quotes twice. one at value assignment and other at query. remove the single quote at query, it will work.

hope it helps.
 
Share this answer
 
v2
Comments
hareshdgr8 13-Feb-13 4:27am    
but there is only single columns where the value has been inserted
Karthik Harve 13-Feb-13 4:34am    
try with updated answer.
Dim abc As String = "'Abc','CEEE',"

sqlselect.CommandText = "insert into table1(Name1) values (@a)"
sqlselect.Parameters.AddWithValue("@a", abc)
sqlselect.ExecuteNonQuery
 
Share this answer
 
SQL
DECLARE @strNm AS NVARCHAR(MAX)

SET @strNm='''MyData'',''MySinger'''

DECLARE @SQLString AS NVARCHAR(MAX)
DECLARE @ParmDefinition AS NVARCHAR(MAX)

SET @SQLString =
     N'insert into temptbl(name,name1) values(' + @strNm + ')'


EXECUTE(@SQLString)


Please replace your table name and field name in above query
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900