|
|
|
C# ช่วยดูทีคับ insert เข้าฐานข้อมูลไม่ได้ครับ Syntax error in INSERT INTO statement. |
|
|
|
|
|
|
|
มึนไปหมดแล้วววมันขึ้นอันนี้อะคับ Syntax error in INSERT INTO statement. ช่วยหน่อยนะครับ
Code (C#)
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 frmMain
{
public partial class Form2 : Form
{
OleDbConnection myConn = new OleDbConnection();
public Form2()
{
InitializeComponent();
}
private void Register_Load(object sender, EventArgs e)
{
myConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\Database.mdb";
myConn.Open();
}
public void GetData(string str)
{
label1.Text = str;
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
OleDbCommand mycommand = new OleDbCommand();
OleDbTransaction myTransaction;
OleDbParameter Name = new OleDbParameter();
Name.ParameterName = "@N";
Name.OleDbType = OleDbType.Char;
Name.Direction = ParameterDirection.Input;
Name.Value = textBox1.Text;
OleDbParameter Sarname = new OleDbParameter();
Sarname.ParameterName = "@S";
Sarname.OleDbType = OleDbType.Char;
Sarname.Direction = ParameterDirection.Input;
Sarname.Value = textBox2.Text;
OleDbParameter Email = new OleDbParameter();
Email.ParameterName = "@E";
Email.OleDbType = OleDbType.Char;
Email.Direction = ParameterDirection.Input;
Email.Value = textBox6.Text;
OleDbParameter Tel = new OleDbParameter();
Tel.ParameterName = "@T";
Tel.OleDbType = OleDbType.Char;
Tel.Direction = ParameterDirection.Input;
Tel.Value = textBox7.Text;
OleDbParameter Username = new OleDbParameter();
Username.ParameterName = "@U";
Username.OleDbType = OleDbType.Char;
Username.Direction = ParameterDirection.Input;
Username.Value = textBox3.Text;
OleDbParameter Password = new OleDbParameter();
Password.ParameterName = "@P";
Password.OleDbType = OleDbType.Char;
Password.Direction = ParameterDirection.Input;
Password.Value = textBox4.Text;
myTransaction = myConn.BeginTransaction();
mycommand.CommandText = "INSERT INTO [member](Username,Password,Name,Sarname,Tel,Email)VALUES(@U,@P,@N,@S,@T,@E)";
mycommand.CommandType = CommandType.Text;
mycommand.Transaction = myTransaction;
mycommand.Connection = myConn;
mycommand.Parameters.Add(Username);
mycommand.Parameters.Add(Password);
mycommand.Parameters.Add(Name);
mycommand.Parameters.Add(Sarname);
mycommand.Parameters.Add(Tel);
mycommand.Parameters.Add(Email);
mycommand.ExecuteNonQuery();
myTransaction.Commit();
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox6.Text = "";
textBox7.Text = "";
}
}
}
Tag : C#
|
|
|
|
|
|
Date :
2012-10-11 17:23:10 |
By :
white31969 |
View :
1325 |
Reply :
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (C#)
myTransaction = myConn.BeginTransaction();
OleDbParameter[] pr;
mycommand.CommandText = "INSERT INTO [member](Username,Password,Name,Sarname,Tel,Email)VALUES(@U,@P,@N,@S,@T,@E)";
pr = new OleDbParameter[6];
pr[0] = new OleDbParameter("@N",OleDbType.Varchar);
pr[0].Value = TextBox1.Text;
pr[1] = new OleDbParameter("@S",OleDbType.Varchar);
pr[1].Value = TextBox2.Text;
pr[2] = new OleDbParameter("@E",OleDbType.Varchar);
pr[2].Value = TextBox3.Text;
pr[3] = new OleDbParameter("@T",OleDbType.Varchar);
pr[3].Value = TextBox4.Text;
pr[4] = new OleDbParameter("@U",OleDbType.Varchar);
pr[4].Value = TextBox5.Text;
pr[5] = new OleDbParameter("@P",OleDbType.Varchar);
pr[5].Value = TextBox6.Text;
mycommand.CommandType = CommandType.Text;
mycommand.Transaction = myTransaction;
mycommand.Connection = myConn;
mycommand.Parameters.AddRange(pr);
mycommand.ExecuteNonQuery();
myTransaction.Commit();
|
ประวัติการแก้ไข 2012-10-12 17:41:17
|
|
|
|
Date :
2012-10-11 18:00:31 |
By :
lee_latee |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ไม่เข้าใจอะครับ @_@ ต้องเอาไว้ตรงไหน ผมลองเอาไว้แทนที่มันก็ Error ตรงสีแดงครับ
Code
myTransaction = myConn.BeginTransaction();
OleDbParameter[] pr;
mycommand.CommandText = "INSERT INTO [member](Username,Password,Name,Sarname,Tel,Email)VALUES(@U,@P,@N,@S,@T,@E)";
pr = new OleDbParameter[6];
pr[0] = new OleDbParameter("@U",OleDbType.Varchar);
pr[0].Value = TextBox1.Text;
pr[1] = new OleDbParameter("@P",OleDbType.Varchar);
pr[1].Value = TextBox2.Text;
pr[2] = new OleDbParameter("@N",OleDbType.Varchar);
pr[2].Value = TextBox3.Text;
pr[3] = new OleDbParameter("@S",OleDbType.Varchar);
pr[3].Value = TextBox4.Text;
pr[4] = new OleDbParameter("@T",OleDbType.Varchar);
pr[4].Value = TextBox5.Text;
pr[5] = new OleDbParameter("@E",OleDbType.Varchar);
pr[5].Value = TextBox6.Text;
mycommand.CommandType = CommandType.Text;
mycommand.Transaction = myTransaction;
mycommand.Connection = myConn;
mycommand.Parameters.AddRange(pr);
mycommand.ExecuteNonQuery();
myTransaction.Commit();
|
|
|
|
|
Date :
2012-10-11 18:16:33 |
By :
white |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตัวอย่างครับ
Code (C#)
//*** Insert to Database ***'
OleDbConnection objConn = new OleDbConnection();
string strConnString = null;
string strSQL = null;
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
Server.MapPath("App_Data/mydatabase.mdb")+";Jet OLEDB:Database Password=;";
strSQL = "INSERT INTO files (Name,FilesName,FilesType) VALUES (@sName,@sFilesName,@sFilesType)";
objConn.ConnectionString = strConnString;
objConn.Open();
OleDbCommand objCmd = new OleDbCommand(strSQL, objConn);
objCmd.Parameters.Add("@sName", OleDbType.VarChar).Value = this.txtName.Text;
objCmd.Parameters.Add("@sFilesName", OleDbType.Binary).Value = imbByte;
objCmd.Parameters.Add("@sFilesType", OleDbType.VarChar).Value = strMIME;
objCmd.ExecuteNonQuery();
objConn.Close();
objConn = null;
|
|
|
|
|
Date :
2012-10-12 06:22:13 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
คือตอนแรกผมใช้โค๊ดนี้แหละครับ แต่ว่าแอดแค่ usernameกับpassword มันแอดได้ครับแต่พอมาเพิ่มมันก็แอดไม่ได้แล้วไม่เข้าใจเลย Y_Y
|
|
|
|
|
Date :
2012-10-12 14:05:03 |
By :
white |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ผมลืมบอก งั้นก็ลอง ปรับ ตามนี้ครับ
Code (C#)
private void button1_Click(object sender, EventArgs e)
{
OleDbCommand mycommand = new OleDbCommand();
OleDbTransaction myTransaction;
myTransaction = myConn.BeginTransaction();
OleDbParameter[] pr;
try
{
mycommand.CommandText = "INSERT INTO [member](Username,Password,Name,Sarname,Tel,Email)VALUES(@U,@P,@N,@S,@T,@E)";
pr = new OleDbParameter[6];
pr[0] = new OleDbParameter("@N",OleDbType.Varchar);
pr[0].Value = textBox1.Text;
pr[1] = new OleDbParameter("@S",OleDbType.Varchar);
pr[1].Value = textBox2.Text;
pr[2] = new OleDbParameter("@E",OleDbType.Varchar);
pr[2].Value = textBox6.Text;
pr[3] = new OleDbParameter("@T",OleDbType.Varchar);
pr[3].Value = textBox7.Text;
pr[4] = new OleDbParameter("@U",OleDbType.Varchar);
pr[4].Value = TextBox3.Text;
pr[5] = new OleDbParameter("@P",OleDbType.Varchar);
pr[5].Value = textBox4.Text;
mycommand.CommandType = CommandType.Text;
mycommand.Transaction = myTransaction;
mycommand.Connection = myConn;
mycommand.Parameters.AddRange(pr);
mycommand.ExecuteNonQuery();
myTransaction.Commit();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
myTransaction.Rollback();
}
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox6.Text = "";
textBox7.Text = "";
}
ส่วน OleDbType.Varchar นั้น เป็น Data Type ของ ฟิวด์ ในเทเบิล ครับ
|
ประวัติการแก้ไข 2012-10-12 17:39:16 2012-10-12 17:52:48 2012-10-12 17:53:45
|
|
|
|
Date :
2012-10-12 17:38:08 |
By :
lee_latee |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
รันผ่านครับแต่พอแอดข้อมูลมันเป็นแบบนี้
|
|
|
|
|
Date :
2012-10-12 20:41:59 |
By :
white31969 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (C#)
string sql = "INSERT INTO Car ([Car_ID],[Car_Band],[Car_Model],[Car_Color],[Car_Year],[Cus_ID]) values('" + txtAddID.Text + "','" + txtAddBand.Text + "','" + txtAddModel.Text + "','" + cmbAddColor.Text + "','" + txtAddYear.Text + "','" + cmbCusID.Text + "')";
ใส่ชื่อฟิลด์ใน dbms ให้ถูก ส่วนหลังคำว่า Values ก็ใส่ textbox ตรงตามที่เราจะเพิ่มข้อมูลโดนเรียงลำดับเหมือนกัน สังเกตุเอานะครับ Car_ID อันแรก txtAddID.text ก็อันแรกเหมือนกัน ลองสังเกตุนะครับ
"แนะนำการใส่ชื่อฟิลด์ ควรจะมี [] นำหน้า ไม่อย่างนั้นบางชื่ออาจจะติดคำสงวนนะครับ"
|
|
|
|
|
Date :
2012-10-14 01:10:03 |
By :
peteyothin |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณมากๆคับทุกคนเดี๋ยวจะลองเอาไปทำ
|
|
|
|
|
Date :
2012-10-14 08:04:33 |
By :
white31969 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ได้แล้วๆๆๆๆๆๆๆๆๆ
|
|
|
|
|
Date :
2012-10-14 08:12:07 |
By :
white31969 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|