Dim constr As String = "provider=sqloledb;"
constr &= "data source=ART-PC;"
constr &= "initial catalog=book;"
constr &= "integrated security=sspi"
Dim conn As New OleDbConnection(constr)
Dim cmd As New OleDbCommand
Dim ra As String
Dim sql As String = ""
Dim sid As String = txt_book_id.Text
Dim sname As String = txt_book_name.Text
conn.Open()
sql = "insert into book_properties('" & sid & "','" & sname & "')"
cmd = New OleDbCommand(sql, conn)
ra = cmd.ExecuteNonQuery
MessageBox.Show("เพิ่มข้อมูลเรียบร้อยแล้ว " & ra)
txt_book_id.Clear()
txt_book_name.Clear()
conn.Close()
รบกวนช่วยดูให้หน่อยค่ะ เพิ่มข้อมูลVB.Net มันขึ้น MessageBox ไม่สามารถเพิ่มได้เนื่องจาก Unclosed quotation mark after ther character string 'ข้อมูลที่กรอก)'. iIncorrect syntax near 'ข้อมูลที่กรอก)'.ค่ะ
Code (VB.NET)
Dim sqlEmployee As String = ""
Dim comEmployee As SqlCommand = New SqlCommand
Dim tmpMSG As String = ""
If (txtEmpID.Text = "") Or (txtEmpName.Text = "") Or (txtEmpPosition.Text = "") Or (txtEmpTel.Text = "") Or (txtUser.Text = "") Or (txtPass.Text = "") Then
MessageBox.Show("กรุณาป้อนข้อมูลสมาชิกให้ครบ !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtEmpID.Focus()
Exit Sub
End If
Try
Select Case ActionFlag
Case "Add"
sqlEmployee = "INSERT INTO Employee (EmployeeID,Emp_Name,Emp_Tel,"
sqlEmployee &= " Emp_Position, Username, Password)"
sqlEmployee &= " VALUES ('" & txtEmpID.Text & "',"
sqlEmployee &= "'" & txtEmpName.Text & "',"
If OptMale.Checked = True Then
sqlEmployee &= "'M',"
ElseIf OptFeMale.Checked = True Then
sqlEmployee &= "'F',"
End If
sqlEmployee &= "'" & txtEmpPosition.Text & "',"
sqlEmployee &= "'" & txtEmpTel.Text & "',"
sqlEmployee &= "'" & txtUser.Text & "',"
sqlEmployee &= "'" & txtPass.Text & ")"
Case "Edit"
sqlEmployee = "UPDATE Employee "
sqlEmployee &= " SET Emp_Name='" & txtEmpName.Text & "',"
If OptMale.Checked = True Then
sqlEmployee &= " Sex='M',"
ElseIf OptFeMale.Checked = True Then
sqlEmployee &= " Sex='F',"
End If
sqlEmployee &= " Emp_Position='" & txtEmpPosition.Text & "',"
sqlEmployee &= " Emp_Tel='" & txtEmpTel.Text & "',"
sqlEmployee &= " Username='" & txtUser.Text & "',"
sqlEmployee &= " Password='" & txtPass.Text & "',"
sqlEmployee &= " WHERE (EmployeeID='" & txtEmpID.Text & "')"
End Select
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
If ActionFlag = "Add" Then
tmpMSG = "คุณต้องการเพิ่มข้อมูล ใช่หรือไม่ ?"
ElseIf ActionFlag = "Edit" Then
tmpMSG = "คุณต้องการแก้ไขข้อมูล ใช่หรือไม่ ?"
End If
If MessageBox.Show(tmpMSG, "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
With comEmployee
.CommandType = CommandType.Text
.CommandText = sqlEmployee
.Connection = Conn
.ExecuteNonQuery()
End With
End If
Catch ErrProcess As Exception
MessageBox.Show("ไม่สามารถเพิ่มสมาชิกใหม่ได้ เนื่องจาก " & ErrProcess.Message, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Exit Sub
End Try
MessageBox.Show("บันทึกลงฐานข้อมูล เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
grbSearch.Enabled = True
ControlStatus(False)
btnAdd.Enabled = True
btnEdit.Enabled = False
btnOK.Visible = False
btnCancel.Visible = False
ClearData()
cmdMoveFirst.Enabled = False
cmdMovePrevious.Enabled = False
cmdMoveNext.Enabled = False
cmdMoveLast.Enabled = False
lblShowPositions.Text = ""
txtSearch.Text = ""
txtSearch.Focus()
Private Sub txtSearch_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtSearch.KeyDown
If txtSearch.Text <> "" Then
If (e.KeyCode = Keys.Enter) Then
Dim sqlEmployee As String = ""
sqlEmployee = "SELECT * FROM Employee"
If cboSearch.SelectedIndex = 0 Then
sqlEmployee &= " WHERE (EmployeeID='" & txtSearch.Text & "')"
ElseIf cboSearch.SelectedIndex = 1 Then
sqlEmployee &= " WHERE (Emp_Name LIKE '%" & txtSearch.Text & "%')"
End If
sqlEmployee &= " ORDER BY EmployeeID"
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
If IsFind = True Then
ds.Tables("Employee").Clear()
End If
da.SelectCommand.CommandText = sqlEmployee
da.Fill(ds, "Employee")
If ds.Tables("Employee").Rows.Count <> 0 Then
IsFind = True
ClearBinding()
lblEmpID.DataBindings.Add("Text", ds, "Employee.EmployeeID")
txtEmpNumber.DataBindings.Add("Text", ds, "Employee.Emp_Number")
txtEmpName.DataBindings.Add("Text", ds, "Employee.Emp_Name")
txtEmpTel.DataBindings.Add("Text", ds, "Employee.Emp_Tel")
txtEmpTel.DataBindings.Add("Text", ds, "Employee.Emp_Sex")
txtEmpPosition.DataBindings.Add("Text", ds, "Employee.Emp_Position")
txtUser.DataBindings.Add("Text", ds, "Employee.Username")
txtPass.DataBindings.Add("Text", ds, "Employee.Password")
dt = ds.Tables("Employee")
ShowPosition()
btnAdd.Enabled = False
btnEdit.Enabled = True
cmdMoveFirst.Enabled = True
cmdMovePrevious.Enabled = True
cmdMoveNext.Enabled = True
cmdMoveLast.Enabled = True
btnEdit.Focus()
Else
IsFind = False
MessageBox.Show("รายชื่อเจ้าหน้าที่คุณระบุ ไม่มี !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
lblShowPositions.Text = ""
btnAdd.Enabled = True
btnEdit.Enabled = False
cmdMoveFirst.Enabled = False
cmdMovePrevious.Enabled = False
cmdMoveNext.Enabled = False
cmdMoveLast.Enabled = False
txtSearch.Focus()
txtSearch.SelectAll()
Exit Sub
End If
End If
End If
End Sub