'00'错误附近的语法不正确

时间:2014-08-22 14:02:29

标签: c# sql

请帮我在sql数据库中包含一个日期,一旦它工作但现在我只是更改了表单(从主表单到添加表单),它现在不会插入。

这是我的代码,我已将其设置为自定义格式dd-MM-yyyy,数据库中的恶魔是日期时间类型:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Data.SqlClient;
using DgvFilterPopup;

namespace ExpertGeoMaster_v._1
{
    public partial class Ajout : Form

    {
        DataTable DT = new DataTable();

        public Ajout()
        {
            InitializeComponent();
        }

        private void Ajout_Load(object sender, EventArgs e)
        {
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            // textbox1.SetValue(TextBoxHelper.ShowWatermarkProperty, box.Text == string.Empty);
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection(global::ExpertGeoMaster_v._1.Properties.Settings.Default.AgendaConnectionString);
            try
            {
                string sql = @"INSERT INTO Archive$ (DATE,TRAVAIL,SITUATION,DEMANDEUR,ACCORD,AV,solde,OBSAERVATION)  VALUES ("+ dateTimePicker1.Value.Date +",'" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "'," + textBox8.Text + "," + textBox9.Text + "," + textBox10.Text + ",'" + textBox11.Text + "')";

                SqlCommand execSql = new SqlCommand(sql, cn);
                cn.Open();
                execSql.ExecuteNonQuery();

                MessageBox.Show("Ajouté avec succé !!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);



            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());//Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                cn.Close();
            }
        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {

        }
        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox9_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox10_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox11_TextChanged(object sender, EventArgs e)
        {

        }

        private void bk_btn_Click(object sender, EventArgs e)
        {
            Form1 new_form = new Form1();
            new_form.Show();
            this.Close();
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox5_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void textBox7_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void textBox8_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsNumber(e.KeyChar) || e.KeyChar == ',' || e.KeyChar == '.') 
             {

             }
            else
            {
                e.Handled = e.KeyChar != (char)Keys.Back;
            }
        }

        private void textBox10_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsNumber(e.KeyChar) || e.KeyChar == ',' || e.KeyChar == '.') 
             {

             }
            else
            {
                e.Handled = e.KeyChar != (char)Keys.Back;
            }
        }

        private void textBox9_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsNumber(e.KeyChar) || e.KeyChar == ',' || e.KeyChar == '.') 
             {

             }
            else
            {
                e.Handled = e.KeyChar != (char)Keys.Back;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您尝试将日期的字符串表示形式传递给数据库引擎,并且正如预期的那样,您将失败。这是使用参数化查询的原因之一。您创建一个DateTime参数,让数据库引擎知道如何正确读取日期而不尝试解释字符串。另一个原因是Sql Injection和更好的性能。

所以,说这是一个例子

string sql = @"INSERT INTO Archive$
              ([DATE],TRAVAIL,SITUATION,DEMANDEUR,ACCORD,AV,solde,OBSAERVATION)  
              VALUES (@dt, @tr, @si, @de, @ac, @av, @so, @ob)";
using(SqlCommand execSql = new SqlCommand(sql, cn))
{
    cmd.Parameters.AddWithValue("@dt",dateTimePicker1.Value);
    cmd.Parameters.AddWithValue("@tr",textBox5.Text );
    cmd.Parameters.AddWithValue("@si",textBox6.Text );
    ..... and so on for the other parameters .....
    cn.Open();
    execSql.ExecuteNonQuery();
}

请记住,如果您需要最高性能并且需要将参数值转换为基础表所需的确切数据类型,则使用AddWithValue不是最佳选项。例如,如果solde是十进制字段,则在将参数添加到集合时需要将文本框内容转换为十进制值(原因与日期和字符串相同。您将离开解释数据库引擎的小数分隔符)

您的查询中还有另一个可能的错误。 DATE是Sql Server中的reserved keyword(可能在其他数据库系统中),因此您应将其括在方括号中