hi all,
please help to tell me how can i connect to my vb.net app to SQL Server Database?
Printable View
hi all,
please help to tell me how can i connect to my vb.net app to SQL Server Database?
Here is a short example of how i'm connecting to a Sql server.. I've found this to take a bit of time to impliment, however with using the stored proc's it is easier to update...
Gremmy..Code:Imports System.Data.SqlClient
Public Class SqlData
Public Function Getdata() As List(Of dataformat)
Dim sqlConn As New SqlConnection([ConnectionString]) '<-- Connection string [Data source=./Sql; Initial catalog=My data; User=sa; Password=*******]
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
Getdata = New List(Of dataformat)
Try
sqlConn.Open()
With cmd
.Connection = sqlConn
.CommandType = CommandType.StoredProcedure
.CommandText = "CF3_Get_MyData" '<--- SQL Stored procedure
.Connection = sqlConn
dr = .ExecuteReader()
If Not dr Is Nothing Then
If dr.HasRows Then
While dr.Read()
Dim Thisdata As New dataformat
With Thisdata
.Uid = dr("Uid")
.Month = dr("Month")
'........ all your table items listed here..
End With
Getdata.Add(Thisdata)
End While
End If
End If
End With
Catch ex As Exception
' error system call here...
End Try
End Function
thank you so much Gremmy :D thank you so much
Glad you came right...
there was one more thing that i forgot to add in ..
Gremmy....Code:.CommandText = "CF3_Get_MyData" '<--- SQL Stored procedure
.Parameters.AddWithValue("@Uid", Uid) '<------- To pass parameters too the SQL procudure.