Dim ConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Sever.MapPath("dbFile.accdb") & ";Jet OLEDB:Database Password=xxxxxx"
Dim Connection As OleDbConnection = New OleDbConnection(ConnectionString)
Insert Command
Dim InsertCommandString As String = "Insert Into [MyTable] ([ID], [MyData]) Values (@ID, @Data)"
Dim InsertCommand As OleDbCommand = New OleDbCommand(InsertCommandString, Connection)
InsertCommand.Parameters.Add("@ID", OleDbType.Integer).Value = 1
InsertCommand.Parameters.Add("@Data", OleDbType.VarChar).Value = "Insert Insert"
Connection.Open()
InsertCommand.ExecuteNonQuery()
Connection.Close()
Update Command
Dim UpdateCommandString As String = "Update [MyTable] Set [MyData]=@Data Where [ID]=@ID"
Dim UpdateCommand As OleDbCommand = New OleDbCommand(UpdateCommandString, Connection)
UpdateCommand.Parameters.Add("@ID", OleDbType.Integer).Value = 1
UpdateCommand.Parameters.Add("@Data", OleDbType.VarChar).Value = "Update Update"
Connection.Open()
UpdateCommand.ExecuteNonQuery()
Connection.Close()
Delete Command
Dim DeleteCommandString As String = "Delete From [MyTable] Where [ID]=@ID"
Dim DeleteCommand As OleDbCommand = New OleDbCommand(DeleteCommandString, Connection)
DeleteCommand.Parameters.Add("@ID", OleDbType.Integer).Value = 1
Connection.Open()
DeleteCommand.ExecuteNonQuery()
Connection.Close()
แถม Select Command
Dim SelectCommandString As String = "Select [ID], [MyData] From [MyTable] Where [ID]=@ID"
Dim SelectCommand As OleDbCommand = New OleDbCommand(SelectCommandString, Connection)
SelectCommand.Parameters.Add("@ID", OleDbType.Integer).Value = 1
Dim Dt As DataTable = New DataTable()
Dim Adapter As OleDbAdapter = New OleDbAdapter(SelectCommand)
Adapter.Fill(Dt)
Imports System.Data.OleDb
Imports System.Data
Public Class Form1
Public con As New OleDbConnection
Public Sub connectData() Handles MyBase.Load
Dim s As String
If con.State = ConnectionState.Open Then con.Close()
s = "Provider = Microsoft.Jet.OLEDB.4.0;"
s = s + "Data Source = C:\ProjectVB Data\Data1.mdb;"
s = s + "User Id=;Password=;"
con.ConnectionString = s
con.Open()
End Sub
Public Sub FormLoad() Handles MyBase.Load
connectData()
MessageBox.Show("เชื่อมต่อฐานข้อมูลสำเร็จ", "welcome")
End Sub
Public Function ExecuteReader(ByVal prmSQL As String) As OleDbDataReader
Dim dr As OleDbDataReader
Dim com As New OleDbCommand
connectData()
com.CommandType = CommandType.Text
com.CommandText = prmSQL
com.Connection = con
dr = com.ExecuteReader
Return dr
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dr As OleDbDataReader
Dim ID, SQL As String
ID = InputBox("ป้อนรหัสที่ต้องการค้นหา")
SQL = "Select * From data1 Where ID = '" & ID & "'"
dr = ExecuteReader(SQL)
dr.Read()
TextBox1.Text = dr.Item("ID").ToString
TextBox2.Text = dr.Item("Pname").ToString
TextBox3.Text = dr.Item("Fname").ToString
TextBox4.Text = dr.Item("LName").ToString
ComboBox1.Text = dr.Item("Age").ToString
TextBox6.Text = dr.Item("Email").ToString
TextBox7.Text = dr.Item("Phone").ToString
TextBox8.Text = dr.Item("Add").ToString
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim sql As String
sql = "INSERT INTO Data1(ID,Pname,Fname,Lname,Age.Email,Phone,Add )VALUES ("
sql += "'" & TextBox1.Text & "',"
sql += "'" & TextBox2.Text & "',"
sql += "'" & TextBox3.Text & "',"
sql += "'" & TextBox4.Text & "',"
sql += "'" & ComboBox1.Text & "',"
sql += "'" & TextBox6.Text & "',"
sql += "'" & TextBox7.Text & "',"
sql += "'" & TextBox8.Text & "'"
sql += ")"
ExecuteReader(sql)
MessageBox.Show("เพิ่มข้อมูลสำเร็จ", "ยืนยัน")
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim sql As String
sql = "UPDATE Data1 SET "
sql += "ID='" & TextBox1.Text & "',"
sql += "Pname='" & TextBox2.Text & "',"
sql += "FName='" & TextBox3.Text & "',"
sql += "LName='" & TextBox4.Text & "',"
sql += "Age='" & ComboBox1.Text & "',"
sql += "Email='" & TextBox6.Text & "',"
sql += "Phone='" & TextBox7.Text & "',"
sql += "Add='" & TextBox8.Text & "',"
sql += " WHERE ID='" & TextBox1.Text & "' "
ExecuteReader(sql)
MessageBox.Show("แก้ไขข้อมูลสำเร็จ", "ยืนยัน")
End Sub
End Class
Date :
2010-11-17 09:03:02
By :
Dragons_first
No. 5
Guest
^
^
โค้ดโบราณมาก มาอัฟเดตให้แล้วกัน
Code (VB.NET)
Imports System.Data.OleDb
Imports System.Data
Imports System.Text
Public Class Form1
Public con As New OleDbConnection
Public Sub connectData()
Dim s As New StringBuilder()
s.Append("Provider = Microsoft.Jet.OLEDB.4.0;")
s.Append("Data Source = C:\ProjectVB Data\Data1.mdb;")
s.Append("User Id=;Password=;")
con.ConnectionString = s.ToString()
End Sub
Public Sub FormLoad() Handles MyBase.Load
connectData()
MessageBox.Show("เชื่อมต่อฐานข้อมูลสำเร็จ", "welcome")
End Sub
Public Function ExecuteQuery(ByVal comSQL As OleDbCommand) As DataTable
Dim Dt As New DataTable()
Dim Da As New OleDbDataAdapter()
Da.SelectCommand = comSQL
Da.Fill(Dt)
Return Dt
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ID As String = InputBox("ป้อนรหัสที่ต้องการค้นหา")
Dim SQL As String = "Select [ID], [Pname], [Fname], [LName], [Age], [Email], [Phone], [Add] From [data1] Where [ID] = @ID"
Dim com As New OleDbCommand()
com.CommandType = CommandType.Text
com.CommandText = SQL
com.Connection = con
com.Parameters.AddWithValue("@ID", ID)
Dim Dt As DataTable = ExecuteQuery(com)
If Dt.Rows.Count > 0 Then
Dim Dr As DataRow = Dt.Rows(0)
TextBox1.Text = Dr("ID").ToString()
TextBox2.Text = Dr("Pname").ToString()
TextBox3.Text = Dr("Fname").ToString()
TextBox4.Text = Dr("LName").ToString()
ComboBox1.Text = Dr("Age").ToString() '<--- what ???
TextBox6.Text = Dr("Email").ToString()
TextBox7.Text = Dr("Phone").ToString()
TextBox8.Text = Dr("Add").ToString()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim sql As New StringBuilder()
sql.AppendLine("INSERT INTO [Data1]")
sql.AppendLine("([ID], [Pname], [Fname], [Lname], [Age], [Email], [Phone], [Add]) VALUES (")
sql.AppendLine("@ID @Pname @Fname @Lname @Age @Email @Phone @Add)")
Dim com As New OleDbCommand()
com.CommandType = CommandType.Text
com.CommandText = sql.ToString()
com.Connection = con
com.Parameters.AddWithValue("@ID", TextBox1.Text)
com.Parameters.AddWithValue("@Pname", TextBox2.Text)
com.Parameters.AddWithValue("@Fname", TextBox3.Text)
com.Parameters.AddWithValue("@Lname", TextBox4.Text)
com.Parameters.AddWithValue("@Age", ComboBox1.Text) '<--- what ???
com.Parameters.AddWithValue("@Email", TextBox6.Text)
com.Parameters.AddWithValue("@Phone", TextBox7.Text)
com.Parameters.AddWithValue("@Add", TextBox8.Text)
con.Open()
com.ExecuteNonQuery()
con.Close()
MessageBox.Show("เพิ่มข้อมูลสำเร็จ", "ยืนยัน")
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim sql As New StringBuilder()
sql.AppendLine("UPDATE [Data1] SET "
sql.AppendLine("([ID]=@ID, [Pname]=@Pname, [Fname]=@Fname, [Lname]=@Lname, [Age]=@Age, [Email]=@Email, [Phone]=@Phone, [Add]=@Add)")
sql.AppendLine("WHERE [ID]=@ID)")
Dim com As New OleDbCommand()
com.CommandType = CommandType.Text
com.CommandText = sql.ToString()
com.Connection = con
com.Parameters.AddWithValue("@ID", TextBox1.Text)
com.Parameters.AddWithValue("@Pname", TextBox2.Text)
com.Parameters.AddWithValue("@Fname", TextBox3.Text)
com.Parameters.AddWithValue("@Lname", TextBox4.Text)
com.Parameters.AddWithValue("@Age", ComboBox1.Text) '<--- what ???
com.Parameters.AddWithValue("@Email", TextBox6.Text)
com.Parameters.AddWithValue("@Phone", TextBox7.Text)
com.Parameters.AddWithValue("@Add", TextBox8.Text)
con.Open()
com.ExecuteNonQuery()
con.Close()
MessageBox.Show("แก้ไขข้อมูลสำเร็จ", "ยืนยัน")
End Sub
End Class