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.

Friday, July 1, 2011

WPF transparent form application

1) First you have to create a new WPF application project
                                           File -> New -> project
select WPF application and press OK

2) Then you have to create a circle inside the form using the Ellipse tool in the toolbox
3) After that you have to do few changes to the XAML coding


Your  coding appear like the following


<Window x:Class="MyApp.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Ellipse Margin="-1,-1,-1,1" Name="ellipse1" Stroke="Black"/>
    </Grid>
</Window>


That must change like following


<Window x:Class="MyApp.Window1" MouseLeftButtonDown="Window_MouseLeftButtonDown" WindowStyle="None" AllowsTransparency="True" Background="Transparent"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Ellipse Margin="-1,-1,-1,1" Name="ellipse1" Stroke="Black" Fill="Gray" Opacity="0.5"/>
        <Button Height="23" Margin="99,28,104,0" Name="button1" VerticalAlignment="Top">Close</Button>
    </Grid>
</Window>


following picture show you the changes by underlining newly added parts
4) Then double click on the button and write the following coding part
               Close();
5) Then write the following code inside the "Window_MouseLeftButtonDown" method
               DragMove();
   You can see the above 2 steps from the picture
6) Finally build the solution and see the result. you can drag the form by draging the content of the form
This is a very basic WPF form application. You can develop any complex interfaces or application same as the normal windows form application by using this theory