In the past, I have used ADO to access SQL Server, the connection string for the ADO connection object is:
Provider=sqloledb;Data Source=MYPC;Integrated Security=SSPI;
where MYPC is the name of my computer, and SQL Server is installed on my computer as the default instance.
The above connection string works well.
However, now it is said that ADO is outdated and ODBC is recommended again by Microsoft (see https://blogs.msdn.microsoft.com/sqlnativeclient/2011/08/29/microsoft-is-aligning-with-odbc-for-native-relational-data-access/ ), so I have to modify my code to use ODBC instead.
So I changed the connection to SQL Server to the following code:
CDatabase Database;
// Provider=sqloledb;Data Source=MYPC;Integrated Security=SSPI;
Database.OpenEx(_T("Driver = {SQL Native Client}; Server = MYPC; Trusted_Connection = yes;"), CDatabase::noOdbcDialog);
Database.ExecuteSQL(_T("create database [MyDB2019] on primary (name=[MyDB2019_File],filename='F:\\MyDB2019.mdf')"));
Database.Close();
However, this code does not work. After executing Database.OpenEx, there will be a CDBException indicating
Data source name not found and no default driver specified.
Why?
Note: I am using Visual C++ 2008 and ADO
The error/exception is pointing to the problem (albeit it seems a bit generic at first).
"Data source name not found and no default driver specified"
Data source name not found --> You did not specify a DSN, you do not
want to use a DSN, ignore this part
.
and no default driver specified --> You intend to use a driver, so this part of the error most likely applies to you.
The connection string appears syntactically correct: "Driver={Driver Name}...", so the next
step is to check whether the driver you try to use, named SQL Native Client, exists
on your machine.
"SQL Native Client" was/is the driver name for SQL Server 2005.
From SQL2008 the driver name got a version descriptor.
Driver={SQL Native Client} ,sql2005
Driver={SQL Server Native Client 10.0} ,sql2008
Driver={SQL Server Native Client 11.0} ,sql2012 and later
Driver={ODBC Driver 11 for SQL Server} ,sql2012 and later, odbc appears in the name
Driver={ODBC Driver 13 for SQL Server}
Driver={ODBC Driver 17 for SQL Server}
An easy way to find the installed 'SQL Native' and 'ODBC Driver for SQL Server' drivers on your machine, would be to run the ODBC Data Source Administrator. Type Odbc data sources in your search box or odbcad32.exe in the command/address bar (for 64bit: %windir%\system32\odbcad32.exe)
When in ODBC Data Source Administrator, switch to the Drivers tab and there you will find all the available/installed drivers, at your disposal.
Here is a powershell script that opens a connection to localhost using odbc. Adjust accordingly to your installed driver(s)
cls
$conn = new-object system.data.odbc.odbcconnection
$conn.connectionstring =
# all these installed on my pc and working
#"Driver={SQL Server Native Client 11.0};Server=localhost; Database=master;Trusted_Connection=yes;"
#"Driver={SQL Server};Server=localhost; Database=master;Trusted_Connection=yes;"
#"Driver={ODBC Driver 11 for SQL Server};Server=localhost; Database=master;Trusted_Connection=yes;"
"Driver={ODBC Driver 17 for SQL Server};Server=localhost; Database=master;Trusted_Connection=yes;"
$conn.Open()
$conn.State
$conn.Close();
..and an mfc button
void CMFCDBTestDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
CDatabase database;
CString connectionstring = _T("Driver={SQL Server Native Client 10.0};Server=localhost;Trusted_Connection=yes;");
CString messagetext;
TRY{
//database.Open(NULL, FALSE, FALSE, connectionstring, FALSE); //ok
database.OpenEx(connectionstring, CDatabase::noOdbcDialog); //ok
if (database.IsOpen()){
messagetext = _T("open, database:") + database.GetDatabaseName();
database.Close();
}
}CATCH(CDBException, e) {
messagetext = _T("Database error: ")+e->m_strError;
}
END_CATCH;
AfxMessageBox(messagetext, 0, 0);
}
Create a file with udl extension. Example: test.udl
Double click the file. Use the GUI to connect to your database. Once done, open the UDL file using notepad and extract the connection string.
See https://www.dmxzone.com/go/312/how-to-generate-an-ado-connection-string/
The problem comes from the extra spaces in the connection string, after removing extra spaces from:
"Driver = {SQL Native Client}; Server = MYPC; Trusted_Connection = yes;"
to(and change the driver name as well):
"Driver={SQL Server Native Client 10.0};Server=MYPC;Trusted_Connection=yes;"
The connection will succeed.
Related
I try to connect in a C++ via Ado to a MariaDB Database.
CADODatabase m_dbConnection;
(my test.h)
m_dbConnection.SetConnectionString(strConnectionString);
m_dbConnection.SetConnectionTimeout(60);
Connection String is
DRIVER={MariaDB ODBC 3.1 Driver}; Server=xx.xx.xx.xx.xx; Port=3306; database=db; UID=8001; password=********;
I tried to setup a DSN but I donĀ“t know how the connection string has to look like?
DSN=Name;
Is it important if the program is a 32 or 64 bit version? which DSN is used in this case?
I always get the error:
Init(): Could not connect to database with connection string 'DSN=Cue1 DATABASE=yesdb'
Error message: Connection String = DSN=Cue1 DATABASE=yesdb
CADODataBase Error
Code = 80004005
Code meaning = Unspecified error
Source = Microsoft OLE DB Provider for ODBC Drivers
Description = [Microsoft][ODBC Driver Manager] Data source name not found and no default driver
specified
or
Init(): Could not connect to database with connection string 'Driver={MariaDB ODBC 3.1 Driver}
Server=xx.xx.xx.xx; Port=3306; database=db; UID=8001; password=xxxxxxx;Option=3;'
Error message: Connection String = Driver={MariaDB ODBC 3.1 Driver} Server=xx.xx.xx.xx; Port=3306;
database=db; UID=8001; password=xxxxxx;Option=3;
CADODataBase Error
Code = 80040e73
Code meaning = IDispatch error #3187
Source = Microsoft OLE DB Service Components
Description = Format of the initialization string does not conform to the OLE DB specification.`
Any ideas?
Not sure what library you're using to connect.
CADODatabase m_dbConnection;
I'm not used to seeing CADODatabase. Your connection string looks like what I would expect using the MySQL ADODB library. But usually raw ADO connection strings use a property called "Provider"...
I'm a bit clueless at the moment what the problem is with this code.
It trows a exception of type CDBException when trying to connect with the given ODBC Connect String and never comes back from .OpenEx - the lower message boxes never display.
There is a message from the ODBC Driver that states that the DSN isnt specified and that no default driver is found.
The Message Box that shows the connect string gives a good looking string - as it should, coming from a working connection
ODBC;DSN=DBTEST;Trusted_Connection=Yes;APP=XXX;WSID=NEUROMANCER;DATABASE=master;
Same problem when using SA/Password for the connection
ODBC;DSN=DBTEST_PW;UID=SA;PWD=password;APP=XXX;WSID=NEUROMANCER;DATABASE=master;
After reading other posts I added "DRIVER=" to the beginning and "SERVER=localhost;" to the end but that changed nothing. Resulting tested connection strings where
DRIVER=ODBC;DSN=DBTEST;Trusted_Connection=Yes;APP=XXX;WSID=NEUROMANCER;DATABASE=master;
DRIVER=ODBC;DSN=DBTEST;Trusted_Connection=Yes;APP=XXX;WSID=NEUROMANCER;DATABASE=master;SERVER=localhost
DRIVER=ODBC;DSN=DBTEST_PW;UID=SA;PWD=password;APP=XXX;WSID=NEUROMANCER;DATABASE=master;
DRIVER=ODBC;DSN=DBTEST_PW;UID=SA;PWD=password;APP=XXX;WSID=NEUROMANCER;DATABASE=master;SERVER=localhost
I also made sure to use the 32bit ODBC Admin tool to generate the system DSNs for the 32bit application.
If I replace "CDatabase::NoOdbcDialog" with "0" it opens the ODBC-Dialog again as if informations necessary to open the connection are missing.
db.OpenEx(NULL, CDatabase::forceOdbcDialog);
CString cstmp;
if (db.IsOpen()) {
cstmp = db.GetConnect();
db.Close();
}
MessageBox(cstmp, _T(""), MB_OK);
if (db.OpenEx(cstmp, CDatabase::noOdbcDialog )) { //Exception here CDBException at 0x004FDD9
MessageBox(_T("Connection success!"), _T("Connection"), MB_OK);
db.Close();
}
else {
MessageBox(_T("Connection failed!"), _T("Connection"), MB_OK);
}
Can anyone tell me where my problem is?
I am beginner in c++,my problem on Linux for connecting to SQL Server with soci library, my code this but I have error and I don't found solution for my problem, my code this:
I have error:
[unixODBC][Driver Manager]Data source name not found, and no default driver specified (SQL state IM002)
Here is the code:
try{
soci::session sql("odbc","DSN=ODBC;UID=sa;PWD=sa123; Connection Driver=TDS;Database=ReportServer; Server=192.168.1.52;Port=1433;");
}
catch(soci::odbc_soci_error const & e){
cout<<"start error";
cout<<e.odbc_error_code()<<endl;
cout<<e.what();
}
Your ODBC connection string in incorrect, your first parameter specifies a datasource called "ODBC", which you haven't configured in your operating system/odbc manager.
So either you should configure a data source with the given parameters like UID=sa;PWD=sa123; Connection Driver=TDS;Database=ReportServer; Server=192.168.1.52;Port=1433; with the corresponding ODBC data source manager/linux administration program, with a useable name like "local_test_db" and just use "DSN=local_test_db" as your connection string
or (exclusive or)
remove the "dsn=ODBC" part from your connection string and try it again with "UID=sa;PWD=sa123; Connection Driver=TDS;Database=ReportServer; Server=192.168.1.52;Port=1433;"
I am trying to create an Sqlite database in the selected folder but can`t do this.
Operating System - Windows, editor - Visual studio, progect encoding is unicode. Encoding of input string with database connection unknown. Language - C++
The problem:
I can't create database when I use following connection string: file:C:/Users/Public/Desktop/testDb.sqlite
I am always getting error 14 (SQLITE_CANTOPEN).
I use following function to create database:
CSqliteManager::CSqliteManager(const char* dbName)
{
db = nullptr;
int rc = sqlite3_open(dbName, &db);
if (rc != SQLITE_OK)
{
db = nullptr;
}
}
How should me change connection string to create database fo the sollowing pass:
C://Users/Public/Desctop/MyBD.sqlite
You can't use URI style filenames with sqlite3_open() as they're disabled by default (I'm assuming you didn't enable them globally). Instead you need to use sqlite3_open_v2() with the appropriate option (SQLITE_OPEN_URI). Details and more details.
(Or just not use a URI, of course)
DBPROCESS *dbconnect(DBPROCESS *dbproc)
int i,count;
char choice;
char num[2];
int page;
int index;
unsigned long num_recs = 0;
char db_epass[40], db_user[40], db_pass[40], db_database[60], db_server[40], db_dbname[60];
bool status,valid;
LOGINREC *loginrec;
int y=ScrWidth/2-10;
count=0;
/I WANT TO EXECUTE STORED PROCEDURE FROM HERE/
dbexit();
I'm new to c++ programming, I have the script above where I want to execute a SP with one parameter, passing the information of the 'db_user'.
can anyone guide my how to script it.
Database: mssql 2008
to connect to database and execute various pl/sql command (including stored procedure call) every vendor provides ODBC driver. In your question it is not clear where you are using the ODBC driver in your code and to what database you are trying to connect to. Frist check ODBC driver for your database and read vendor provided documentation/manual about how to use their driver with some examples. Every ODBC driver API are different and codes to execute pl/sql will also be different for different vendor provided ODBC driver APIs. Some third party provided ODBC driver APIs are generic for all the different database and you no need to write separate codes for different database, only database connecting string is different. One of them is datadirect. Read first vendor provided material on ODBC driver and you will get all the information in it. Wish you all the very best.