What is the connection string to connect to local db file (.mdf) - sql-server

I use the following connection string to connect to database in the sqlserver instance SQLEXPRESS:
QString connStr = "DRIVER={SQL SERVER};SERVER=%1;DATABASE=%2;";
But I want now to connect to SQL Server database file (.mdf) and I already installed Microsoft SQL Server 2016 LocalDB. What connection string should I use?
I tried the following connection string:
QString connStr = "DRIVER={SQL SERVER};SERVER=%1;AttachDbFileName=%2;"
db.setDatabaseName(connStr.arg("(LocalDB)\\v11.0",dbFilename));
And I also tried a different server:
db.setDatabaseName(connStr.arg("(LocalDB)\\MSSQLLocalDB",dbFilename));
But none of them works and throw the same error:
"[Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not
exist or access denied. [Microsoft][ODBC SQL Server
Driver][DBNETLIB]ConnectionOpen (Connect()). QODBC3: Unable to
connect"

After some researches I found the solution:
1- Ensure that Microsoft SQL Server 2016 LocalDB is installed.
2- Change the 'Driver' in the connection string to:
DRIVER={ODBC Driver 13 for SQL Server}
3- Ensure the attribute 'AttachDbFileName' points to the correct database file path, especially you need to pay attention to the path separator whereas QT use "/" but in Windows you have to use "\" ("\\") <-- (This is was my main problem).
To get rid of this problem use QDir::toNativeSeparators(dbFilename).
So and after those changes the connection string will be like this:
QString connStr = "DRIVER={ODBC Driver 13 for SQL Server};SERVER=%1;AttachDbFilename=%2;Integrated Security=true;";
db.setDatabaseName(connStr.arg("(localdb)\\MSSQLLocalDB").arg(dbFilename));
Thanks.

Related

What connection string and or OLEDB/ODBC drivers when moving "classic ASP/ADO" app from SQL Server 2008 to SQL Server 2017?

