2011-02-23 6 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

で入れているのは、挿入文が間違っている理由を説明していませんか?あなたは@ id、@uname、@passでクエリの値を置き換える必要があると確信しています。 –

+0

@shobanこのコーディングで何が間違っているのか教えてください。 – Beginner

+0

@Austinこれは、 "INSERT INTO文の構文エラー"というエラーです。 – Beginner

答えて

1

あなたは、コマンドパラメータきたかのように、あなたはおそらく次のパラメータ化クエリを書くことで起動する必要があります見て(良い仕事を!):

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

私はMS Accessのクエリ構文についてもう分かりませんが、いくつかを使用してDBに対してクエリをテストしましたか?他のクエリツール? – Reddog

0

それはあなたがvariablesに合格しようとしているように見えますあなたはSQL文でそれらを宣言していません。

はこれをやってみてください。

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

あなたは、文字列にいくつかの余分な引用符を持っているように見えます。パラメータを使用するときは、文字列内に引用符を付ける理由はありません。いつかは誰かをアイルランド人に雇うので、このような状況ではパラメータが絶対必要です。

デバッガを使用し、ExecuteNonQuery()の直前のコマンドテキストを見てください。

0

あなたは、コマンドパラメータきたかのように、あなたはおそらく次のパラメータ化クエリの書き込みを開始する必要があります見て(良い仕事を!):EMPLOYEE_TABLE(ID、ユーザ名、パスワード)VALUES挿入 "@ =

文字列のクエリを( '@id'、 '@uname'、 '@pass') "; VARCHARの値をApstropheの

関連する問題