title: Connecting to SQL Server 2008 database using asp
body: <p>I tried a lot but I am not able to deal with this problem. I wasted last 2 days without any significant result. Hope I will get some help here.</p>
  
  <p>I wanted to connect to a SQL Server 2008 database using ASP. I installed SQL Server 2008, created a database, but I am not able to connect to that database using the asp code. I can see the database in Visual Web Developer, I can also connect it through asp.net using the add connection wizard of visual web developer but I don't want to use add connection wizard. I want to write my asp code to connect to SQL Server database in notepad. My IIS is working and I am able to run asp code to access other database like ms access database but I can't access SQL Server db. </p>
  
  <p>The object explorer of SQL Server Management Studio shows:</p>
  
  <pre><code>localhost\SQLSERVER3(SQL Server 10.0.1600-RAJ-PC\raj)
  </code></pre>
  
  <p>Databases</p>
  
  <pre><code>examples
    tables
     System Tables
        dbo.cars
        columns
            id(PK,int,not null)
            name(varchar(50),null)
  </code></pre>
  
  <p>You can see the SQL Server and its databases in the attached jpg image. I want to connect to example database and then want to access cars table.</p>
  
  <p>Please help me.</p>
  
  <p><strong>UPDATED</strong></p>
  
  <p>here is my code : </p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Guestbook&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body bgcolor="white" text="black"&gt;
  &lt;%
  'Dimension variables
  Dim adoCon          'Holds the Database Connection Object
  Dim rsGuestbook         'Holds the recordset for the records in the database
  Dim strSQL          'Holds the SQL query for the database
  
  'Create an ADO connection odject
  Set adoCon = Server.CreateObject("ADODB.Connection")
  
  'Set an active connection to the Connection object using a DSN-less connection
  adoCon.Open "ODBC;Driver={SQL Native Client};" &amp; _
             "Server=localhost\SQLSERVER3;" &amp; _
             "Database=examples;" &amp; _
             "Uid=raj;" &amp; _
             "Pwd=love1987"
  
  'Set an active connection to the Connection object using DSN connection
  'adoCon.Open "DSN=guestbook"
  
  'Create an ADO recordset object
  Set rsGuestbook = Server.CreateObject("ADODB.Recordset")
  
  'Initialise the strSQL variable with an SQL statement to query the database
  strSQL = "SELECT name FROM dbo.cars;"
  
  'Open the recordset with the SQL query 
  rsGuestbook.Open strSQL, adoCon
  
  'Loop through the recordset
  Do While not rsGuestbook.EOF
      'Write the HTML to display the current record in the recordset
      Response.Write ("&lt;br&gt;")
      Response.Write (rsGuestbook("Name"))
      'Response.Write ("&lt;br&gt;")
      'Response.Write (rsGuestbook("Comments"))
      'Response.Write ("&lt;br&gt;")
  
      'Move to the next record in the recordset
      rsGuestbook.MoveNext
  Loop
  
  'Reset server objects
  rsGuestbook.Close
  
  Set rsGuestbook = Nothing
  Set adoCon = Nothing
  %&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>