Using DateTime in a SqlParameter for Stored Procedure, format error

后端 未结 4 1223
予麋鹿
予麋鹿 2020-11-27 20:04

I\'m trying to call a stored procedure (on a SQL 2005 server) from C#, .NET 2.0 using DateTime as a value to a SqlParameter. The SQL type in the s

相关标签:
4条回答
  • 2020-11-27 20:37

    How are you setting up the SqlParameter? You should set the SqlDbType property to SqlDbType.DateTime and then pass the DateTime directly to the parameter (do NOT convert to a string, you are asking for a bunch of problems then).

    You should be able to get the value into the DB. If not, here is a very simple example of how to do it:

    static void Main(string[] args)
    {
        // Create the connection.
        using (SqlConnection connection = new SqlConnection(@"Data Source=..."))
        {
            // Open the connection.
            connection.Open();
    
            // Create the command.
            using (SqlCommand command = new SqlCommand("xsp_Test", connection))
            {
                // Set the command type.
                command.CommandType = System.Data.CommandType.StoredProcedure;
    
                // Add the parameter.
                SqlParameter parameter = command.Parameters.Add("@dt",
                    System.Data.SqlDbType.DateTime);
    
                // Set the value.
                parameter.Value = DateTime.Now;
    
                // Make the call.
                command.ExecuteNonQuery();
            }
        }
    }
    

    I think part of the issue here is that you are worried that the fact that the time is in UTC is not being conveyed to SQL Server. To that end, you shouldn't, because SQL Server doesn't know that a particular time is in a particular locale/time zone.

    If you want to store the UTC value, then convert it to UTC before passing it to SQL Server (unless your server has the same time zone as the client code generating the DateTime, and even then, that's a risk, IMO). SQL Server will store this value and when you get it back, if you want to display it in local time, you have to do it yourself (which the DateTime struct will easily do).

    All that being said, if you perform the conversion and then pass the converted UTC date (the date that is obtained by calling the ToUniversalTime method, not by converting to a string) to the stored procedure.

    And when you get the value back, call the ToLocalTime method to get the time in the local time zone.

    0 讨论(0)
  • 2020-11-27 20:45

    If you use Microsoft.ApplicationBlocks.Data it'll make calling your sprocs a single line

    SqlHelper.ExecuteNonQuery(ConnectionString, "SprocName", DOB)
    

    Oh and I think casperOne is correct...if you want to ensure the correct datetime over multiple timezones then simply convert the value to UTC before you send the value to SQL Server

    SqlHelper.ExecuteNonQuery(ConnectionString, "SprocName", DOB.ToUniversalTime())
    
    0 讨论(0)
  • 2020-11-27 20:49

    Here is how I add parameters:

    sprocCommand.Parameters.Add(New SqlParameter("@Date_Of_Birth",Data.SqlDbType.DateTime))
    sprocCommand.Parameters("@Date_Of_Birth").Value = DOB
    

    I am assuming when you write out DOB there are no quotes.

    Are you using a third-party control to get the date? I have had problems with the way the text value is generated from some of them.

    Lastly, does it work if you type in the .Value attribute of the parameter without referencing DOB?

    0 讨论(0)
  • 2020-11-27 20:55

    Just use:

     param.AddWithValue("@Date_Of_Birth",DOB);
    

    That will take care of all your problems.

    0 讨论(0)
提交回复
热议问题