|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
A float data type is recognized as varcharHello,
I am on a c# project right now. If I want to insert a float data type (c#) into the sql server, I am getting the message then, that a varchar cannot be inserted into a float. I tried it with money, small money and decimal, too. And the result is ever the same. (Varchar cannot be entered into a float/decimal, money etc..) I am using direct sql command, not a SP. As example: (simplified, without connection opening, etc.) In c#: float variable = 20; sqlcommandobject.commandtext = "insert into products (price) VALUES ('"+variable+"')" Why are you putting the value between apostrophes?
> sqlcommandobject.commandtext = "insert into products (price) VALUES sqlcommandobject.commandtext = "insert into products (price) VALUES > ('"+variable+"')" (" + variable + ")" AMB Show quote "the friendly display name" wrote: > Hello, > > I am on a c# project right now. If I want to insert a float data type (c#) > into the sql server, I am getting the message then, that a varchar cannot be > inserted into a float. I tried it with money, small money and decimal, too. > And the result is ever the same. (Varchar cannot be entered into a > float/decimal, money etc..) > > I am using direct sql command, not a SP. > > As example: (simplified, without connection opening, etc.) > > In c#: > > float variable = 20; > > sqlcommandobject.commandtext = "insert into products (price) VALUES > ('"+variable+"')" > > > Wait people, I got it a bit wrong.
The above example works, what doesn't work are floating point numbers. An integer works. something like float thing = 20 works. But, double thing = 20.5 gives the described failure. Get rid of the single quotes around the value, e.g.
sqlCommandObject.CommandText = "INSERT INTO PRODUCTS (price) VALUES (" + variable.ToString() + ")" Better yet, use parameters in your query so that it will automatically do the formatting for you, this is also more flexible for data types like binary, etc. and you don't have to worry about escaping text strings:: sqlCommandObject.CommandText = "INSERT INTO PRODUCTS (price) VALUES (@var)"; sqlCommandObject.Parameters.Add("@var", variable); Mike "the friendly display name" <thefriendlydisplayn***@discussions.microsoft.com> wrote in message Show quote news:26A40CD8-45CE-4149-BF00-6064A0741640@microsoft.com... > Hello, > > I am on a c# project right now. If I want to insert a float data type (c#) > into the sql server, I am getting the message then, that a varchar cannot > be > inserted into a float. I tried it with money, small money and decimal, > too. > And the result is ever the same. (Varchar cannot be entered into a > float/decimal, money etc..) > > I am using direct sql command, not a SP. > > As example: (simplified, without connection opening, etc.) > > In c#: > > float variable = 20; > > sqlcommandobject.commandtext = "insert into products (price) VALUES > ('"+variable+"')" > > > Thank you.
The parameters solved the problem. Show quote "Mike Jansen" wrote: > Get rid of the single quotes around the value, e.g. > > sqlCommandObject.CommandText = > "INSERT INTO PRODUCTS (price) VALUES (" + variable.ToString() + ")" > > Better yet, use parameters in your query so that it will automatically do > the formatting for you, this is also more flexible for data types like > binary, etc. and you don't have to worry about escaping text strings:: > > sqlCommandObject.CommandText = > "INSERT INTO PRODUCTS (price) VALUES (@var)"; > sqlCommandObject.Parameters.Add("@var", variable); > > Mike |
|||||||||||||||||||||||