Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
insert into语句中的语法错误(使用MS Access数据库的MS.net代码)_.net_Sql_Ms Access - Fatal编程技术网

insert into语句中的语法错误(使用MS Access数据库的MS.net代码)

insert into语句中的语法错误(使用MS Access数据库的MS.net代码),.net,sql,ms-access,.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 S

我在运行此代码时出错。在我使用“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;
            }
        }
    }
}

我不太确定,但我在VB中知道的。。SQL插入语法为

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

我没有使用您的样式,但可能您缺少这些字段列表。

代码中有两条INSERT语句,单击按钮1或按钮2时是否会出现错误?我建议在format语句之后显示字符串Q的内容,以查看显示的内容。我怀疑这是一个引用问题,但如果我们知道语句的实际外观,则可以更好地判断。如果您为表中的每个字段提供值,则字段列表是可选的。。。