I have a database table with two float type columns, and I am trying to insert two numbers via c# code.
This is my code for sql parameters:
sqlCommand.Parameters.Add(new SqlParameter("@Number1", SqlDbType.Decimal) { Precision = 17, SqlValue = Number1FloatValue ?? (object)DBNull.Value });
sqlCommand.Parameters.Add(new SqlParameter("@Number2", SqlDbType.Decimal) { Precision = 17, SqlValue = Number2FloatValue ?? (object)DBNull.Value });
First number is -102272.125 and the second is 256.245
In the debugger when I take a look at the values, from some reason SqlValue of the first number becomes -102272.1 - last two digits are missing, and Value property is fine -102272.125, and the second number is ok and gets inserted correctly as 256.245. I thought that it is some problem with the default precision, so I set it to 17, but it didn't helped. And also, firstly I was setting Value instead of SqlValue and later changed to SqlValue assuming that that could be a problem but it didn't helped.
More strangely: when I change SqlDbType.Decimal to SqlDbType.Float (as I tried initially), then first number gets inserted correctly as -102272.125, but the second number gets some unwanted additional decimals: 256.244996832...
I now that float type is not the best way to store decimal numbers in database, but I cannot change the types of table columns.
So, how to get this code insert both values correctly?
This is my code:
static void Main(string[] args)
{
float? number1 = -102272.125f;
float? number2 = 256.245f;
using (SqlConnection openCon = new SqlConnection("Data source=.; Initial Catalog=MyDatabase; Persist Security Info=True; Integrated Security=true;"))
{
string saveStaff = "INSERT into test_float (Number1,Number2) VALUES (@Number1,@Number2)";
using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))
{
querySaveStaff.Connection = openCon;
querySaveStaff.Parameters.Add("@Number1", SqlDbType.Float).Value = number1 ?? (object)DBNull.Value;
querySaveStaff.Parameters.Add("@Number2", SqlDbType.Float).Value = number2 ?? (object)DBNull.Value;
openCon.Open();
querySaveStaff.ExecuteNonQuery();
}
}
}
And this is databse table and result of an insert. This was my initial problem - Number2 has much more decimals than expected, after which I tried to change SqlDbType to decimal, but running into problem that the first number got rounded to -102272.1, and second number was fine: 256.245. So I am not sure how to get it work in all cases.
And I cannot change the sql table.
When I try something like this:
static void Main(string[] args)
{
float? number1 = -102272.125f;
float? number2 = 256.245f;
using (SqlConnection openCon = new SqlConnection("Data source=.; Initial Catalog=MyDatabase; Persist Security Info=True; Integrated Security=true;"))
{
string saveStaff = "INSERT into test_float (Number1,Number2) VALUES (@Number1,@Number2)";
using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))
{
querySaveStaff.Connection = openCon;
//querySaveStaff.Parameters.Add("@Number1", SqlDbType.Float).Value = number1 ?? (object)DBNull.Value;
//querySaveStaff.Parameters.Add("@Number2", SqlDbType.Decimal).Value = number2 ?? (object)DBNull.Value;
querySaveStaff.Parameters.Add(new SqlParameter("@Number1", SqlDbType.Decimal){Precision = 17, Scale=6, Value = number1, SqlValue = number1});
querySaveStaff.Parameters.Add(new SqlParameter("@Number2", SqlDbType.Decimal) { Precision = 17, Scale = 6, Value = number2, SqlValue = number2 });
openCon.Open();
querySaveStaff.ExecuteNonQuery();
}
}
}
Then, second number is somehow fine, but the first isn't:
Edit 2:
I have just found that using double type in c# for numbers I am trying to insert MAYBE will help:
double? number1 = -102272.125;
double? number2 = 256.245;
using (SqlConnection openCon = new SqlConnection("Data source=.; Initial Catalog=MyDatabase; Persist Security Info=True; Integrated Security=true;"))
{
string saveStaff = "INSERT into test_float (Number1,Number2) VALUES (@Number1,@Number2)";
using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))
{
querySaveStaff.Connection = openCon;
querySaveStaff.Parameters.AddWithValue("@Number1", number1 ?? (object)DBNull.Value);
querySaveStaff.Parameters.AddWithValue("@Number2", number2 ?? (object)DBNull.Value);
openCon.Open();
querySaveStaff.ExecuteNonQuery();
}
}
Now it looks fine at least for this two numbers: