📅  最后修改于: 2023-12-03 14:59:39.897000             🧑  作者: Mango
As a programmer, you may often need to work with dates and times in your programs. In C#, the DateTime
class provides various methods and properties to work with date and time values. However, when you need to store date and time values in a SQL Server database, you need to convert the DateTime
value to SQL Server's datetime data type.
To convert a C# DateTime
value to SQL Server's datetime data type, you can use the ToString()
method with a specific format string. The format string that you use will depend on the format that you need for your SQL Server datetime value.
Here's an example of converting a C# DateTime
value to SQL Server's datetime data type using the format string yyyy-MM-dd HH:mm:ss
:
DateTime myDateTime = DateTime.Now;
string sqlDateTime = myDateTime.ToString("yyyy-MM-dd HH:mm:ss");
In the above code, we first create a DateTime
object myDateTime
with the current date and time using DateTime.Now
. We then convert this to a string using the ToString()
method with the format string yyyy-MM-dd HH:mm:ss
. This format string will produce a string in the format yyyy-MM-dd HH:mm:ss
, which is compatible with SQL Server's datetime data type.
When you insert or update data in a SQL Server database using C#, you should always use SQL parameters to prevent SQL injection attacks. To use SQL parameters with datetime values, you can use the SqlParameter
class and its SqlDbType
property to specify the data type as SqlDbType.DateTime
.
Here's an example of using SQL parameters to insert a DateTime
value into a SQL Server database:
DateTime myDateTime = DateTime.Now;
using (SqlConnection connection = new SqlConnection("ConnectionString"))
{
string sql = "INSERT INTO MyTable (MyDateTimeColumn) VALUES (@MyDateTime)";
SqlCommand command = new SqlCommand(sql, connection);
SqlParameter myDateTimeParam = new SqlParameter("@MyDateTime", SqlDbType.DateTime);
myDateTimeParam.Value = myDateTime;
command.Parameters.Add(myDateTimeParam);
connection.Open();
command.ExecuteNonQuery();
}
In the above code, we first create a DateTime
object myDateTime
with the current date and time using DateTime.Now
. We then create a SqlConnection
object with the appropriate connection string, and a SQL query string sql
to insert a value into a column MyDateTimeColumn
in a table MyTable
.
We create a SqlCommand
object command
with the SQL query string and the SqlConnection
object. We then create a SqlParameter
object myDateTimeParam
, set its SqlDbType
property to SqlDbType.DateTime
, and set its Value
property to myDateTime
. We add this parameter to the command.Parameters
collection.
We open the connection using connection.Open()
, and execute the SQL query using command.ExecuteNonQuery()
. The myDateTime
value is inserted into the MyDateTimeColumn
column in the MyTable
table as a SQL Server datetime value.
In this article, we have seen how to convert a C# DateTime
value to SQL Server's datetime data type, and how to insert a DateTime
value into a SQL Server database using SQL parameters. It is important to use SQL parameters to prevent SQL injection attacks in your applications.