combo控件与数据库
我有一个SQL数据库CMSystem,里面有一个部门表,表里的字段为ID,部门名称,我想把部门名称加到一个combo控件中,怎么做呀,我主要是不会和数据库的连接,怎样在在打开的数据里,从指定的表里将数据读出来.
Dim dbConn As ADODB.Connection
Dim Rst As ADODB.Recordset
Dim conString As String
conString = "Provider=sqloledb;Data Source=127.0.0.1;Initial Catalog =Northwind;User Id =sa;Password=coffee;"
Set dbConn = CreateObject("ADODB.Connection")
dbConn.ConnectionString = conString
dbConn.Open
Set Rst = CreateObject("ADODB.Recordset")
With Rst
.CursorLocation = adUseClient
.Open "select Distinct ShipCity from orders order by ShipCity asc", dbConn, adOpenForwardOnly, adLockReadOnly, adCmdText
End With
Dim i As Long
For i = 0 To Rst.RecordCount - 1
Combo1.AddItem Rst.Fields("ShipCity").Value
Rst.MoveNext
Next
Rst.Close
dbConn.Close
Set Rst = Nothing
Set dbConn = Nothing