如何将此代码更改为SqlConnection?

时间:2013-07-06 18:06:10

标签: vb.net

有人可以将此代码更改为SqlConnection吗?

Dim Db As ADODB.Connection
Dim rs As ADODB.Recordset

Set Db = New ADODB.Connection
Db.ConnectionString = GetConnectString & AppPath & "schedule.mdb"
Call Db.Open
Set rs = New ADODB.Recordset
Set rs = Db.Execute("select * from tbl_Schedule where StartDate = #1/1/2002#")

While Not rs.EOF
Call Me.Schedule1.ScheduleItems.Add("", #1/1/2002#, rs!StartTime, _ 
rs!Length, rs!Description, "")
Call rs.MoveNext
Wend

1 个答案:

答案 0 :(得分:0)

请找到以下代码片段,它将帮助您从sql数据库中获取数据。您可以使用您的值更改脚本。导入System.Data.SqlClient命名空间以使用SqlConnection

Dim connection As New SqlConnection("Server=.\sqlexpress;Integrated   security=sspi;database=Automation")
Dim query As String = "Select * from Heads"
If connection.State = ConnectionState.Closed Then connection.Open()
   Using cmd As New SqlCommand(query, connection)
        Dim reader As SqlDataReader = cmd.ExecuteReader
        While reader.Read
            ListBox1.Items.Add(reader("HeadName").ToString)
        End While
        reader.Close()
        connection.Close()
   End Using