Can't connect to database, error 80040e21. Impossible to debug - sql-server-2005

I have a simple file that tries to connect to a database-
<%
Set RSDiscounts = Server.CreateObject("ADODB.Recordset")
RSDiscounts.ActiveConnection = "Data Source=serverName;Initial Catalog=dbName.dbo;Integrated Security=True"
%>
When I run it, I get-
error '80040e21'
/filename.asp, line 3
Searching for the error code doesn't help. My best guess is that something is specified in the connection string that shouldn't be there. But I used Visual Studio to create the string, and that connects to the database fine.
Is there any way I can figure out what's wrong? This seems like it's impossible to debug.

I think the problem is with your connection string - the string you have there is for ADO.Net but I don't believe that will work with ADODB.
Try a connection string like this:
Driver={SQL Native Client};Server=myServerName\theInstanceName;Database=myDataBase; Trusted_Connection=Yes
Or this is a connection string from one of my old projects with ADODB (from asp classic)
Provider=SQLOLEDB.1;Initial Catalog=databaseName;Data Source=serverName;Trusted Connection=Yes
That may not be 100% right, but you can find more details of all the connection strings you could want at the excellent ConnectionStrings.com.

From the library I wrote :
function SqlServerConnectionString( byval psDataSource, byval psCatalog, byval psUid, byval psPw)
'______________________________________________________________________________
'
' 'Sql Server Connection String'
'______________________________________________________________________________
dim x
x = "Provider=MSDASQL.1;Persist Security Info=False;User ID=" & psUid & ";Data Source=" & psDataSource & ";Initial Catalog=" & psCatalog
if psPw <> "" then
x = x & ";pwd=" & psPw
end if
SqlServerConnectionString = x
end function
I have similar routines for Firebird, Odbc and Access.

