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.










Thursday, May 10, 2012

How To Read A Weight Scale via Serial Port

For my project that given by my company, I have created a application for take readings inside c# function. This is a serial port connected weighing machine and this is a CAS PDII machine. But technique that I have used is applicable for any serial port connected weighing machine. Before I do this I followed many blogs and sits. But I couldn't find anything that successfully worked. I think this may helpful to you definitely.

First you have to create a separate method to be called to take weight.


Then you have to fill the code in event handler. Following is the code inside the event handler.
You have to keep two double variables value and the weight.


Now connect your weighing to serial port and try to execute. Hope this will help you a lot.