My "classic ASP" application is on Windows Server 2003 and uses ADO to connect to a SQL Server 2008 database.
The database server is being upgraded from a changing and will be running SQL Server 2017.
I am trying to prove that the old application can still work, connecting to this newer version of SQL Server.
I have so far not had success in making a connection to a SQL Server 2017 installation from the old WS2003 machine.
I am not sure what OLEDB or ODBC providers or drivers I need to install to get this to work, and ultimately, what the Connection String will need to be.
I have created a small demo program to make the connection using the same code as the real application will. Essentially, all I am doing here is providing a connection string and attempting to connect. This is VB6 using ADO:
Private Sub btnConnect_Click()
Dim theConnection As New ADODB.Connection
theConnection.ConnectionString = Text1.Text
theConnection.Open ConnectString
If theConnection.State = 1 Then
MsgBox ("connected ")
Else
MsgBox ("failed to connect ")
End If
End Sub
I can get this program to run on the new server - the one with SQL Server 2017, and it connects successfully.
This connection string works :
Driver={SQL Server Native Client 11.0};Server=192.168.0.23;Database=TestDB;Uid=user1;Pwd=xxx;
This also works :
Driver={SQL Server};Server=192.168.0.23;Database=TestDB;Uid=user1;Pwd=xxx;
This one is the exact format that the existing code uses to connect.
Now, I understand that for ADO, I have not specified Provider, so I think it defaults to MSDASQL, Microsoft's OLEDB provider for ODBC.
So, to get this to connect from the WS2003 application server, is where I have problems. Is this going to be possible?
I have tried these connection strings with these results :
Driver={SQL Server};Server=<IP address of SQL2008 server>;Database=yyy;Uid=xxx;Pwd=xxx;
This WORKS - and this is the existing SQL2008 database.
Driver={SQL Server};Server=<IP address of SQL2017 server>;Database=TestDB;Uid=user1;Pwd=xxx;
This takes approx 20-30 seconds to fail with this message :
[ODBC SQL SERVER DRIVER]SQL Server does not exist or access denied
Is the "old" {SQL Server} driver going to be able to connect to to SQL2017?
Driver={SQL Server Native Client 11.0};Server=192.168.0.23;Database=TestDB;Uid=user1;Pwd=user1;
This fails immediately with the message :
[ODBC Driver Manager]Data Source Name not found and no default driver
specified
Does this mean that the {SQL Server Native Client 11.0} ODBC driver is not installed on the WS2003 machine? Can it be installed?
I have tried looking at the various ODBC driver installations from Microsoft here : https://learn.microsoft.com/en-us/sql/connect/odbc/download-odbc-driver-for-sql-server?view=sql-server-2017
However, I'm not sure if I need to be doing this or if they are compatible with WS2003.
Microsoft ODBC Driver 17 for SQL Server : requires at least Windows Server 2008 R2. Same for Microsoft ODBC Driver 13 and 11. I did get the version 11 to install, event though it probably is not supported. I did use this to successfully create a new ODBC data source to the old SQL2008 database, but not to the SQL2017 database.
What do I need to do to resolve this?
ADO doesn't use "Drivers" that's ODBC terminology. It uses OleDb "Providers". There are three OleDb providers for SQL Sever (in addition to MSDASQL, the OleDb Provider for ODBC drivers which shouldn't be necessary for SQL Server, since it has native OleDb providers).
The old, SQLOLEDB provider has been included in Windows since Windows Server 2003, and it's probably the right one to connect from your old server. The current one is MSOLEDBSQL, and is available here, and should be used whenever possible instead of the old SQLOLEDB or the newer, but deprecated SQLNCLI provider.
Here's a VbScript that you can use to test connections from any Windows computer.
Wscript.Echo "SQLOLEDB"
set theConnection = createobject("ADODB.Connection")
theConnection.ConnectionString = "Provider=SQLOLEDB;Data Source=localhost;Integrated Security=SSPI"
theConnection.Open ConnectString
Wscript.Echo "SQLNCLI"
set theConnection = createobject("ADODB.Connection")
theConnection.ConnectionString = "Provider=SQLNCLI;Data Source=localhost;Integrated Security=SSPI"
theConnection.Open ConnectString
Wscript.Echo "MSOLEDBSQL"
set theConnection = createobject("ADODB.Connection")
theConnection.ConnectionString = "Provider=MSOLEDBSQL;Data Source=localhost;Integrated Security=SSPI"
theConnection.Open ConnectString
Save it to a .vbs file and run it with the command:
c:\test> cscript test.vbs

Why is my connection to a local SQL Server db failing?

Based on what I learned here, I'm using this connection string:
using (var conn = new SqlConnection(#"AttachDBFilename=C:\HoldingTank\AdventureWorksLT2012_Database\AdventureWorksLT2012_Data.MDF;Integrated Security=True;User Instance=true"))
...to (attempt to) attach to a local SQL Server db, but I get this exception at runtime:
System.Data.SqlClient.SqlException was unhandled by user code
HResult=-2146232060
Message=A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Source=.Net SqlClient Data Provider
Based on my legacy MS Access connection string, I had previously also had:
Provider=System.Data.SqlClient;
prior to the "AttachDBFilename=..." part of the connection string, but that caused an exception of its own...
You are missing a few values in the connection string. Here is one I used recently:
"Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\mydbname.mdf;Initial Catalog=mydbname;Integrated Security=True"
I'm thinking the most likely culprit is the lack of the "Initial Catalog" value.
The connection string you need will depend on several factors, such as the edition of SQL Server (LocalDB/Express/Standard), whether or not it's a named instance, and the type of authentication (SQL vs Integrated) you have in place.
The answer from #zippit is a good example of a connection string for LocalDB using integrated security.
The same string to a Sql Express server would look like this:
"Data Source=serverNameOrIpAddress\sqlepxress;AttachDbFilename=|DataDirectory|\mydbname.mdf;Initial Catalog=mydbname;Integrated Security=True"
..and to a standard edition of Sql Server would look like this:
"Data Source=serverNameOrIpAddress\sqlepxress;AttachDbFilename=|DataDirectory|\mydbname.mdf;Initial Catalog=mydbname;Integrated Security=True"
..and to a named instance on a standard edition of Sql Server would look like this:
"Data Source=serverNameOrIpAddress\instanceName;AttachDbFilename=|DataDirectory|\mydbname.mdf;Initial Catalog=mydbname;Integrated Security=True"
All those assume integrated authentication. If you have sql authentication set up, you would substitute "Integrated Security=True" with "User Id=username; Password=pword;"
Also, if the sql server is on the same machine, you can use this for the Data Source parameter for Sql Express
.\sqlexpress
..and this for standard Sql Server
(local)
Here's a site I've found useful: Connection Strings

How to write connection string to connect from classic ASP page to SQL 2008 R2?

I connect from classic ASP page to SQL SERVER 2008 R2 with following connection string
"Data Source=(local);Initial Catalog=my_db;Persist Security Info=True;User ID=my_user;Password=my_pass;"
but I get error
Microsoft OLE DB Provider for ODBC Drivers: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
I verified that my_db exists!, so what is wrong in my connection string?
The (local) name you've given for your data source is invalid. It should be an IP address, server name or server/instance name of your SQL installation.
UPDATE
If the SQL server is on the webserver you can use . to specify local. Here's an example connection string from one of my classic ASP projects:
"Provider=SQLNCLI10;Server=.;Database=my_db;Uid=user;Pwd=pass;"
You need to specify an oledb provider to prevent it trying to default to ODBC (and failing)
Provider=SQLNCLI10;Data Source=(local);Initial Catalog=my_db;User ID=XXX;Password=YYY;
(You also need the 2008 SQL Client OleDb provider installed on the machine)
try to change (local) with : 127.0.0.1
Try:
Provider=SQLNCLI10;Server=(local);Database=my_db;Uid=my_user; Pwd=my_pass;
If in doubt, check connectionstrings.com.

Windows server 2003 problem connection to remote sql server

I have this connection string:
Driver={SQL Server}; Server=XX.XXX.XX.XXX; Database={databasename}; Uid={username}; Pwd={password};
I have even tried
Driver={SQL Server}; Server=XX.XXX.XX.XXX; Database={databasename}; Data Provider = SQLOLEDB; NETWORK LIBRARY = DBMSSOCN; Uid={username}; Pwd={password};
but i get
Microsoft OLE DB Provider for ODBC
Drivers error '80004005'
[Microsoft][ODBC SQL Server
Driver][DBNETLIB]SQL Server does not
exist or access denied.
what is the solution? i read on msdn and microsoft and accross the web but couldn't fix it.
i don't have access to the sql server, it belong to another company that give me the mailing list service.
from my home computer (windows 7) i can connect with anything i try but from the server it is not working

Cannot connect to remote MS SQL Server Express 2008 R2 from OLE DB

I cannot connect to remote MS SQL server express 2008 R2. On the server, the external connection is enabled, firewall is turned off.
When my friend tried to connect using python, everything is OK.
I am using Visual C++ and OLE DB under VS 2010.
Error I get is:
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Thanks
the solution is to use Open method of CDataSource clas instead of OpenFromInitializationString..
You can try to use adodb connection, for example:
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","rsEOF")
_ConnectionPtr m_conn;
_RecordsetPtr m_rs;
::CoInitialize(NULL);
try
{
m_conn.CreateInstance("ADODB.Connection");
CString conn="Driver={SQL Server};server=(127.0.0.1);uid=sa;database=pubs";
if(FAILED(m_conn->Open((_bstr_t)conn,"","",-1)))
{
::MessageBox(NULL,"connect database error","error",MB_ICONEXCLAMATION);
return;
}
}
m_rs.CreateInstance("ADODB.Recordset");
CString strSql = "select * from Table";
m_rs=m_conn->Execute((_bstr_t)strSql,NULL,adCmdText);
I hope this can avail for you

Resources