CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2007
    Posts
    445

    [RESOLVED] connection to sqlserver

    hi all,
    please help to tell me how can i connect to my vb.net app to SQL Server Database?

    please rate the post if this is useful. And also never forget to mark the thread as [Resolved] when your problem is solved

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: connection to sqlserver

    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...

    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
    Gremmy..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  3. #3
    Join Date
    Aug 2007
    Posts
    445

    Re: connection to sqlserver

    thank you so much Gremmy thank you so much

    please rate the post if this is useful. And also never forget to mark the thread as [Resolved] when your problem is solved

  4. #4
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: connection to sqlserver

    Glad you came right...
    there was one more thing that i forgot to add in ..
    Code:
                        .CommandText = "CF3_Get_MyData"   '<--- SQL Stored procedure
                        .Parameters.AddWithValue("@Uid", Uid) '<------- To pass parameters too the SQL procudure.
    Gremmy....
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured