Insert Datetime Value in SQL Database with C#

How to INSERT date into SQL database date column using dateTimePicker?

What you really should do is use parameters to avoid SQL injection attacks - and it also frees you from string formatting dates - also a good thing!

//cmd is sql command
cmd.CommandText = "INSERT INTO dbo.Person(birthdate) VALUES(@Birthdate);";

cmd.Parameters.Add("@Birthdate", SqlDbType.Date).Value = dateTimePicker.Value.Date;

//con is sql connection
con.Open();
cmd.ExecuteNonQuery();
con.Close();

Also, it's a recommend best practice to put your SqlConnection, SqlCommand and SqlDataReader into using(....) { .... } blocks to ensure proper disposal:

string connectionString = ".......";
string query = "INSERT INTO dbo.Person(birthdate) VALUES(@Birthdate);";

using (SqlConnection con = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.Parameters.Add("@Birthdate", SqlDbType.Date).Value = dateTimePicker.Value.Date;

con.Open();
cmd.ExecuteNonQuery();
con.Close();
}

Insert and update a datetime into SQL database

You are missing a command , from the INSERT and UPDATE statement.

The syntax to insert data into the database is:

 INSERT INTO Table 
(Column1, Column2, Column3)
VALUES
('Value 1', 'Value 2', 'Value3')

Aside that, you are vulnerable to SQL injection, use SQL paramerterised queries to prevent this.

I would first start off by using a SqlCommand object.

SqlCommand cmd = new SqlCommand("INSERT INTO Wedstrijdschema (Team1, Team2, Datum) VALUES (@V1, @V2, @V3");

cmd.Parameters.AddWithValue("@V1", txtTeam1.Text);
cmd.Parameters.AddWithValue("@V2", txtTeam2.Text);
cmd.Parameters.AddWithValue("@V3", Convert.ToDateTime(txtDatum.Text));

And then execute it using cmd.ExecuteNonQuery();

As an additional note I would also ensure that the value in txtDatum is converted correctly to the desired date format.

Insert Datetime.now from asp.net into sql server

You're specifying a SqlDbType of Date - that doesn't have a time. So when the driver is converting the parameter value into the desired type, it's removing the time part.

You're also converting DateTime.Now into a string for no obvious reason... and doing so in a way which uses an odd and lossy time format (h:m:s, with no AM/PM designator). If you really did want to convert to a string, it would be better to use HH:mm:ss for the time specifier.

This line:

sqlComm.Parameters.Add("@join_date", SqlDbType.Date).Value = DateTime.Now.ToString("yyyy-MM-dd h:m:s");

should be:

sqlComm.Parameters.Add("@join_date", SqlDbType.DateTime).Value = DateTime.Now;

Insert multiple datetime value in sql database using c#

Try putting single-quotes around the date-value in your format string

    '{1}'

See if that helps. In fact there is actually no problem quoting up all your values that way. SQL Server will un-quote anything it doesn't like quoted.

Insert date time value into SQL Server database

Your code is equivalent to:

var parameter = new SqlParameter("@mydate", SqlDbType.DateTime);
var value = MyDTP01.Value;
parameter.Value = value;
cmd.Parameters.Add(value);

You want to add the parameter, not the value. So:

cmd.Parameters.Add(new SqlParameter("@mydate", SqlDbType.DateTime)).Value = MyDTP01.Value;

Note the location of the brackets.

This can be simplified, however - you don't need to call the SqlParameter constructor yourself - you can just pass the name and the type to Add:

cmd.Parameters.Add("@mydate", SqlDbType.DateTime).Value = MyDTP01.Value;

How to insert DateTime in MySql Database using C# code

    string dt;   
string dt2;
DateTime date = DateTime.Now;
DateTime date2 = DateTime.Now;
dt = date.ToLongTimeString(); // display format: 11:45:44 AM
dt2 = date2.ToShortDateString(); // display format: 5/22/2010

cmd.Parameters.Add("@time_out", SqlDbType.NVarChar,50).Value = dt;
cmd.Parameters.Add("@date_out", SqlDbType.NVarChar, 50).Value = dt2;
cmd.Parameters.Add("@date_time", SqlDbType.NVarChar, 50).Value = string.Concat(dt2, " ", dt); // display format: 11/11/2010 4:58:42


Related Topics



Leave a reply



Submit