insert into语句中的语法错误(MS Access代码与MS Access数据库)

时间:2013-06-05 13:31:08

标签: .net sql ms-access

运行此代码时出错。我使用“insert into”语句时出现错误。我不明白是什么问题。

错误:INSERT INTO语句中的语法错误

请告诉我给定代码中的问题在哪里

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.Data.OleDb;

namespace GTC
{
    public partial class Adminhome : Form
    {
        public Adminhome()
        {
            InitializeComponent();
        }

        OleDbConnection cn = new OleDbConnection("provider=microsoft.ace.oledb.12.0;data source=|DataDirectory|/GTC.accdb;");




        void clear()
        {
            textBox1.Text = "";
            comboBox1.Text = "-Select-";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cn.Open();
            // TODO: This line of code loads data into the 'GTCDataSet2.Society' table. You can move, or remove it, as needed.
            this.SocietyTableAdapter.Fill(this.GTCDataSet2.Society);
            // TODO: This line of code loads data into the 'GTCDataSet2.User' table. You can move, or remove it, as needed.
            this.UserTableAdapter.Fill(this.GTCDataSet2.User);
            this.reportViewer1.RefreshReport();
            this.reportViewer2.RefreshReport();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                String q = String.Format("insert into Society values('{0}')", textBox5.Text);
                OleDbCommand cmd = new OleDbCommand(q, cn);
                cmd.ExecuteNonQuery();
                label8.Text = "Society Added...";
                clear();
            }
            catch (Exception err)
            {
                label8.Text = err.Message;
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            try
            {
                String q = String.Format("insert into User values('{0}','{1}','{2}','{3}','{4}')", textBox1.Text, comboBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text);
                OleDbCommand cmd = new OleDbCommand(q, cn);
                cmd.ExecuteNonQuery();
                label6.Text = "User Added...";
                clear();
            }
            catch (Exception err)
            {
                label6.Text = err.Message;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我不太确定,但我在VB中知道.. SQL插入语法是

 INSERT INTO tablename (field1, field2, ...) VALUES (val1, val2, ...)

我没有使用你的风格,但也许你已经错过了那些字段列表..