Unable to connect to SQL2005 using VBScript - sql-server

I have the following VBScript, which is supposed to connect to a SQL Server 2005 database. But, my connection is failing. Why?
Set dbConnection = CreateObject("ADODB.Connection")
dbConnString = "Provider=SQLOLEDB.1;Data Source=srv\test1;" & _
"Initial Catalog=tset_DB;user id ='abc';password='abc'"
'Open the connection
dbConnection.Open dbConnString

You don't need quotations around your username and password. Try this:
Set dbConnection = CreateObject("ADODB.Connection")
dbConnString = "Provider=SQLOLEDB.1;Data Source=srv\test1;" & _
"Initial Catalog=test_DB;user id =abc;password=abc"
'Open the connection
dbConnection.Open dbConnString
Also, your Initial Catalog was set to tset_DB, when I'm guessing it should be test_DB.
ConnectionStrings.com is a huge help for ensuring that your connection strings are valid.

Related

how to connect VBScript (Excel) to SQL Server web database (via IP)

I'm at my job trying to do some unknow stuff for me, you see, we're trying to connect an excel document with a VBScript Macro to a databse stored in web server but for some reason doesn't recognizes the user and throws an error repeatedly, i discarded a connection issue since it returns an SQL error instead of something like a timeout or server doesn't exists or something like that, we're trying to connect to the server using the ip address, we also checked that the logging method is on mixed (win and sql) and remotes connections to the server are enabled as well, also if i use the credentials provided in the connection string (username and password) i can actually log in to SQL Server without any issue, we also tried a direct connection (external vpn) because we thought it could be our firewall, but got the same error anyway, so we have no clue what it could be and we're kinda running out of ideas on how to do this, i'll post down below the code i'm using to trying the connection (obviously test data but similar to reality)
picture of the error i'm getting (don't post the original since it's in spanish but is very similar to this):
code i'm currently trying:
Sub excel_sqlsrv()
Set rs = CreateObject("ADODB.Recordset")
Set conn = CreateObject("ADODB.Connection")
strConn = "Driver={ODBC Driver 17 for SQL Server};Server=10.20.30.5;Database=mydb;UID=sa;PWD=abcd12345;"
conn.Open strConn
strSqL = "SELECT * FROM USERS"
rs.Open strSqL
End Sub
Any advice, tip or trick could be of tremendous help for me, i'll be looking forward to any kind of comment, thanks in advance
Use the ODBC Data Source Administrator to create a connection named mydb and test it works. Then use
Sub excel_sqlsrv()
Const strConn = "mydb" ' ODBC source
Const strsql = "SELECT * FROM USERS"
Dim conn As Object, rs As Object
Set rs = CreateObject("ADODB.Recordset")
Set conn = CreateObject("ADODB.Connection")
On Error Resume Next
conn.Open strConn
If conn.Errors.Count > 0 Then
Dim i, s
For i = 0 To conn.Errors.Count - 1
s = s & conn.Errors(i) & vbLf
Next
MsgBox s
Else
On Error GoTo 0
Set rs = conn.Execute(strsql)
Sheet1.Range("A1").CopyFromRecordset rs
End If
End Sub
You can try using OLEDB provider instead of ADODB.

How to secure connection between Vb6 application and mssql server?

I am working on a legacy application which uses sqloledb provider and activex data objects to connect to mssql database. Now I need to encrypt the connection between the application and server without force encryption option in sql server.
I have installed a self signed certificate in the sql server instance and tried putting the Encrypt=true and Trustservercertificate=true in connection string. But the connection is not encrypted.
I have tried using ODBC provider with ADO and while using encrypt=true and trustservercertificate=true, I am getting a SSL security error which opening the connection.
Please let me know how to establish a secure connection using ADO 2.8 library.
Private Sub Command1_Click()
Dim sConnectionString As String
Dim strSQLStmt As String
'-- Build the connection string
'sConnectionString = "UID=userid;PWD=password;Initial Catalog=EHSC_SYM_Kings_Development;Server=EHILP-257\MIB14;Provider=MSOLEDBSQL;Encrypt=YES;trustServerCertificate=YES"
'sConnectionString = "Provider=sqloledb;Data Source=192.168.27.91\MIB14;Initial Catalog=EHSC_SYM_Kings_Development;User Id=userid;Password=password;Encrypt=YES;trustServerCertificate=YES"
'sConnectionString = "driver={SQL Server};server=192.168.27.91\MIB14;user id=userid;password=password;Initial Catalog=EHSC_SYM_Kings_Development;Encrypt=Yes;trustServerCertificate=True"
sConnectionString = "Provider=SQLNCLI11;Server=192.168.27.91\MIB14;Database=EHSC_SYM_Kings_Development;Uid=userid;Pwd=password;Encrypt=yes;trustServerCertificate=True"
strSQLStmt = "select * from dbo.patient where pat_pid = '1001'"
'DB WORK
Dim db As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rs As New ADODB.Recordset
Dim result As String
db.ConnectionString = sConnectionString
db.Open 'open connection
With cmd
.ActiveConnection = db
.CommandText = strSQLStmt
.CommandType = adCmdText
End With
With rs
.CursorType = adOpenStatic
.CursorLocation = adUseClient
.LockType = adLockOptimistic
.Open cmd
End With
If rs.EOF = False Then
rs.MoveFirst
Let result = rs.Fields(0)
End If
'close conns
rs.Close
db.Close
Set db = Nothing
Set cmd = Nothing
Set rs = Nothing
End Sub
Thank you all for your suggestions, I finally managed to make the connection secure by changing the driver to sqlserver native client 11(ODBC). Looks like sqloledb doesn't have support for tls. Changing the driver to ODBC doesn't seem to change the behaviour much. Rest of my code works fine without any changes
sConnectionString = "Driver={SQL Server Native Client 11.0};Server=192.168.27.91\MIB14;Database=xxxxxxxx;user id=xxxxxxx;password=xxxxxxxxx;Encrypt=yes;TrustServerCertificate=yes"
Just went through this myself.
For your original connection string the keyword you're looking for is not "Encrypt=true" or "Encrypt=yes" like most of the internet would lead you to believe. It's "Use Encryption for Data=true".
So your connection string becomes:
sConnectionString = "Provider=SQLNCLI11;Server=192.168.27.91\MIB14;Database=EHSC_SYM_Kings_Development;Uid=userid;Pwd=password;Use Encryption for Data=true;trustServerCertificate=True"
The trustservercertificate=true is just for testing with self-signed certs... and exposing yourself to man-in-the-middle attacks ;)
For reference this was tested with:
VB6 connecting to SQL Server 2016 using SQLNCLI11 provider via ADODB.

