jomachi jomachi - 1 month ago 6
Vb.net Question

How can I change this code to SqlConnection?

Can anyone change this code to

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

Answer

Please find the below snippet which will helps you to get data from sql database. You can alter the script with your values. Import System.Data.SqlClient namespace to work with 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