2016-12-21 1 views
-4

VB.NET에서 텍스트 상자의 데이터를 Access 데이터베이스에 입력하는 프로그램이 있습니다. 다음은 샘플 이미지입니다 :VB 2010 오류 (INSERT INTO) 구문 오류

enter image description here

이것은 내가 사용하고있는 코드는 그것은 나에게 오류를 제공합니다

m = TextBox1.Text 
b = "'" + TextBox2.Text + "'" 
x = "'" + TextBox3.Text + "'" 
d = TextBox4.Text 
n = "'" + TextBox5.Text + "'" 
Dim s2 As String 
s2 = "insert into users1 (num , name1 , pass , add , phone) " & " values (" + m + " , " + n + " , " + b + " , " + x + " , " + d + ") " 
Dim cmd2 As New OleDbCommand(s2, con) 
cmd2.ExecuteNonQuery() 

답변

1

당신의 SQL이 실패하는 이유는 "추가하기"예약어이다 . 즉, 대괄호 ([]) 안에 넣지 않고서는 사용할 수 없습니다. 데이터 유형을 삽입하려고 일치하지 않는 경우 텍스트를 삽입하려고, 그래서 만약 당신이 ...

Using oleCmd As New OleDbCommand("Insert Into users1 (num,name1,pass,[add],phone) values (@num,@name1,@pass,@add,@phone)", con) 
    oleCmd.Parameters.Add("@num", OleDbType.Integer).Value = Textbox1.Text 
    oleCmd.Parameters.Add("@name1", OleDbType.VarChar).Value = Textbox2.Text 
    oleCmd.Parameters.Add("@pass", OleDbType.VarChar).Value = Textbox3.Text 
    oleCmd.Parameters.Add("@address", OleDbType.VarChar).Value = Textbox4.Text 
    oleCmd.Parameters.Add("@phone", OleDbType.Integer).Value = Textbox5.Text 
    oleCmd.ExecuteNonQuery() 
End Using 

Using oleCmd As New OleDbCommand("Insert Into users1 (num,name1,pass,[add],phone) Values (?,?,?,?,?)", con) 
    oleCmd.Parameters.AddWithValue("?", Textbox1.Text) 
    oleCmd.Parameters.AddWithValue("?", Textbox2.Text) 
    oleCmd.Parameters.AddWithValue("?", Textbox3.Text) 
    oleCmd.Parameters.AddWithValue("?", Textbox4.Text) 
    oleCmd.Parameters.AddWithValue("?", Textbox5.Text) 
    oleCmd.ExecuteNonQuery() 
End Using 

주 쿼리의 파라미터를, 그래서 작동이 중 하나를해야 이상, 그것은 실패합니다 전화 번호 또는 전화 번호는 실패합니다. 입력 내용의 유효성을 검사하고 텍스트 상자 텍스트를 사용하는 대신 변환하는 것이 바람직합니다.

관련 문제