Results 1 to 4 of 4

Thread: Connecting to SQLSERVER with ADO.net

  1. #1

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    Connecting to SQLSERVER with ADO.net

    I've seen several examples on this board.. all of which were different and none of which actually seemed to work..


    Does anyone have a good example using ADO.net to connect to SQL Server?

    Thanks!
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  2. #2
    Lively Member
    Join Date
    Jan 2001
    Location
    Worcester, MA
    Posts
    77
    This is how I do it,

    This goes into the web.config file:
    Code:
    <appSettings>
    <add key="dsnNt" value="data source=***.***.***.***;initial catalog=nnerensql;UID=******;PWD=*****"/>
    </appSettings>
    then I add these imports to my code behind page

    Code:
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Collections.Specialized
    then my query execution goes like this:
    Code:
    Dim conn As String
            Dim appsetting As NameValueCollection = CType(HttpContext.Current.GetConfig("appSettings"), NameValueCollection)
            conn = CStr(appsetting("dsnNT"))
            Dim myConnection2 As SqlConnection = New SqlConnection(conn)
            Dim dsLogin As SqlCommand = New SqlCommand("select something from the database", myConnection2)
            Dim ds As SqlDataReader
            myConnection2.Open()
            ds = dsLogin.ExecuteReader()
    while ds.read()
    'do something
    end while
    ds.close
    myconnection2.close

    hope that helps
    "Find all you need in your mind if you take the time" -DT

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    What doesn't work about them?

    The basics of the SQL connection string are the following:

    NOTE: There is no provider section to the SQL connection string like in OLEDB

    'Data Source'=This is either the SQL Server name if local or an IP address of the remote server, which may also need the port if not default

    'Initial Catalog'=The database in the SQL Server you want to connect to


    'Integrated Security=SSPI'=This uses the windows account to login to the SQL Server and can be replaced with a username (UID) and password (PWD) if not using Windows Authentication

    'Persist Security Info'=I'm not really sure what this does or if it is required but I always seem to have it in my connection strings

    Here is an example connection string:
    Data Source=MHC2;Initial Catalog=MHC_NET;integrated security=SSPI;

    VB Code:
    1. 'How to connect to a SQL Server
    2. Dim cnn As New System.Data.SQLClient.SQLConnection("Data Source=MHC2;Initial Catalog=MHC_NET;integrated security=SSPI;")
    3. cnn.Open

  4. #4

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    OK thanks i'll give it a shot.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


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