One machine can do the work of fifty ordinary men. No machine can do the work of one extraordinary man

Sunday, May 20, 2012

C++ Program Connect with SQL Database


First i am going to show you how to build up the connection between SQL database and the C++ application that you have created.

1st STEP
=======

First you have to add the following name spaces.

using namespace System::Data;
using namespace System::Data::SqlClient;

2nd STEP
=======

Once you add this namespaces then you have to set the connection string and you have to open the connection before you interact with database(inside the particular method that you want to call the query).

SqlConnection ^cn = gcnew SqlConnection();
cn->ConnectionString ="Server=localhost;Database=NVIS;Integrated Security=true";
cn->Open(); 

In here my server is localhost and the database is NVIS.

3rd STEP
=======

If you want to execute the insert query following code should be added.

SqlCommand ^insertCommand 
= gcnew SqlCommand(("insert into Vessel values ('"+gcnew String(b.Name)+"', '"+gcnew String(b.Type)+"', '"+gcnew String(b.Signature)+"', "+b.MaximumSpeed+", "+b.Length+", "+b.MaximumRange+", "+b.MaximumDisplacement+", "+b.NumberOfCrew+");")  ,  cn  );

insertCommand->ExecuteNonQuery();

In here first parameter is the Query that you want to execute (string type), and the second parameter is the Connection (SqlCommad Type).

If you want to do a update or delete you can use the same procedure.

4th STEP
=======

If you do selection,
1) Select a single  row only
2) Select more than one row

1) Select a single  row only
---------------------------
SqlConnection ^cn = gcnew SqlConnection();
  
SqlCommand ^selectQuery = gcnew SqlCommand("select * from Vessel where vesselName='"+txtSearch->Text+"'",cn);

SqlDataReader ^ reader;
reader = selectQuery->ExecuteReader();

reader->Read();

try{txtName->Text=reader->GetString(0);}catch(...){}
try{cmbType->Text=reader->GetString(1);}catch(...){}
try{txtSignature->Text=reader->GetString(2);}catch(...){}
try{txtMaxSpeed->Text=reader->GetDouble(3).ToString();}catch(...){}
try{txtLength->Text=reader->GetDouble(4).ToString();}catch(...){}
try{txtMaxRange->Text=reader->GetDouble(5).ToString();}catch(...){}
try{txtMaxDisplacement->Text=reader->GetDouble(6).ToString();}catch(...){}
try{txtCrew->Text=reader->GetInt32(7).ToString();}catch(...){}

reader->Close();

2) Select more than one row
----------------------------
    SqlCommand ^selectQuery = gcnew SqlCommand("select * from Vessel",cn);
itm = new ItemOnList[count];
SqlDataReader ^ reader;
reader = selectQuery->ExecuteReader();

int i=0;
while(reader->Read())
{
try{u.StringToChar(itm[i].Name, reader->GetString(0));}catch(...){}
try{u.StringToChar(itm[i].Type, reader->GetString(1));}catch(...){}
try{u.StringToChar(itm[i].Signature, reader->GetString(2));}catch(...){}
try{itm[i].MaxSpeed=reader->GetDouble(3);}catch(...){}
try{itm[i].Length=reader->GetDouble(4);}catch(...){}
try{itm[i].MaxRange=reader->GetDouble(5);}catch(...){}
try{itm[i].MaxDisplacement=reader->GetDouble(6);}catch(...){}
try{itm[i].Crew=reader->GetInt32(7);}catch(...){}
    }

Here are all the codings to connect with SQL Database and the C++ Program.
If there are any issue or a question please don't forget to put a comment.










6 comments:

  1. Where I can find DATA and SQLCLIENT class?

    ReplyDelete
    Replies
    1. Those are in-built classes and you can easily import them.

      Delete
    2. hi hiran can i get c++ application connect to oracle datbase

      Delete
    3. Oracle C++ Call Interface (OCCI) is an Application Programming Interface (API) that provides C++ applications access to data in an Oracle database. Try that out.

      Delete
  2. can you tell me exact code of sql connect with c++.for log in page

    ReplyDelete