我正在尝试将数据插入MS Access中的表中。我一直在SQL语句结束时收到错误分号(;)。或者是一个不同的错误,说我的插入查询需要有一个值或表。这是我的代码
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();
}
}
}
}
答案 0 :(得分:0)
您应该以分号
分隔查询fig, ax = plt.subplots(len(data), 1, figsize=(7,5))
此外,您无法将列名称作为参数传递。在这种情况下,请使用动态查询。
答案 1 :(得分:0)
您应该将查询更改为:
string cmdText = @"INSERT INTO [Sales]
([Printers], [Ink], [Paper], [EmpID])
VALUES (@Printers,@Ink,@Paper,
SELECT EmpID FROM (Employee) )";
答案 2 :(得分:0)
最有可能应该阅读:
string cmdText = @"INSERT INTO [Sales]
([Printers], [Ink], [Paper], [EmpID])
VALUES (@Printers, @Ink, @Paper, @EmpID)";
答案 3 :(得分:0)
“cmd.Paramter.AddWithValue”是不必要的。
尝试将cmdText格式化为
cmd = "INSERT INTO [Sales] ([Printers], [Ink], [Paper])
VALUES (" + txtPrinters.Text + " ," + txtInk.Text + ", " + txtPaper.Text + ")
这清除了“cmd.Paramter.AddWithValue”重复或覆盖值的混淆。
重写btnUpdate_Click(也不确定Emplopyee是否是拼写错误)
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);
}
}