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