这是我尝试使用C#将数据添加到访问数据库中的地方。现在当我运行应用程序时,我得到一个错误,说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.Windows.Forms;
using System.Data.OleDb;
namespace Santosh
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" && textBox2.Text == "" && textBox3.Text == "")
MessageBox.Show("Plz Specify the UserName & Password","ERROR",MessageBoxButtons.OKCancel,MessageBoxIcon.Error);
else if (textBox2.Text == "" && textBox3.Text == "")
MessageBox.Show("Plz Specify the Password", "ERROR", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
else if (textBox3.Text == "")
MessageBox.Show("Plz RE Enter the Password", "ERROR", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
else
{
string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\leelakrishnan\Desktop\database1.accdb";
String uname, pass;
string id;
id = textBox1.Text;
uname = textBox2.Text;
//System.Windows.Forms.MessageBox.Show(uname);
pass = textBox3.Text;
OleDbConnection myConnection = new OleDbConnection(connectionString);
myConnection.Open();
int i=107;
string query = @"insert into EMPLOYEE_TABLE (ID,UserName,Password) VALUES ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')";
//string query = "insert into LOGIN_TABLE (ID,UserName,Password) VALUES (i,'uname',' pass')";
i++;
OleDbCommand myCommand = myConnection.CreateCommand();// new OleDbCommand();
myCommand.CommandText = query;
//TODO: Review the datatypes and lengths for these parameters
OleDbParameter myParm = myCommand.Parameters.Add("@id", OleDbType.VarChar,50);
myParm.Value = textBox1.Text;
myParm = myCommand.Parameters.Add("@uname", OleDbType.VarChar, 50);
myParm.Value = textBox2.Text;
myParm = myCommand.Parameters.Add("@pass", OleDbType.VarChar, 50);
myParm.Value = textBox3.Text;
myCommand.ExecuteNonQuery();
myConnection.Close();
System.Windows.Forms.MessageBox.Show("User Account Succefully Created", "Caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
}
}
}
答案 0 :(得分:1)
看到你已经参数化了你的命令(干得好!),接下来你应该开始编写参数化查询:
string query = @"insert into EMPLOYEE_TABLE (ID,UserName,Password) VALUES (@id, @uname, @pass)";
答案 1 :(得分:1)
看起来你的字符串中有一些无关的引号。当你使用参数时,根本没有理由在字符串中引用引号 - 在这样的情况下绝对需要参数,因为有一天你会聘请一些爱尔兰人。 (非参数化版本将在O'Neill先生身上崩溃并烧毁。)
使用调试器并查看ExecuteNonQuery()之前的命令文本。
答案 2 :(得分:0)
看起来你正试图将variables传递给命令,但是你没有在sql语句中声明它们。
尝试这样做:
string query = @"insert into EMPLOYEE_TABLE (ID,UserName,Password) VALUES (@id, @uname, @pass)";
答案 3 :(得分:0)
看到你已经参数化了你的命令(干得好!),接下来你应该开始编写参数化查询:
string query = @“insert into EMPLOYEE_TABLE(ID,UserName,Password)VALUES('@ id','@ uname','@ pass')”;确保使用Apstrophe的
来填充VARCHAR值