CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2008
    Posts
    20

    How to insert a decimal value to database?

    Hi all,

    I have a problem with SQL:

    I have this SQL command: "insert into stores Value (Int,"String","Sting", double)
    The problem is that the double has a ',' in it so the SQL think it is 5 valus and not 4.

    I can not make a convert to Sting because I need to make a selection ordered by the double valu later on (that is select Ä from stores order by income (that is the double value)

    Any Ideas?

  2. #2
    Join Date
    Feb 2008
    Posts
    20

    Re: How to insert a decimal value to database?

    Ahh Solved it myself Change the ',' to a '.' solves the problem

  3. #3
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: How to insert a decimal value to database?

    This depends on the culture of the server where your database runs. I think in your case, the server is set to US (where '.' is the decimal seperator), and your client pc is set to some language where ',' is the decimal seperator.

    What happens when your server changes to another language. Replacing the ',' by '.' will not work anymore!!

    You should use the parameters of the command
    Code:
    int someInt = 1;
    string string1 = "test";
    string string2 = "value";
    double someDouble = 2.5;
    
    System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand();
    command.CommandText = "INSERT INTO stores VALUES (@int, @string1, @string2, @double)";
    command.Parameters.Add(new System.Data.SqlClient.SqlParameter() { ParameterName = "@int", Value = someInt });
    command.Parameters.Add(new System.Data.SqlClient.SqlParameter() { ParameterName = "@string1", Value = string1 });
    command.Parameters.Add(new System.Data.SqlClient.SqlParameter() { ParameterName = "@string2", Value = string2 });
    command.Parameters.Add(new System.Data.SqlClient.SqlParameter() { ParameterName = "@double", Value = someDouble });
    For the int and string values, it does not matter that much. It does for the double. Also you will have the same problems with DateTime values.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured