Unable to connet to SQL server using soci library - c++

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;"

Related

ADO Connection to MariaDB

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"...

ODBC Connection via CDatabase fails with connection string from previous succesfull connection

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?

Cannot make connection to SQL Server via ODBC

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.

Sample stored procedure execute from c++ program

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.

can't connect to database by executable file

i wrote a program that need to connect to database to insert some data , my executable file not connect to database but when i checked with code connection established! i don't know what is problem , do you know what is problem?
i use Qt to connect to database and my database is on mySql and here is how i connect :
soccer_db = QSqlDatabase::addDatabase("QMYSQL" , "sss");
soccer_db.setHostName(addrrFile.c_str());
soccer_db.setDatabaseName("sss");
soccer_db.open();
if (!soccer_db.open()){
emit dsignal("ssss not opened. Ckech whether server is down or change config file");
return false;
}
You are getting a failure because you try to open the database twice. The first attempt succeeds but the second one fails. Remove the first call to open, like this
soccer_db = QSqlDatabase::addDatabase("QMYSQL" , "sss");
soccer_db.setHostName(addrrFile.c_str());
soccer_db.setDatabaseName("sss");
if (!soccer_db.open()){
emit dsignal("ssss not opened. Ckech whether server is down or change config file");
return false;
}
i want to answer my question, in qt you should address your files completely not relative address, if you need to address completely you can give current directory by QtDir