简体   繁体   中英

C# Insert Into syntax error

I am trying to insert data into a table in MS Access. I keep getting the error Missing semicolon (;) at end of SQL statement. or a different error saying that i my Insert query needs to have a value or table in it. Here is my code

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;
using System.Data.SqlClient;



namespace MiddleWare
{
public partial class Sales : Form
{
    public Sales()
    {
        InitializeComponent();
    }

    private void btnUpdate_Click(object sender, EventArgs e)
    {
        int empId = int.Parse(txtEmpID.Text);
        string cmdText = @"INSERT INTO [Sales] 
                        ([Printers], [Ink], [Paper])
               VALUES (@Printers,@Ink,@Paper)
               SELECT @EmpID FROM (Emplopyee)";

        using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\cp-stud-nas1\users\mat72462\Documents\SalesData.accdb"))
        using (OleDbCommand cmd = new OleDbCommand(cmdText, con))
        {
            con.Open();
            cmd.Parameters.AddWithValue("@Printers", OleDbType.VarWChar).Value = txtPrinters.Text;
            cmd.Parameters.AddWithValue("@Ink", OleDbType.VarWChar).Value = txtInk.Text;
            cmd.Parameters.AddWithValue("@Paper", OleDbType.VarWChar).Value = txtPaper.Text;
            cmd.Parameters.AddWithValue("@EmpID", OleDbType.VarWChar).Value = txtEmpID.Text;
            cmd.ExecuteNonQuery();

            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@EmpID", txtEmpID.Text);

            cmd.CommandText = "SELECT [Total Sales] FROM Sales WHERE EmpID=@EmpID";
            string result = cmd.ExecuteScalar().ToString();
            MessageBox.Show(result);

        }
    }

    private void Sales_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'DataSet1.DataTable1' table. You can move, or remove it, as needed.
        this.DataTable1TableAdapter.Fill(this.DataSet1.DataTable1);






    }

    private void btnReport_Click(object sender, EventArgs e)
    {
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\cp-stud-nas1\users\mat72462\Documents\SalesData.accdb");

        {
            this.DataTable1TableAdapter.Fill(this.DataSet1.DataTable1);

            this.reportViewer1.RefreshReport();
        }
    }
}
}

You should seperate the queries by semicolon

string cmdText = @"INSERT INTO [Sales] 
                ([Printers], [Ink], [Paper])
       VALUES (@Printers,@Ink,@Paper);
       SELECT @EmpID FROM (Emplopyee)";

Also you can't pass the column name as parameter. In that case use dynamic query.

you should change the query to this:

 string cmdText = @"INSERT INTO [Sales] 
                    ([Printers], [Ink], [Paper], [EmpID])
           VALUES (@Printers,@Ink,@Paper,
                   SELECT EmpID FROM (Employee) )";

It most likely should read:

string cmdText = @"INSERT INTO [Sales] 
    ([Printers], [Ink], [Paper], [EmpID])
    VALUES (@Printers, @Ink, @Paper, @EmpID)";

The "cmd.Paramter.AddWithValue" are unnecessary.

Try formatting your cmdText as

 cmd = "INSERT INTO [Sales] ([Printers], [Ink], [Paper])
     VALUES (" + txtPrinters.Text + " ," + txtInk.Text + ", " + txtPaper.Text + ")

This clears up confusion of "cmd.Paramter.AddWithValue" duplicating or overriding values.

Rewritten btnUpdate_Click (also not sure if Emplopyee is a typo or not)

private void btnUpdate_Click(object sender, EventArgs e)
{
    using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\cp-stud-nas1\users\mat72462\Documents\SalesData.accdb"))
    using (OleDbCommand cmd = new OleDbCommand(cmdText, con))
    {
        con.Open();
        cmd = "INSERT INTO [Sales] ([Printers], [Ink], [Paper]) VALUES (" + txtPrinters.Text + " ," + txtInk.Text + ", " + txtPaper.Text + ") SELECT " + textEmpID.Text + " FROM (Emplopyee)";
        cmd.ExecuteNonQuery();

        cmd.CommandText = "SELECT [Total Sales] FROM Sales WHERE EmpID=@EmpID";
        string result = cmd.ExecuteScalar().ToString();
        MessageBox.Show(result);

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM