여기가 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);
}
}
}
}
으로 VARCHAR 값을 넣었는지 확인하십시오. 왜 삽입 문이 잘못 되었습니까? 쿼리의 값을 @id, @uname, @pass로 대체해야한다고 확신합니다. –
@shoban이 코딩에서 잘못된 점을 알려주십시오. – Beginner
@Austin 이것은 "INSERT INTO 문에서 구문 오류"오류입니다. – Beginner