0

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.

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.

The object explorer of SQL Server Management Studio shows:

localhost\SQLSERVER3(SQL Server 10.0.1600-RAJ-PC\raj)

Databases

examples
  tables
   System Tables
      dbo.cars
      columns
          id(PK,int,not null)
          name(varchar(50),null)

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.

Please help me.

UPDATED

here is my code :

<html>
<head>
<title>Guestbook</title>
</head>

<body bgcolor="white" text="black">
<%
'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};" & _
           "Server=localhost\SQLSERVER3;" & _
           "Database=examples;" & _
           "Uid=raj;" & _
           "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 ("<br>")
    Response.Write (rsGuestbook("Name"))
    'Response.Write ("<br>")
    'Response.Write (rsGuestbook("Comments"))
    'Response.Write ("<br>")

    'Move to the next record in the recordset
    rsGuestbook.MoveNext
Loop

'Reset server objects
rsGuestbook.Close

Set rsGuestbook = Nothing
Set adoCon = Nothing
%>

</body>
</html>
7
  • C# or VB? What does you code look like so far? Commented Sep 29, 2011 at 20:58
  • 2
    2 things. 1. Are you looking for help with ASP (classic), or ASP.NET. 2. Do you have a sample of the code you're using to try to connect to the database to show what you've tried? Commented Sep 29, 2011 at 20:58
  • ahh, you changed the default instance? Commented Sep 29, 2011 at 21:04
  • @Alan Moore : I am using VB. I thing i am not able to specify the connection string properly. Here is part of my code. adoCon.Open "ODBC;Driver={SQL Native Client};" & _ "Server=localhost\SQLSERVER3;" & _ "Database=examples;" & _ "Uid=raj;" & _ "Pwd=love1987" Commented Sep 29, 2011 at 21:10
  • @blake i was looking for help with asp but it would be really of great help if you explain the connectivity with asp.net since it is better than classic asp. Commented Sep 29, 2011 at 21:24

2 Answers 2

2

I am able to connect using the following connection string on my local dev machine (with SQL 2008 R2 Express):

Driver={SQL Server}; Server=hostname\instancename; Database=dbname; Uid=user; Pwd=password

One thing I noticed in your code: you are trying to establish a DSN-less connection, then you run a query on it without USE dbname or anything. That may be the issue, or a least an issue.

Sign up to request clarification or add additional context in comments.

7 Comments

thanks for your reply. So i should use Driver={SQL Server}; Server=localhost\SQLSERVER3; Database=examples; Uid=raj; Pwd=xxxxxx..........i have used windows authentication option while connecting to my sql server database. So shall i use windows username and password in uid and pwd field or use the normal username 'sa' and corresponding password to connect..?
thanks a lot buddy....you solved my problem..i am able to connect now...thank you very much...just one more help. Does the same connection string work with asp.net too? If not plz specify the connection string used in asp.net.
Just checked on as ASP.net project, and I'm using a very similar connetion string, just without the Driver={SQL Server}; part. However, I'm not 'manually' connecting to the server, my CMS (DotNetNuke) is doing that for me, so I'm not sure.
hi, is there any way to add the value of server attribute at runtime in connection string you specified. That means i am providing the user with a text box in which he enters the server name and i want to user that server name in my connection string. please help.
Sure, it's a string after all! In classic ASP (vbscript) you could do this: server=request("server_name") [line break] connstr = "Driver={SQL Server}; Server=" & server & "; Database=dbname; Uid=user; Pwd=password" [line break] adoCon.Open connstr. HOWEVER: what you are trying to do sounds dangerous in terms of server security, be careful on what you'll allow your web users to do!
|
1

Try creating a DSN, and then refer to it by name in your connection string.

  1. Open the ODBC icon in your Control Panel.
  2. Choose the System DSN tab.
  3. Click on Add in the System DSN tab.
  4. Select the Microsoft Access Driver. Click Finish.
  5. In the next screen, click Select to locate the database.
  6. Give the database a Data Source Name (DSN).
  7. Click OK.

    set conn=Server.CreateObject("ADODB.Connection")

    conn.Open "northwind"

http://www.w3schools.com/ado/ado_connect.asp

3 Comments

hi, thanks for your answer but i am trying to connect to mssql server database. Can we do that using microsoft access driver? Your post is really very informative but i need to create a dsn-less connection.
Yeah, sorry I just copy and pasted that text, but a DSN can be created for SQL Server instead of Access. Just thinking you could try the DSN approach for troubleshooting purposes, or just to get you over this hurdle for now so you can focus on other parts of the project. If the problem is unique to your dev environment, then it will not matter when you move to prod.
but the DSN will also only exist on the machine its created on, so if you move the asp site, you'll need to make a new DSN on the machine you move it to. This is good if you need to reuse the code but have to reconfigure for different databases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.