Click to See Complete Forum and Search --> : [RESOLVED] connection to sqlserver


chunks
October 2nd, 2008, 12:56 AM
hi all,
please help to tell me how can i connect to my vb.net app to SQL Server Database?

GremlinSA
October 2nd, 2008, 02:30 AM
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...


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


Gremmy..

chunks
October 3rd, 2008, 12:36 PM
thank you so much Gremmy :D thank you so much

GremlinSA
October 6th, 2008, 01:55 AM
Glad you came right...
there was one more thing that i forgot to add in ..
.CommandText = "CF3_Get_MyData" '<--- SQL Stored procedure
.Parameters.AddWithValue("@Uid", Uid) '<------- To pass parameters too the SQL procudure.
Gremmy....