简体   繁体   中英

How to insert and update date in SQL using list view in c#

I am doing my program using SQL and c#.How can I insert a value of date through a datetimepicker in list view from c# to database? What data type should I use in date in SQL database?

First, I would like to insert the date using datetimepicker. Then, how would it be saved in SQL database in the ReservedDate field? (What line of code should I use). Also what data type should I set the ReservedDate field?

You can use following to convert your datetimepicker value to DateTime

Convert.ToDateTime(this.dateTimePicker1.Value).ToString("MM/dd/yyyy");

Insert like

 cmd.Parameters.AddWithValue("@ReservedDate", dateTimePicker1.Value.ToString("MM/dd/yyyy"));

NOTE: And its better to use datetime datatype for date fields.

EDIT:

"INSERT INTO yourTableName(ReservationDate) Values( '" + dateTimePicker1.Value.Date + "')";

and Take your ReservationDate column of type date in SQL SERVER database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM