|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
insert string to SQL Server 2000Hi
I try to write a C# program which will get an input string and I need to insert to the SQL database. I set the column type as Text. and when I insert the string variable, for exmaple, "Now", it gave the error " The name 'Now' is not permitted in this context. Only constants, expressions, or variables allowed here. Column names are not permitted. " I try to change the column type "nvachar", it gives me the same error does anyone know how to fix it? Thanks a lot How are you inserting the row?.
Do not use double quotation mark to delimit the string, because if QUOTED_IDENTIFIER is on, then all strings delimited by double quotation marks are interpreted as object identifiers. Use single quotation mark instead. You can check the settings using "dbcc useroptions". use northwind go create table t1 ( c1 varchar(50) ) go SET QUOTED_IDENTIFIER on go insert into t1 values("this will give an error.") go insert into t1 values('this will not give an error.') go SET QUOTED_IDENTIFIER off go insert into t1 values('this will not give an error.') go insert into t1 values("this will not give an error.") go SET QUOTED_IDENTIFIER on go select * from t1 go drop table t1 go AMB Show quote "Lam" wrote: > Hi > I try to write a C# program which will get an input string and > I need to insert to the SQL database. I set the column type as Text. > and when I insert the string variable, for exmaple, "Now", it gave the error > " > The name 'Now' is not permitted in this context. Only constants, > expressions, or variables allowed here. Column names are not permitted. " > > > I try to change the column type "nvachar", it gives me the same error > does anyone know how to fix it? > Thanks a lot > > > > I store a input string in a variable
then I use insert command to insert insertQuery="INSERT INTO Log (Name) VALUES ("+salesName+")"; where salesName is a string variable how can I fix it? Show quote "Alejandro Mesa" <AlejandroM***@discussions.microsoft.com> wrote in message news:800D594F-A3F2-4E59-9B59-18FB9DFBBE72@microsoft.com... > How are you inserting the row?. > > Do not use double quotation mark to delimit the string, because if > QUOTED_IDENTIFIER is on, then all strings delimited by double quotation marks > are interpreted as object identifiers. Use single quotation mark instead. You > can check the settings using "dbcc useroptions". > > use northwind > go > > create table t1 ( > c1 varchar(50) > ) > go > > SET QUOTED_IDENTIFIER on > go > > insert into t1 values("this will give an error.") > go > > insert into t1 values('this will not give an error.') > go > > SET QUOTED_IDENTIFIER off > go > > insert into t1 values('this will not give an error.') > go > > insert into t1 values("this will not give an error.") > go > > SET QUOTED_IDENTIFIER on > go > > select * from t1 > go > > drop table t1 > go > > > AMB > > "Lam" wrote: > > > Hi > > I try to write a C# program which will get an input string and > > I need to insert to the SQL database. I set the column type as Text. > > and when I insert the string variable, for exmaple, "Now", it gave the error > > " > > The name 'Now' is not permitted in this context. Only constants, > > expressions, or variables allowed here. Column names are not permitted. " > > > > > > I try to change the column type "nvachar", it gives me the same error > > does anyone know how to fix it? > > Thanks a lot > > > > > > > > Wrap your value in single quotes.
.... VALUES ( '" + salesName + "' )"; In case it's hard to see, that's parenthesis single-quote double-quote plus variablename plus double-quote single-quote parenthesis hth, -- Show quoteDaniel Wilson Senior Software Solutions Developer Embtrak Development Team http://www.Embtrak.com DVBrown Company "Lam" <javabeanb***@hotmail.com> wrote in message news:OORVcYwgFHA.1248@TK2MSFTNGP12.phx.gbl... > I store a input string in a variable > then I use insert command to insert > insertQuery="INSERT INTO Log (Name) VALUES ("+salesName+")"; > where salesName is a string variable > how can I fix it? > > > "Alejandro Mesa" <AlejandroM***@discussions.microsoft.com> wrote in message > news:800D594F-A3F2-4E59-9B59-18FB9DFBBE72@microsoft.com... > > How are you inserting the row?. > > > > Do not use double quotation mark to delimit the string, because if > > QUOTED_IDENTIFIER is on, then all strings delimited by double quotation > marks > > are interpreted as object identifiers. Use single quotation mark instead. > You > > can check the settings using "dbcc useroptions". > > > > use northwind > > go > > > > create table t1 ( > > c1 varchar(50) > > ) > > go > > > > SET QUOTED_IDENTIFIER on > > go > > > > insert into t1 values("this will give an error.") > > go > > > > insert into t1 values('this will not give an error.') > > go > > > > SET QUOTED_IDENTIFIER off > > go > > > > insert into t1 values('this will not give an error.') > > go > > > > insert into t1 values("this will not give an error.") > > go > > > > SET QUOTED_IDENTIFIER on > > go > > > > select * from t1 > > go > > > > drop table t1 > > go > > > > > > AMB > > > > "Lam" wrote: > > > > > Hi > > > I try to write a C# program which will get an input string and > > > I need to insert to the SQL database. I set the column type as Text. > > > and when I insert the string variable, for exmaple, "Now", it gave the > error > > > " > > > The name 'Now' is not permitted in this context. Only constants, > > > expressions, or variables allowed here. Column names are not permitted. > " > > > > > > > > > I try to change the column type "nvachar", it gives me the same error > > > does anyone know how to fix it? > > > Thanks a lot > > > > > > > > > > > > > > |
|||||||||||||||||||||||