Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm working on a project of school(information system) data management system.I've to save the Date of admission of students in Database.
i'm confuse what is the best and easy method to save the "Date" in database.
There are many options . i.e. there is a "Date"DataType already given in table column definition of SQL server.
another "datetimepicker" control is available in C# forms.
suggest me the better way to save the date in database that will be easy to handle in further development of project.
Posted
Updated 23-Mar-15 0:13am
v2
Comments
Tushar sangani 23-Mar-15 6:17am    
you can use datatype Datetime in sql
Ammar Shaukat 23-Mar-15 13:58pm    
i'm using Date only. well i get your point you're saying i use DateTime in SQL to save the input value from DateTimePicker control form C# Forms

1 solution

Use the DateTimePicker control, and collect the user input from it's Value property - this hands you a DateTime value which is guaranteed to be valid.

Then use a parameterised query to pass the DateTime value directly to SQL and store it in a DATE Column.
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("INSERT INTO myTable (myDateColumn) VALUES (@DT)", con))
        {
        cmd.Parameters.AddWithValue("@DT", myDateTimePicker.Value);
        cmd.ExecuteNonQuery();
        }
    }
 
Share this answer
 
Comments
Ammar Shaukat 23-Mar-15 13:56pm    
Thanks..
cmd.Parameters.AddwithValue();
you're using StoredProcedure here. as looking from code...
OriginalGriff 23-Mar-15 15:00pm    
Stored Procedure? No - it's a standard INSERT query. No SP involved.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900