ติดปัญหาอะไรครับ การ Insert ใช้ผ่าน ADO.NET กับ NameSpace ของ SqlClient สามารถดูได้จากบทความของ ASP.NET กับ SQL Server ครับ
Date :
2012-10-15 09:16:28
By :
mr.win
No. 2
Guest
Code (VB.NET)
Dim Conn As OleDbConnection
Dim cmd As OleDbCommand
Dim sql as string
Conn = New OleDbConnection("Provider=sqloledb;server=" & SVName & ";database=" & DBName & ";uid=" & UID & ";pwd=")
Conn.Open()
sql = "insert into tablename(id,name) select (select isnull(max(id),0) + 1 from tablename),me.txtname.text"
sql = "update tablename set name='xxx' where id="& me.txtid.text
sql = "delete tablename where id="& me.txtid.text
Try
cmd = New OleDbCommand(Sql, Conn)
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("ets_mc_plan Insert Data Incomplete as" & ex.Message, "Insert Data Error !!!", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End Try
Public Sub loaddata()
Dim sqlQuery As String = "SELECT * FROM sj_id"
Dim sqlAdapter As New SqlDataAdapter
Dim sqlcommand As New SqlCommand
Dim TABLE As New DataTable
Dim i As Integer
With sqlcommand
.CommandText = sqlQuery
.Connection = sconnection
End With
With sqlAdapter
.SelectCommand = sqlcommand
.Fill(TABLE)
End With
ListView1.Items.Clear()
For i = 0 To TABLE.Rows.Count - 1
With ListView1
.Items.Add(TABLE.Rows(i)("sj_id"))
With .Items(.Items.Count - 1).SubItems
.Add(TABLE.Rows(i)("sj_id"))
.Add(TABLE.Rows(i)("sj_name"))
.Add(TABLE.Rows(i)("unit"))
End With
End With
Next
End Sub
ช่วยดูโค้ดให้หน่อยคับ เออเร่อ ตรง .Fill(TABLE)
Date :
2012-12-18 13:34:05
By :
pirasak
No. 14
Guest
รบกวนดูโค้ดให้หน่อยค่ะไม่รู้ติดอะไรค่ะ พอกดเพิ่มข้อมูลแล้วมันขึ้น 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 & "')"
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)
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
Try
If txtCusID.Text = "" Then
MessageBox.Show("Please Select Data", "Update", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtCusID.Focus()
Exit Sub
End If
cm = New OleDbCommand("UPDATE tbFile SET FileNo=@FileNo,Date=@Date,CustomerID=@CustomerID,CustomerName=@CustomerName,TypeName=@TypeName,FileID=@FileID,FileName=@FileName,FilePath1=@FilePath1,FilePath2=@FilePath2,FilePath3=@FilePath3,Remark=@Remark Where FileNo=@FileNo", conn)
cm.Parameters.AddWithValue("@FileNo", txtFileNo.Text)
cm.Parameters.AddWithValue("@Date", Format(CDate(dtpDate.Text), "yyyy-MM-dd"))
cm.Parameters.AddWithValue("@CustomerID", txtCusID.Text)
cm.Parameters.AddWithValue("@CustomerName", txtCusName.Text)
cm.Parameters.AddWithValue("@TypeName", cbType.Text)
cm.Parameters.AddWithValue("@FileID", txtFileID.Text)
cm.Parameters.AddWithValue("@FileName", txtFileName.Text)
cm.Parameters.AddWithValue("@FilePath1", txtSave1.Text)
cm.Parameters.AddWithValue("@FilePath2", txtSave2.Text)
cm.Parameters.AddWithValue("@FilePath3", txtSave3.Text)
cm.Parameters.AddWithValue("@Remark", txtRemark.Text)
cm.ExecuteNonQuery()
MessageBox.Show("Update successful", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information)
ShowFile()
AutoFileNo()
AllClear()
Catch ex As Exception
End Try
End Sub