Insert语句

问:

问题是::未处理System.Data.OleDb.OleDbException   HResult=-2147217900   Message=语法错误 (操作符丢失) 在查询表达式 '2014/12/1 0:00:00' 中。

public void add(string addselect)
        {
            oledbconn = dataconn.getcon();
            oledbconn.Open();
            OleDbCommand cmd = new OleDbCommand(addselect, oledbconn);
          int x = (int)cmd.ExecuteNonQuery();
            if (x > 0)
            {
                MessageBox.Show("添加信息成功!");
            }
            oledbconn.Close();

       
        }



private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "")
                MessageBox.Show("登记编号或姓名不能为空!");
            else
            {
                string st1 = dateTimePicker1.Value.Date.ToString();
                string inSQL = String.Format("insert into dingfang(登记编号,姓名,性别,身份证号,联系方式,房间类型,房间号,入住天数,登记日期) values({0},{1},{2},{3},{4},{5},{6},{7},{8})", textBox1.Text, textBox2.Text, comboBox3.Text, textBox3.Text, textBox4.Text, comboBox1.Text, comboBox2.Text, textBox5.Text, st1);
                
                dataoperate.add(inSQL);
                dataoperate.SaveImage(textBox1.Text.Trim(), openFileDialog1);
            }
        }

答:

string inSQL = String.Format("insert into dingfang(登记编号,姓名,性别,身份证号,联系方式,房间类型,房间号,入住天数,登记日期) values({0},{1},{2},{3},{4},{5},{6},{7},{8})", textBox1.Text, textBox2.Text, comboBox3.Text, textBox3.Text, textBox4.Text, comboBox1.Text, comboBox2.Text, textBox5.Text, st1);


大家特别注意对字符串的''号

原文地址:https://www.cnblogs.com/dengshiwei/p/4258654.html