.Net Programming Information and User Forum
September 04, 2010, 02:57:08 pm *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Welcome all new members and guests.

"I really enjoy helping all the people learning .net and c#. I hope I can show them something new today", Ken Nipper
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Insert DateTime in SQL Database in proper format  (Read 44 times)
Mahendra
Administrator
New Member
*****
Posts: 1



View Profile WWW
« on: June 14, 2010, 10:47:00 am »

Inserting DateTime in Database as a string in a DateTime datatype in SQL Server can be
stored wrong in the database and hence can retreive wrong from the database.
For eg: Below Date is

            DateTime dt = new DateTime(2009, 5, 10);
Which is May 10 2009
But when stored as string in DateTime datatype can be stored as October 5 2009

To solve this We can use SqlParameter Class
which code is given below in
StoreDateTimeasDateTime Function


Code:
namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        SqlConnection con = new SqlConnection("Data Source=PC;Initial Catalog=SampleDB;User Id=sa;Password=sa2005");

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void StoreDateTimeasString()
        {
            DateTime dt = new DateTime(2009, 5, 10);
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into Sample1 values('1','" + dt.ToShortDateString() + "')",con);
            int rupdate = cmd.ExecuteNonQuery();

            if (rupdate == 1)
            {
                MessageBox.Show("Rows Updated");
            }
            else
            {
                MessageBox.Show("Error");
            }
            con.Close();
        }

        private void StoreDateTimeasDateTime()
        {
            DateTime dt = new DateTime(2009, 5, 10);
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into Sample1 values(@Id,@DateTime)", con);

            //declare parameters
            cmd.Parameters.Add(new SqlParameter("@Id", SqlDbType.VarChar));
            cmd.Parameters.Add(new SqlParameter("@DateTime", SqlDbType.DateTime));

            //Assign parameters
            cmd.Parameters["@Id"].Value = "2";
            cmd.Parameters["@DateTime"].Value = dt;

            int rupdate = cmd.ExecuteNonQuery();

            if (rupdate == 1)
            {
                MessageBox.Show("Rows Updated");
            }
            else
            {
                MessageBox.Show("Error");
            }
            con.Close();
        }

        private void btnsds_Click(object sender, EventArgs e)
        {
            StoreDateTimeasString();
        }

        private void btnsdd_Click(object sender, EventArgs e)
        {
            StoreDateTimeasDateTime();
        }

        private void RetrievefromDatabase()
        {
            DateTime datetime = new DateTime();
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from sample1", con);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                textBox1.Text += dr.GetString(0) + "    "+dr.GetDateTime(1).ToLongDateString() +  Environment.NewLine;
                datetime=dr.GetDateTime(1);
                textBox1.Text += datetime.ToLongDateString() + Environment.NewLine;

            }
            con.Close();
        }

        private void btnrfd_Click(object sender, EventArgs e)
        {
            RetrievefromDatabase();
        }
    }
}


Thanks for Reading

Mahendra
« Last Edit: June 14, 2010, 10:50:03 am by Mahendra » Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!