tags:

views:

187

answers:

3

i cant insert for using c# language DateTime.Now.ToString()

insert sqlserver in datatype datetime field

A: 

You shouldnt have to perform ToString() in order to insert to an SQL server db

qui
A: 

Your question didn't make a lot of sense, but I think you're looking for this:

DateTime.Now.ToString(string format)

That'll format the DateTime in the way you want it to.

Yous really shouldn't be building your SQL queries as strings in the first place, though. You should be using parameters, which allow you to give a C# non-string object rather than a converted string.

Matthew Iselin
mm or MM ? month right?
monkey_boys
http://msdn.microsoft.com/en-us/library/aa326721(VS.71).aspx
Matthew Iselin
Also, read http://www.codeproject.com/KB/database/sql_in_csharp.aspx. Specifically, look at the SQL parameters stuff - it's what you need.
Matthew Iselin
Indeed, MM/dd/yyyy, mm = minutes.
Ruben
+4  A: 

Don't convert your DateTime value to a string. Use parameterised SQL instead:

string sql = "INSERT INTO Your_Table (Your_Column) VALUES (@YourParam)";

using (SqlConnection conn = new SqlConnection("..."))
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
    cmd.Parameters.Add("@YourParam", SqlDbType.DateTime).Value = yourDate;

    conn.Open();
    cmd.ExecuteNonQuery();
}
LukeH