如何创建我想在sql中插入的列类型日期

时间:2016-12-26 14:01:30

标签: c# datetime

我想将日期格式插入到sql

我该怎么做?

 private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection conn = new SqlConnection("Data Source=Server-1;Initial Catalog=Eczane;Integrated Security=True");
                conn.Open();
                SqlCommand cmd = new SqlCommand("INSERT INTO TBL_Musteri (MUSTERI_TC,MUSTERI_AD,MUSTERI_SOYADI,MUSTERI_DOGUM_TARIHI,MUSTERI_CINSIYET,MUSTERI_TELEFON,MUSTERI_ADRES,MUSTERI_IL,MUSTERI_ILCE,MUSTERI_EKLEYEN_ADMIN) VALUES ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox9.Text + "','" + comboBox1.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "')", conn);
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (Exception)
            {

                MessageBox.Show("Erorr!");
            }
        }

1 个答案:

答案 0 :(得分:0)

Parameters对象上使用cmd集合。

您可以指定类型,并为您完成转换。此外,它还可以防止您的代码暴露给SQL注入。

SqlCommand cmd = new SqlCommand("INSERT INTO TBL_Musteri (MUSTERI_TC,MUSTERI_AD,MUSTERI_SOYADI,MUSTERI_DOGUM_TARIHI,
MUSTERI_CINSIYET,MUSTERI_TELEFON,MUSTERI_ADRES,MUSTERI_IL,MUSTERI_ILCE,MUSTERI_EKLEYEN_ADMIN) 
VALUES (@param1,@param2,@param3,@param4,@param5,
@param6,@param7,@param8,@param9,@param10)", conn);

cmd.Parameters.Add("@param1", SqlDbType.NVarChar);
cmd.Parameters["@param1"] = textBox1.Text;
...
cmd.Parameters.Add("@param4", SqlDbType.Date;
cmd.Parametes["@param4"] = textBox9.Text;
...