Login failed for user (SQL Server) via VBA (Excel) Macro

I'm creating a macro in an Excel spreadsheet that will connect to an instance of SQL server using SQL Server Authentication. This is a recycled function I've written previously that I know works. I suspect it's either the way I've constructed the connection string for the SQL authentication, or the way the user is configured (although I have successfully logged into the server through SSMS with the user account so I know the account works).
Public Function GetConnection(ServerLocation As String, db As String) As ADODB.Connection
Dim ServerName As String, cn As ADODB.Connection, un As String, pw As String
un = "MyUser"
pw = "MyPassword"
ServerName = "MyServer"
If cn Is Nothing Then
Set cn = New ADODB.Connection
cn.ConnectionString = "Driver={SQL Server};Server=" & ServerName & ";Database=" & db & ";UID='" & un & "';Pwd=" & pw & ";"
cn.CursorLocation = adUseClient
cn.Open
When I run the macro I get
Login Failed for user "MyUser"
at the cn.Open line.
I've checked that remote logins are allowed, and both SQL Server and Windows Authentication modes are enabled. I've also recreated the user account just in case.
Is there an issue with the connection string or am I missing something on the server?
Many thanks!
I found the solution: the single quotes around the UID are not required. I spotted these after viewing the Log and noticing the way the user name was formatted. Without the additional single quotes the connection is successful and my procedure executes successfully.
My final connection string looks like this:
cn.ConnectionString = "Driver={SQL Server};Server=" & ServerName & ";Database=" & db & ";UID=" & un & ";Pwd=" & pw & ";"

Method/Property Error on New SQL Server Connection

I'm running VB .Net in VS2013, on a Windows 8 over SQL Server 2008 R2, and my creation of an SQL Connection is failing with the error:
Property access must assign to the property or use its value.
Here's my code:
Dim oCnn As SqlConnection
Dim sCnn As String
Dim bSunCnnOK as Boolean
Try
If vsSunServer <> "" Then
sCnn = "Provider=SQLOLEDB.1;" & _
"Integrated Security=SSPI;" & _
"Persist Security Info=False;" & _
"Initial Catalog=SunSystemsData;" & _
"Data Source=" & vsSunServer
oCnn = New SqlConnection(sCnn)
oCnn.Open()
bSunCnnOK = True
End If
Catch ex As Exception
bSunCnnOK = False
End Try
_vsSunserver_ is a string being passed in to the sub, and has a run-time value of "SVRSUN07".
The error is being raised on the line:
oCnn = New SqlConnection(sCnn)
So at run-time, sCnn holds:
"Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=SunSystemsData;Data Source=SVRSUN07"
I've lifted this connection string from a .udl file, which returns successful when I Test Connection.
I can run SQLSMS over this database OK.
Below is an ASCII art diagram that shows how the components related to each other. You are trying to use a SQL OLEDB connection string with SqlConnection. These things don't go together.
C#/VB.NET code -┬------------> SqlConnection -----------------┬-> SQL Server
| |
├--> OleDbConnection -┬-> SQL OLEDB provider -┤
| | |
| Native code --┤ |
| | |
└-> OdbcConnection ---┴-> SQL ODBC driver ----┘
If you really want to use the SQL OLEDB native provider then you can use OleDbConnection. This might be useful if you want your VB.NET code to be flexible and possible connect to other OLEDB providers such as Access, Postgres, Mysql, etc.
However if you know for sure that you will only be connecting to SQL Server then it will be easier to use SqlConnection instead with a connection string like "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=SunSystemsData;Data Source=SVRSUN07".

Cannot open database "dbname" requested by the login. The login failed

I'm trying to make a connection to sql server database(hosted on localhost) but keep getting the error mentioned in the title.
Application("ConnectionString") = "Provider=SQLOLEDB;Data Source=localhost\SQLExpress;Database=mydb;Trusted_Connection=yes;UID=dbadmin; PWD=dbadmin"
Application("ConnectionTimeout") = 15
Application("CommandTimeout") = 90
Application("CursorLocation") = 3
strQuery = "select * from dec_users"
Set objDBConnection = Server.CreateObject("ADODB.Connection")
objDBConnection.open Application("ConnectionString")
Set RS = Server.CreateObject("ADODB.RecordSet")
RS.Open strQuery, objDBConnection
any ideas?
You are putting it wrong, change to:
Application("ConnectionString") = "Provider=SQLOLEDB.1;Integrated Security=SSPI;
Persist Security Info=False;User ID=dbadmin;Initial Catalog=mydb;
Data Source=localhost\SQLExpress;Password=dbadmin"
(Linebreaks added for legibility)
When in doubt, create a .UDL file, construct the ConnectionString using the GUI, and then copy paste the ConnectionString (opening the file with notepad)

Resources