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

으로 VARCHAR 값을 넣었는지 확인하십시오. 왜 삽입 문이 잘못 되었습니까? 쿼리의 값을 @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

것 같습니다. 매개 변수를 사용하면 문자열에 인용 부호를 사용할 이유가 없습니다. 언젠가 아일랜드 인을 고용 할 것이기 때문에 이런 상황에서는 매개 변수가 절대적으로 필요합니다. (파라미터 화되지 않은 버전은 O'Neill 씨에게 충돌하고 화상을 입습니다.)

디버거를 사용하고 ExecuteNonQuery() 직전의 명령 텍스트를 살펴보십시오.

0

당신이 당신의 명령을 매개 변수화 한 것처럼, 당신은 아마 다음 매개 변수가있는 쿼리 작성 시작해야보고 (좋은 일을!) : EMPLOYEE_TABLE (ID, 사용자 이름, 암호) 가치에 삽입 "@ =

문자열 쿼리를 ('@id', '@uname', '@pass') "; Apstrophe의

관련 문제