0
votes

Theres been a few questions with this but as far as I know/believe they don't help me at all. My code was working fine previously. But for some reason stopped. My sql server version is 2012. The specific error is:

[Microsoft][ODBC SQL Server Driver][SQL Server] Changed database context to 'sqldbname'

My ASP code:

<!--#include file="connv.inc"-->
<%
n=Request.form("total")
response.write(n)

for x = 0 to n-1
ttitle=Request.form("title_"&x)
title=Replace(ttitle, "'", "''")
id=Request.form("id_"&x)
views=Request.form("vViews_"&x)
likes=Request.form("vLikes_"&x)
description=Request.form("vDescription_"&x)

sql="INSERT INTO tbl_videos(videoTitle, videoId, videoLikes, videoViews, videoDescription, swamCompatible) values ('"&title&"', '"&id&"', '"&likes&"', '"&views&"', '"&description&"', '0')"
connv.execute(sql)
response.write(sql&"<br>")
next
%>

<html>
<head>
<title> Updating Tables </title>
</head>
<body>

</body>
</html>

<!--#include file="closeEmv.inc"-->

connv.inc code:

<%
set connv = server.createobject("ADODB.Connection")
connv.open "DRIVER={SQL SERVER}; SERVER=52.2.8.73; UID=myuid; PWD=mypwd; DATABASE=sqldbname"
%>
2
Lets start with the basics... is sqldbname a valid database name on your server? - Bond

2 Answers

0
votes

Try replacing your connv.inc code with below part,

<%
set connv = server.createobject("ADODB.Connection")
connv.open "Provider=SQLOLEDB; Data Source=52.2.8.73; Initial Catalog=sqldbname; User ID=myuid; Password=mypwd"
%>
0
votes

Following one from my comments

@JonathanLin You haven't fixed anything you have just switched one connection type for another. You were using SQL Server ODBC Driver this answer changes that to the Microsoft OLE DB Provider for SQL Server.

@JonathanLin It all depends on what version of SQL Server you are using if you are using SQL Server 2005 you should be using the SQL Native Client 9.0 OLE DB Provider or higher but after SQL Server 2000 you should be using a SQL Native Client OLE Provider to support types like nvarchar(max) for example (worth noting SQL Native Client 9.0 is backward compatible).

You should trying to use the most up to date provider which for the balance of features and backward compatibility (you haven't stated what version of SQL Server you are using) I would recommend using SQL Native Client 9.0 OLE DB Provider.

If this is not installed on your web server you will need to download and install it first.

<%
Set connv = Server.CreateObject("ADODB.Connection")
connv.open "Provider=SQLNCLI; Server=myServerAddress; Database=myDataBase; Uid=myUsername; Pwd=myPassword;"
%>

Using this provider will ensure you have access to features as you upgrade from earlier versions of SQL Server without requiring you to mess with your connection string. Features such as NVARCHAR(MAX) (which was first supported with SQL Server 2005).


Side Note:

Also instead of opening your connection inside an #include file consider storing the connection string as a string variable and only instantiating the ADODB.Connection when it is needed.
In fact when using ADODB.Command to execute SQL Server commands the connection string variable can be passed to ActiveConnection property of the command object and the command object will instantiate the ADODB.Connection for you. This way when the command is closed so is the connection.