2011-02-23 184 views
0

这是我试图使用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

相提并论,但没有说明为什么insert语句错误?我敢肯定你需要用@id,@uname,@pass – 2011-02-23 01:03:52

+0

替换查询中的值@shoban你能帮我解决这个编码中的错误吗? – Beginner 2011-02-23 01:04:45

+0

@Austin这是INSERT INTO语句中的“语法错误”错误。 – Beginner 2011-02-23 01:05:57

回答

1

看到好像你已经参数的命令,你应该在下写一个参数化查询开始(良好的工作!):

string query = @"insert into EMPLOYEE_TABLE (ID,UserName,Password) VALUES (@id, @uname, @pass)"; 
+0

我对MS Access查询语法不再了解太多,但是您是否使用某些测试了针对数据库的查询其他查询工具? – Reddog 2011-02-23 01:17:49

0

它看起来像你试图通过variables到命令中,但是你没有在sql语句中声明它们。

尝试这样做:

string query = @"insert into EMPLOYEE_TABLE (ID,UserName,Password) VALUES (@id, @uname, @pass)"; 
1

它看起来像你有你的字符串一些多余的引号。当你使用参数时,根本就没有理由在字符串中加上引号 - 在这种情况下绝对需要参数,因为有一天你会雇用一些爱尔兰人。 (非参数化版本会在O'Neill先生上崩溃并烧毁。)

使用调试器并查看ExecuteNonQuery()之前的命令文本。

0

看到好像你已经参数的命令,你应该在下写一个参数化查询开始(良好的工作!):

查询字符串= @“插入EMPLOYEE_TABLE(ID,用户名,密码)VALUES ('@id','@uname','@pass')“;请确保您已将VARCHAR值与Apstrophe的

相关问题