UrbanPro

Learn SQL Programming from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

How do I connect to a MS SQL database with C++?

Asked by Last Modified  

Follow 1
Answer

Please enter your answer

Senior Business Analyst

#include "stdafx.h" #include "iostream" #include "windows.h" #include "sqltypes.h" #include "sql.h" #include "sqlext.h" using namespace std; int main() { #define SQL_RESULT_LEN 240 #define SQL_RETURN_CODE_LEN 1000 //define handles and variables SQLHANDLE sqlConnHandle; ...
read more
#include "stdafx.h" #include "iostream" #include "windows.h" #include "sqltypes.h" #include "sql.h" #include "sqlext.h" using namespace std; int main() { #define SQL_RESULT_LEN 240 #define SQL_RETURN_CODE_LEN 1000 //define handles and variables SQLHANDLE sqlConnHandle; SQLHANDLE sqlStmtHandle; SQLHANDLE sqlEnvHandle; SQLWCHAR retconstring[SQL_RETURN_CODE_LEN]; //initializations sqlConnHandle = NULL; sqlStmtHandle = NULL; //allocations if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sqlEnvHandle)) goto COMPLETED; if (SQL_SUCCESS != SQLSetEnvAttr(sqlEnvHandle, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0)) goto COMPLETED; if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_DBC, sqlEnvHandle, &sqlConnHandle)) goto COMPLETED; //output cout << "Attempting connection to SQL Server..."; cout << "\n"; //connect to SQL Server //I am using a trusted connection and port 14808 //it does not matter if you are using default or named instance //just make sure you define the server name and the port //You have the option to use a username/password instead of a trusted connection //but is more secure to use a trusted connection switch (SQLDriverConnectW(sqlConnHandle, NULL, (SQLWCHAR*)L"DRIVER={SQL Server};SERVER=ServerAddress, 1433;DATABASE=DataBaseName;UID=DataBaseUserName;PWD=PassWord;", //(SQLWCHAR*)L"DRIVER={SQL Server};SERVER=localhost, 1433;DATABASE=master;Trusted=true;", SQL_NTS, retconstring, 1024, NULL, SQL_DRIVER_NOPROMPT)) { case SQL_SUCCESS: cout << "Successfully connected to SQL Server"; cout << "\n"; break; case SQL_SUCCESS_WITH_INFO: cout << "Successfully connected to SQL Server"; cout << "\n"; break; case SQL_INVALID_HANDLE: cout << "Could not connect to SQL Server"; cout << "\n"; goto COMPLETED; case SQL_ERROR: cout << "Could not connect to SQL Server"; cout << "\n"; goto COMPLETED; default: break; } //if there is a problem connecting then exit application if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_STMT, sqlConnHandle, &sqlStmtHandle)) goto COMPLETED; //output cout << "\n"; cout << "Executing T-SQL query..."; cout << "\n"; //if there is a problem executing the query then exit application //else display query result if (SQL_SUCCESS != SQLExecDirectW(sqlStmtHandle, (SQLWCHAR*)L"SELECT @@VERSION", SQL_NTS)) { cout << "Error querying SQL Server"; cout << "\n"; goto COMPLETED; } else { //declare output variable and pointer SQLCHAR sqlVersion[SQL_RESULT_LEN]; SQLINTEGER ptrSqlVersion; while (SQLFetch(sqlStmtHandle) == SQL_SUCCESS) { SQLGetData(sqlStmtHandle, 1, SQL_CHAR, sqlVersion, SQL_RESULT_LEN, &ptrSqlVersion); //display query result cout << "\nQuery Result:\n\n"; cout << sqlVersion << endl; } } //close connection and free resources COMPLETED: SQLFreeHandle(SQL_HANDLE_STMT, sqlStmtHandle); SQLDisconnect(sqlConnHandle); SQLFreeHandle(SQL_HANDLE_DBC, sqlConnHandle); SQLFreeHandle(SQL_HANDLE_ENV, sqlEnvHandle); //pause the console window - exit when key is pressed cout << "\nPress any key to exit..."; getchar(); } read less
Comments

Related Questions

I need your help to choose my career as I have three years of gap between my education. I know SQL. Can anyone tell me should I choose MS SQL server or oracle because I am interested in the database? Which one will be better to learn to get into the IT industry? Please help me! Thank you!
As you are strong and interested in SQL, why not to choose ETL Testing? It has a good career ahead in data projects. To survive in the market, you need both Oracle & SQL Server. Actually, both are almost same but are different products.
Santhosh
How do I become master in SQL server? I am looking for real time case studies to write queries, stored procedures etc.
Hi Komal, Try to command basic SQL statement first. Understand the very basic concepts of DDL , DML and TCL commands.. start with writing SELECT statement with WHERE clause , using aggregate functions...
Komal
0 0
5
I want to teach, how can I?
You can teach students Online as well as offline. If you want to teach online you need to have a communicating media.
Yasheela

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

Truncate, Drop And Delete Commands In SQL
Truncate: Truncate command will delete the Table data only, it keeps the table schema as it is. Table data Can be rollback Example : TRUNCATE TABLE User; Drop: Drop command will delete both...

Database Origins
The need for a database originates from the fact that computers are dumb devices having batman like abilities which can be smartly be utilised by feeding it the right content. That is, for example, consider...
M


How does a SQL Query Execute inside Oracle? What are the steps followed in the background?
The above snapshot says it all. Hence it is said, 'Picture says it all'. Basically, there are 4-5 steps. There are two possible ways of query being executed. 1. Soft Parse- Improves performance....

SQL Tips (1 to 3)
SQL tip 1: Instead of Full Outer Join, you can use Union All. Difference is, Full Outer Join provides results side by side. Union All provides result s one by one. Note: MYSQL does not support Full...

Recommended Articles

Business Process outsourcing (BPO) services can be considered as a kind of outsourcing which involves subletting of specific functions associated with any business to a third party service provider. BPO is usually administered as a cost-saving procedure for functions which an organization needs but does not rely upon to...

Read full article >

Almost all of us, inside the pocket, bag or on the table have a mobile phone, out of which 90% of us have a smartphone. The technology is advancing rapidly. When it comes to mobile phones, people today want much more than just making phone calls and playing games on the go. People now want instant access to all their business...

Read full article >

Whether it was the Internet Era of 90s or the Big Data Era of today, Information Technology (IT) has given birth to several lucrative career options for many. Though there will not be a “significant" increase in demand for IT professionals in 2014 as compared to 2013, a “steady” demand for IT professionals is rest assured...

Read full article >

Information technology consultancy or Information technology consulting is a specialized field in which one can set their focus on providing advisory services to business firms on finding ways to use innovations in information technology to further their business and meet the objectives of the business. Not only does...

Read full article >

Looking for SQL Programming Training?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for SQL Programming Classes?

The best tutors for SQL Programming Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn SQL Programming with the Best Tutors

The best Tutors for SQL Programming Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more