The problem will be you are trying to connect to the database using the user which is running the script. If this is running in IIS it will be something like USR_.
2 alternatives.
Give this user access to the database (I wouldn't do this one).
Create a SQL user and connect via this.
PROVIDER=SQLOLEDB;Data Source=serverName;Initial Catalog=dbName.dbo;USER ID=WebUser;PASSWORD=WebUserPassword;

Related

Is there a way to specify a proxy user id in a VBA Macro to connect to oracle?

I am currently trying to connect to an Oracle database from VBA. Below is the code I am running in VBA to try to establish the initial connection. Unfortunately, this code produces an “ORA-01017” error mentioning that my username/password is invalid. I have SQL Developer installed and when I try to login with the same credentials/info, I successfully connect. I also tried instead setting UID = userid in the VBA code below and the connection didn’t throw an error, but I can’t query any tables. The same thing happens if I try the same in SQL developer.
After doing some reading, it looks like the info in the brackets is my proxy user id and I need to somehow specify that separately from my UID, but I can’t figure out how I would go about that. Has anyone else had any experience with this or have any guidance? Let me know if there is any additional details I can provide.
Sub Ora_Connection()
Dim con As ADODB.Connection
Dim rs As ADODB.recordset
Set con = New ADODB.Connection
Set rs = New ADODB.recordset
StrCon = "Driver={Microsoft ODBC for Oracle}; Uid=userid[database name];Pwd=UserPWD;" & _
"CONNECTSTRING=(DESCRIPTION=" & _
"(ADDRESS=(PROTOCOL=TCP)" & _
"(HOST=HostName)(PORT=1521))" & _
"(CONNECT_DATA=(SERVICE_NAME=XXXX)));"
con.Open (StrCon)
End Sub
It doesn't look like its possible to connect to proxy user with ADODB .
One another alternative is to use alter session set current_schema = Proxy_user run this after you open the session.
The only trouble with this is that the grants should be there for the actual user, here proxy user just allows us to avoid using identifiers.

Excel VBA Database connection error: Cannot open database '(unknown)'

I am trying to connect to Access Database using ADO DSN.
StrPath = Sheets("Sheet1").Range("DB_location")
strCon = "DSN=MS Access Database;DBQ=" & StrPath & ";"
Set con = New ADODB.Connection
con.Open strCon
I can connect to DB without any problem on my machine. However, when I tried running this exact macro on another PC, I ran into this issue on the last line:
I assumed it was due to Database Engine, but installing "Microsoft Access Database Engine 2010 Redistributable" from Microsoft's website didn't solve this problem. All the necessary references in VBA editor are present.
There is no problem with DB or the macro, as I checked on a different PC, but this specific computer runs into this problem.
Changing
strCon = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " & StrPath & ";"
solved this problem, which means the problem was somehow due to DSN. Does anyone have a guess about where does this problem might come from?
I had this error and it ended up being my primary keys. WHen i fixed my primary keys in the table it worked like a charm.

MS Access VBA: Using workspace.OpenDatabase to connect to an unavailable SQL server via an ODBC connection - elegant recovery?

In an application that uses a MS Access form as a front-end to some SQL databases, I use DBEngine.CreateWorkspace to get a workspace, then workspace.OpenDatabase to connect to my remote SQL server via a defined ODBC System DSN. This all works quite nicely, until someone disconnects the remote SQL machine from the network, or shuts it down, or something else similarly ridiculous. (Note: I know there's a lot ridiculous about this setup, but unfortunately it's an inevitability at this point)
My question is:
Is there a way to elegantly deal with the timeout and subsequent 'SQL Server does not exist or access denied' error messages that come up, within the VBA code? workspace.OpenDatabase throws an error that I can catch and deal with, but not before two popups come up and stop my VBA code until an operator clicks OK.
DoCmd.SetWarnings False doesn't affect it as the error popups are not actually coming from Access itself - I think they're from the underlying ODBC process or the Jet Engine that drives it.
Any ideas?
A good solution can be found here:
ACC2000: How to Trap ODBC Logon Error Messages
http://support.microsoft.com/kb/210319
The above is from Access 2000 and is 14 years old, but as such it still works fine today. The other possible advantage is you don’t have to adopt and introduce ADO into your application. For applications that already use or have ADO, then no big deal, but if your application sticks to one data object model, then you not have to potentially introduce ADO.
The other BIG bonus of the above is this effectively logs you into the database and thus you avoid having user name and passwords in the linked tables. This means you can have different users and logons, and NOT have to re-link or embed the user name or password in your linked tables.
This wonderful trick and result of the above connection trick is outlined here:
Power Tip: Improve the security of database connections
http://blogs.office.com/b/microsoft-access/archive/2011/04/08/power-tip-improve-the-security-of-database-connections.aspx
I eventually found something that works by searching 'Suppress ODBC connection failure warnings'.
Courtesy of Trevor Best from http://bytes.com/topic/access/answers/201502-how-suppress-odbc-connection-dialog
Some code that uses ADO to make the database connection in a way that allows VBA error trapping to catch the error before the system throws any popups at you.
Function CanOpenSQLDbLB(pstrServer As String, pstrDb As String, pstrUser
As String, pstrPassword As String, Optional pfReportError As Boolean =
True) As Boolean
On Error GoTo CanOpenSQLDbLB_Err
Dim objConn As Object
Dim strConn As String
Dim strError As String, lngErr As Long
Const cstrSQLErr = "[Microsoft][ODBC SQL Server Driver][SQL Server]"
Set objConn = CreateObject("ADODB.Connection")
strConn = strConn & "DRIVER=SQL Server"
strConn = strConn & ";SERVER=" & pstrServer
strConn = strConn & ";APP=" & Application.Name
strConn = strConn & ";WSID=AWorkstation"
strConn = strConn & ";DATABASE=" & pstrDb
objConn.Open strConn, pstrUser, pstrPassword
CanOpenSQLDbLB = True
CanOpenSQLDbLB_Exit:
On Error Resume Next
objConn.Close
Set objConn = Nothing
Exit Function
CanOpenSQLDbLB_Err:
lngErr = Err.Number
strError = Err.Description
If InStr(1, strError, cstrSQLErr) Then
strError = "Error reported by server" & vbCr & vbCr &
Replace(strError, cstrSQLErr, "")
End If
Select Case lngErr
Case Else
If pfReportError Then
MsgBox strError, 16, "Error #" & Err & " Attempting to
open server database"
End If
End Select
Resume CanOpenSQLDbLB_Exit
End Function

Obtaining Databases from SQL Server 2008 using vb in VS 2012

I am fairly new to asp.net but have knowledge is vb. I am building an web application in visual studio 2012 using vb. I have a drop down list that is populated with two 2008 SQL Servers. There is another dropdown that will need to populated with a list of available databases on the selected server. I have a button that once clicked and the server is selected it will do a query and pull the list of databases
I have specified the server name in a globalvariables class and then imported it into the form I am using.
I am thinking that will need to do a IF statement that will need to say, If server1 is selected the open a connection and perform a query on the databases.
Here is my code that I have but its throwing an overload resolution failed because no accesible 'Open' accepts this number of arguments error:
Protected Sub getdb_Click(sender As Object, e As EventArgs) Handles getdb.Click
Dim objConnection As OleDbConnection
objConnection = CreateObject("OleDbConnection")
If DropDownList1.Text = globalvariables.servername1 Then
objConnection.Open("Provider=SQLOLEDB; Data Source=" & "globalvariables.servername1" & ";" & _
"Trusted_Connection=Yes; Initial Catalog=master")
End If
Please help me in getting the correct code as I have tried a number of items off of this site and nothing seems to work.
You use the New keyword to instantiate your objects
Dim serverName = DropDownList1.Text
Dim connectionString = "Provider=SQLOLEDB; Data Source=" & serverName & ";" & _
"Trusted_Connection=Yes; Initial Catalog=master"
Dim objConnection = new OleDbConnection(connectionString)
objConnection.Open()
and pass the instructions on how to open the database using a connectionString.
However, because you are using a SqlServer database why don't you use the specific classes provided for Sql Server?
SqlConnection, SqlCommand, SqlDataAdapter and so on....

MS Excel connection with vb.net

I have used the connection string below but I am getting an error when trying to create a table
Dim ConnString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFName + _
";Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1"""
Cannot modify the design of table 'tablename'. It is in a read-only database.
If the database is read-only, then by definition you will not be able to create any tables in it.
Make absolutely certain that you do have write access to the file. For example are you accessing this from IIS which only has limited permissions. Check the security of the directory. Try a normal File.Open() of the file in the same process.
I'm personally using the following to connect to an access database:
_source = "..\db.mdb"
Dim strconnexion As String
strconnexion = "Provider=Microsoft.Jet.OLEDB.4.0;"
strconnexion &= "User ID=Admin;Password=;"
strconnexion &= "Data source=" & _source
_cnBd = New OleDbConnection (strconnexion)
_cnBd.Open()
Hope this helps.
Your problem is the IMEX=1. That tells excel to open in "import mode" making the connection read-only. I had the same issue, weird weird stuff.
Take that out and it works like a charm.