2014-11-12 5 views
-1
If String.IsNullOrWhiteSpace(txtadmin.Text) Or String.IsNullOrWhiteSpace(txtname.Text) Or String.IsNullOrWhiteSpace(txtcourse.Text) Or String.IsNullOrWhiteSpace(txtic.Text) Or String.IsNullOrWhiteSpace(txtgender.Text) Or String.IsNullOrWhiteSpace(txtaddress.Text) Or String.IsNullOrWhiteSpace(txtbirth.Text) Or String.IsNullOrWhiteSpace(txttel.Text) Or String.IsNullOrWhiteSpace(txtemail.Text) Or String.IsNullOrWhiteSpace(txttpye.Text) Then 
     MessageBox.Show("Please complete the on the box.", "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
     Exit Sub 
    End If 

    Dim Conn As System.Data.OleDb.OleDbConnection 
    Dim ConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\temp\Database1.accdb" 
    Conn = New System.Data.OleDb.OleDbConnection(ConnectionString) 


    Try 
    If Conn.State = ConnectionState.Open Then Conn.Close() 
    Conn.Open()  

Dim sql As String = "INSERT INTO tbl_info ([AdminNo],UserName, [Course Title], ICNo, Gender, Address, [Data of Birth], TelNo, Email, Type) values ('" & txtname.Text & "', '" & txtadmin.Text & "', '" & txtcourse.Text & "', '" & txtic.Text & "', '" & txtgender.Text & "', '" & txtaddress.Text & "','" & txtbirth.Text & "', '" & txttel.Text & "', '" & txtemail.Text & "', '" & txttpye.Text & "')" 
Dim sqlCom As New System.Data.OleDb.OleDbCommand(sql) 
sqlCom.Connection = Conn 

Dim result As Integer = sqlCom.ExecuteNonQuery 

sqlCom.Dispose() 
Conn.Close() 

이 라인에서 "구문 오류가 쿼리 식 (누락 오류)"233 sangkeng 번째 # 6-12 (s'451233 ') ""오류가 :구문 오류 (누락 오류가)

Dim result As Integer = sqlCom.ExecuteNonQuery 

하지만 주소에 난 어쨌든 ggghh처럼 그것이

If result > 0 Then 
     MessageBox.Show("Successfully created.") 
    Else 
     MessageBox.Show("Failure to create.") 
    End If 

    txtname.Text = "" 
    txtadmin.Text = "" 
    txtcourse.Text = "" 
    txtic.Text = "" 
    txtgender.Text = "" 
    txtaddress.Text = "" 
    txtbirth.Text = "" 
    txttel.Text = "" 
    txtemail.Text = "" 
    txttpye.Text = "" 
    txtadmin.Focus() 



    'Catch ex As Exception 
    'MessageBox.Show("Failed to connect to Database..", "Database Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
    'End Try 
+0

질문을 올바르게 형식화하십시오. –

+1

문자열 연결을 사용하여 SQL 코드에 값을 삽입하지 마십시오. 항상 매개 변수를 사용하십시오. 그렇게하면 문제가 해결됩니다. http://jmcilhinney.blogspot.com.au/2009/08/using-parameters-in-adonet.html – jmcilhinney

답변

2

당신은 당신의 데이터에 작은 따옴표를 고려하지 않은 얻을 수있는 물품. 문자열 concatentation을 사용하여 쿼리에 필드를 포함하지 마십시오! 이렇게하면 오류가 발생하지 않을뿐만 아니라 심각한 보안 문제가 발생합니다.

Dim Result As Integer 
Dim sql As String = "INSERT INTO tbl_info ([AdminNo],UserName, [Course Title], ICNo, Gender, Address, [Data of Birth], TelNo, Email, Type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" 

Using conn As New OleDbConnection("connection string here"), _ 
     cmd As New OleDbCommand(sql, conn) 

    'I'm guessing at the DB types here. Use the actual types from your database. 
    cmd.Parameters.Add("AdminNo", OleDbType.VarChar, 5).Value = txtadmin.Text 
    cmd.Parameters.Add("UserName", OleDbType.VarChar, 12).Value = txtname.Text 
    cmd.Parameters.Add("Course Title", OleDbType.VarChar, 20).Value = txtcourse.Text 
    cmd.Parameters.Add("ICNo", OleDbType.VarChar, 5).Value = txtic.Text 
    cmd.Parameters.Add("Gender", OleDbType.Char, 1).Value = txtgender.Text 
    cmd.Parameters.Add("Address", OleDbType.VarChar, 40).Value = txtaddress.Text 
    cmd.Parameters.Add("Date of Birth", OleDbType.DateTime).Value = CDate(txtbirth.Text) 
    cmd.Parameters.Add("TelNo", OleDbType.VarChar, 12).Value = txttel.Text 
    cmd.Parameters.Add("Email", OleDbType.VarChar, 30).Value = txtemail.Text 
    cmd.Parameters.Add("Type", OleDbType.VarChar, 5).Value = txttpye.Text 

    conn.Open() 
    result = cmd.ExecuteNonquery() 
End Using 

사이드 노트 : 몇 곳에서 전화) 정말 지금까지 공백으로 열 이름을 사용하지 않으며, 그래서 내가 Parameters.Add (에 첫 번째 인수에 대한 형식 권리가 확실하지 않다 위. 이 상황에서 이름과 대괄호를 사용해야 할 수도 있습니다.

+0

나는이 주제에 대한 이전 게시물을 참조하는 답변을 게시하려하고있었습니다. –

+0

나는이 점에 관해서 썼습니다 - 그것은 흔한 (그리고 매우 심각한) 문제입니다 - 그래서 그것을 많이 좁히지는 않습니다. 링크를 게시 해 보시지 않겠습니까? –

+0

미안, 내가 의미했던 것은 명성이었다. 나는이 게시물에 답하기위한 참조로 다른 게시물에서 이전 답변 중 하나를 사용하려고했으나 나를 괴롭혔다. –

관련 문제