Reputation: 143
I'm trying to enter a double value in an SQL update statment, which is already converted (see code 1), to keep his numbers behind the comma.
(1)
double.TryParse(splitline[8], NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out natMB //conversion
(2) Putting my value in an procedure to my SQL statement, i putted natMB into 'Verbruikgegevens', index position 2. 0 or 1 indicates if my inputted data is roaming or not. This is a part of an if statement declaring weither it's roaming or national data.
sqlStatementUpdate(TrafficdataID, 0,
(Convert.ToDouble(Verbruikgegevens.Rows[intCount].ItemArray[2],
CultureInfo.InvariantCulture))
(3) SQL statement in Visual studio (C#)
public void sqlStatementUpdate(long TrafficdataID, byte Roaming, double Value)
{
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = connection2;
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.CommandText = "SP_SCTrafficdataDetailUpdate";
DataAccessHelper.AddParam(sqlCmd, "TDDValue", SqlDbType.Decimal, Convert.ToDecimal(Value));
DataAccessHelper.AddParam(sqlCmd, "TrafficdataID", SqlDbType.BigInt, TrafficdataID);
DataAccessHelper.AddParam(sqlCmd, "TDDRoaming", SqlDbType.Bit, Roaming);
DataAccessHelper.ExecSProcDS(sqlCmd);
}
(4) DataAccesHelper
internal static class DataAccessHelper
{
public static void AddParam(SqlCommand cmd, string columnName, SqlDbType dbType, object paramvalue)
{
if (paramvalue is DateTime)
{
if (((DateTime)paramvalue).Date == DateTime.MinValue.Date)
{
paramvalue = DBNull.Value;
}
}
if (paramvalue == null)
{
paramvalue = DBNull.Value;
}
string param = "@" + columnName;
if (!cmd.Parameters.Contains(param))
{
if (dbType == SqlDbType.VarChar || dbType == SqlDbType.NVarChar || dbType == SqlDbType.Char || dbType == SqlDbType.NChar)
cmd.Parameters.Add(param, dbType, 4000);
else
cmd.Parameters.Add(param, dbType);
cmd.Parameters[param].SourceColumn = columnName;
cmd.Parameters[param].Value = paramvalue;
}
}
public static DataSet ExecSProcDS(SqlCommand cmd)
{
DataSet ds = new DataSet();
try
{
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds);
}
catch (Exception ex)
{
throw (ex);
}
return ds;
}
(5) SQL statement
ALTER PROCEDURE [dbo].[SP_SCTrafficdataDetailUpdate] @TDDValue decimal, @TrafficdataID bigint, @TDDRoaming bit
AS
BEGIN
UPDATE TR_SCTrafficDataDetail
SET TDDValue = @TDDValue
WHERE (TDDType = 'Data') AND (TrafficDataID = @TrafficdataID) AND (TDDRoaming = @TDDRoaming)
END
QUESTION: Is it possible that I keep using a double, and also keep my numbers behind the comma when importing the data to my sqltable? Right now it doesn't seem to work that wel ... I defined my Value in my SQLTable as an decimal though
Upvotes: 0
Views: 2216
Reputation: 35613
So the question is:
Answer: This has nothing to do with the SQL data types. You need to first write reliable C# code to convert the number to a double or decimal. Either will do - you should use whichever one matches your database.
The Invariant culture REQUIRES the period as a decimal point, NOT the comma. So you are using the wrong culture if you have commas as decimal separator.
If you know what format your numbers are in, use the overloaded Parse and specify the format directly.
If it is from user input, use the user's culture, OR train the user what format to use.
Finally, if you have some odd format, you might need to write your own code to regularise the format using regexes.
Speed issues
10000 rows is nothing it should take seconds not minutes.
I notice you are creating a new SqlCommand for each row. Each time you do so it has to go to the SQL server and match up the metadata with the stored proc you are calling.
Just create it once, and re-use it by setting the values to the values from the new row. Just don't forget to set the NULL ones to DBNull.Value.
Upvotes: 2