Làm một form cập nhật trong VB.Net, sử dụng Lưới Listview, dùng CSDL Access
-
- Dim str_cn As String
- Dim strSql As String
- Try
- ' khoi tao doi tuong OleDbConnection
- str_cn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & System.Windows.Forms.Application.StartupPath() & " \qlGiaoVien.mdb "
- Dim Con_nn As New OleDbConnection(str_cn)
-
- ' Khai bao va khoi tao doi tuong OleDbCommand
- strSql = " select * from tbKhoa "
- Dim cmdC As OleDbCommand = New OleDbCommand(strSql, Con_nn)
- Con_nn.Open()
- Catch ex As Exception
- MsgBox(ex.Message)
- End Try
-
Với đoạn code như trên mà không kết nối với dữ liệu được thì có lẽ bạn
chưa chép tập tin glGiaoVien.mdb vào thư mục bin\debug của project.
Lưu ý: Application.StartupPath sẽ trả lại chuỗi đường dẫn đến thư mục bin\debug
Dim str_cn As String
Dim strSql As String
Try
' khoi tao doi tuong OleDbConnection
str_cn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & System.Windows.Forms.Application.StartupPath() & " \qlGiaoVien.mdb "
Dim Con_nn As New OleDbConnection(str_cn)
Con_nn.Open() 'Dòng này phải có trước OleDBCommand và / hoặc OleDbDataAdapter
' Khai bao va khoi tao doi tuong OleDbCommand
strSql = " select * from tbKhoa "
Dim daC As OleDbDataAdapter = New OleDbDataAdapter(strSql, Con_nn)
Dim dtC As New DataTable
daC.Fill(dtC)
Catch ex As Exception
MsgBox(ex.Message)
End Try
biết gán cho combobox thì sẽ biết đối với listbox
-
- Sub FillListBox(ByVal lst as ListBox,ByVal sql as String, ByVal s1 as String, ByVal s2 as String)
- Dim ds as DataSet=new DataSet()
- Dim da as SqlDataAdapter=new SQLDataAdapter(sql,con)
- da.Fill(ds)
- lst.DataSource=ds.Table(0)
- lst.DisplayMember=s1
- lst.ValueMember=s2
- End Sub
---------------
-
- Sub connect()
- Try
- 'vô http://connectionstrings.com tham khảo thêm
- con=New SqlConnection("Data Source=trang;Initial Catalog=Center_Library;Integrated Security=True")
- con.Open()
- Catch ex as SqlException
- Messagebox.Show("Không kết nối đc","Thông báo",MessageBoxButtons.OK,MessageBoxIcon.Error)
- End Try
- End Sub
On Error GoTo ErrTrap
myAccessConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Application.StartupPath & "\CSDL\Data.mdb" & ";Persist Security Info=False ;Jet OLEDB:Database Password=nguyenngocbaotran")
myAccessConnection.Open()
dbCmd.Connection = myAccessConnection
dbReader = dbCmd.ExecuteReader(CommandBehavior.SingleResult)
b = False
Do While (dbReader.Read())
On Error Resume Next ' Các số từ 0 đến 6 tương ứng với các trường dữ liệu trong DataBase
LName.Items.Add(dbReader.GetString(0))
LMota.Items.Add(dbReader.GetString(1))
LKey.Items.Add(dbReader.GetString(2))
LWind.Items.Add(dbReader.GetString(3))
LKieu.Items.Add(dbReader.GetString(4))
LValue.Items.Add(dbReader.GetString(5))
LNote.Items.Add(dbReader.GetString(6))
Loop
0 nhận xét Blogger 0 Facebook