【问题标题】:Syntax error in insert into statement(MS.net code with MS Access database)插入语句中的语法错误(带有 MS Access 数据库的 MS.net 代码)
【发布时间】:2013-06-05 13:31:08
【问题描述】:

我在运行此代码时出错。错误存在于我使用“插入”语句的地方。我无法理解是什么问题。

错误: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;
            }
        }
    }
}

【问题讨论】:

  • 代码中有两条INSERT语句,请问是点击按钮1还是按钮2时出现错误?我建议在格式语句之后显示字符串 Q 的内容以查看显示的内容。我怀疑这是一个引用问题,但如果我们知道该语句的实际样子,可以更好地判断。

标签: .net sql ms-access


【解决方案1】:

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

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

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

【讨论】:

  • 如果您要为表中的每个字段提供值,则字段列表是可选的...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 2017-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
相关资源
最近更新 更多