Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
Available now!
Buy at Amazon US or
Buy at Amazon UK



Articles

1.Windows API reference
2.HTML to WML Converter
3.Webcam streaming in VB.NET
4.Remoting with firewalls
5.RSA from first principles
6.Key & MouseLogger in .NET
7.Networking Resource Kit for .NET
8.Automatic Reboot with .NET
9.XAML Schema
10.Migrating VB6 Winsock to VB.NET
11.Migrating C++ sockets to C#
12.RFC Reference guide
13.Lingua - Localization webservice
14.COM Reference guide
15.WMI Reference guide
16.SQL stored procedures
17.TCP & UDP port reference
18..NET Framework reference
19.Ethernet Type codes
21.MAC address assignments
22.DLL entry point reference
23.WHOIS server list
24. Turing Numbers
25. Boost SQL performance
26. Progress Bar in ASP.NET
27. OleDb WebService
27. Internet Explorer

Contact us

C#

private string Query(string field,string szSQL,string szDSN)

{

   OleDbConnection DSN = new OleDbConnection(szDSN);

   DSN.Open();

   OleDbCommand SQL = new OleDbCommand(szSQL,DSN);

   OleDbDataReader dataReader = SQL.ExecuteReader();

   bool success = dataReader.Read();

    if (success)

    {

            return dataReader[field].ToString();

    }

    else

    {

            return null;

    }

}

private void nonQuery(string szSQL,string szDSN)

{

    OleDbConnection DSN = new OleDbConnection(szDSN);

    DSN.Open();

    OleDbCommand SQL = new OleDbCommand(szSQL,DSN);

    SQL.ExecuteNonQuery();

}

VB.NET

Private Function Query(ByVal field As String, ByVal szSQL As String, _

                       ByVal szDSN As String) As String

   Dim DSN As OleDbConnection =  New OleDbConnection(szDSN)

   DSN.Open()

   Dim SQL As OleDbCommand =  New OleDbCommand(szSQL,DSN)

   Dim dataReader As OleDbDataReader =  SQL.ExecuteReader()

   Dim success As Boolean =  dataReader.Read()

   if success=true then

     return dataReader(field).ToString()

    else

    return nothing

   end if

End Function

Private  Sub nonQuery(ByVal szSQL As String, ByVal szDSN As String)

    Dim DSN As OleDbConnection =  New OleDbConnection(szDSN)

    DSN.Open()

    Dim SQL As OleDbCommand =  New OleDbCommand(szSQL,DSN)

    SQL.ExecuteNonQuery()

End Sub

The two functions Query and nonQuery are both private, because remote clients should not have direct access to the database. Both functions execute statements against the database, but only the Query function retrieves data back from the database, which would be passed to the calling function.

Looking at the Query function first off. It is passed the name of the field to be returned, the SQL statement, and the connection string. It takes several discrete steps to execute a statement against a database. The first step is when a connection is established with the database. This is done through the OleDbConnection object, which is passed the connection string in the constructor. The connection is then opened, and an OleDbCommand object is instantiated. The constructor of this object is passes the SQL statement and the OleDbConnection object.

The OleDbCommand then executes the statement when the ExecuteReader method is called. This method returns an OleDbDataReader object, which is used to read data row by row from the result set. Only the top-most row is read, and of that, only the specified field of that row is returned.

The nonQuery function is somewhat simpler. As before, a connection is established with the database through the OleDbConnection object. This object is passed the connection string in the constructor. The connection is then opened, and an OleDbCommand object is instantiated. The constructor of this object is passes the SQL statement and the OleDbConnection object. This time the ExecuteNonQuery method is where the database actually processes the statement. There is no return value to this function. 

At this point, you should compile the code, and note the location of the generated DLL. Create a new Windows application project in Visual Studio .NET. This will become the server application, and as such will not require any special user interface. This is almost a direct copy of the code detailed in the web services chapter.

Click on the form, and add this code.

C#

private void Form1_Load(object sender, System.EventArgs e)

{

    HttpChannel channel = new HttpChannel(8085);

    ChannelServices.RegisterChannel(channel);

    RemotingConfiguration.RegisterWellKnownServiceType(

            typeof(p2pfirewall.tunnel),

            "Firewall_Tunnel",

            WellKnownObjectMode.Singleton);

}

Page 3  Page 5 



Google

Copyright 2012 Open Merchant Account Ltd.
Free SMS UK Free SMS Ireland SMS Gratis Norway SMS Gratis Sverige Ilmainen SMS Suomi SMS Gratis Danmark SMS Tasuta Eestisse SMS Nemokamai Lietuva SMS Bezmaksas Latviju Darmowe smsy Polska SMS Zdarma Ceské SMS Zdarma Slovensko SMS Gratis Deutschland SMS Gratis Schweiz SMS Gratis Österreich SMS Gratuit Belgique SMS Gratis Nederland SMS Gratuit France SMS Gratis Espańa SMS Gratis Portugal Free SMS South Africa Free SMS USA SMS Percuma Malaysia Free SMS Hong Kong