Sunday, April 29, 2007

ADO.net - DataSet vs. DataReader

ADO.net
  • DataSet vs. DataReader
DataReader [while(r.Read())]

When a database query is being executed, DataReader reads the rows one by one and populates the result, let's say on the web page making the request.
Important feature: Data is displayed as soon as partial results become available.
-Works as read-only. Can't update the DB.
-Always maintains connection.

DataSet [foreach(...)]

-Important feature: Data is not displayed until the whole result of the query is received. Once we get the whole result, we can start displaying the rows of data.
-Shouldn't be considered as a local copy of a DB. Can hold data from different tables and relate them.
-Enables operations like binding.
-Provides flexibility to create a datatable on the memory, on-the-fly.

DataAdapter

is used to retrieve data from a DB. Can also update the DB back.


  • Examples: (DataSet File name: DataSet1.xsd)

DataReader example:

private void Query_Click(object sender, EventArgs e)
{
// NO SqlDataAdapter IS NEEDED!!!
SqlConnection objConn = new SqlConnection();
objConn.ConnectionString = "Data Source=hc\\sqlexpress; integrated security=SSPI; Initial Catalog=ContructionProject";

objConn.Open();
SqlCommand objCmd = new SqlCommand("myStoredP_1", objConn);
objCmd.CommandType = System.Data.CommandType.StoredProcedure;

SqlDataReader myreader = objCmd.ExecuteReader();

while (myreader.Read())
{
listBox1.Items.Add(myreader[0].ToString()+" " + myreader[1].ToString());
}

objConn.Close();
}

DataSet example with Fill() method:

private void myDataSet_Query_Click(object sender, EventArgs e)
{
SqlConnection objConn = new SqlConnection();
objConn.ConnectionString = "Data Source=hc\\sqlexpress; integrated security=SSPI; Initial Catalog=ContructionProject";
objConn.Open();
SqlCommand objCmd = new SqlCommand("myStoredP_1", objConn);
objCmd.CommandType = System.Data.CommandType.StoredProcedure;

SqlDataAdapter a = new SqlDataAdapter(objCmd);
DataSet1 s = new DataSet1();
a.Fill(s, s.myStoredP_1.TableName);

foreach (DataSet1.myStoredP_1Row dr in s.myStoredP_1.Rows)
{
MessageBox.Show(dr.FName.ToString());
}

//foreach (DataRow dr in s.Tables[0].Rows)
//{
// MessageBox.Show(dr[1].ToString() + dr[0].ToString());
//} /*This foreach loop works as well*/

objConn.Close();
}

DataSet example with GetData() method:

private void DataSet_GetData_Click(object sender, EventArgs e)
{
// NO NEED TO USE SQLCOMMAND OR CONNECTION STRING!!

DataSet1 dseg = new DataSet1();
DataSet1TableAdapters.myStoredP_1TableAdapter egAdap = new DataSet1TableAdapters.myStoredP_1TableAdapter();

DataSet1.myStoredP_1DataTable dt = egAdap.GetData();
foreach (DataSet1.myStoredP_1Row dr in dt.Rows)
{
listBox1.Items.Add(dr.ID + " " + dr.FName);
}

}

* ADO.net does not support bulk inserts to the DB!
* System.Data namespaces: SqlClient, OleDb, ODBC, OracleClient
* Use of ODBC is discouraged because it is slow, unsecure and has limited functionality.
* Oracle: No multiple SELECT statements!

Friday, April 27, 2007

C# programming notes



  • Two references to the same object--Interesting!!!



Source: C# 2005 for Dummies, Wiley.

Important Subjects

Single point of failure


Load balancing

High-availability

Caching


thin client

thick client


Replay Attacks


Proxy

Credentials

Single sign-on

ClearTrust
Strong password -Weak password

Connection Pool

Windows Service

Monday, April 23, 2007

Web.config

WEB.CONFIG file in a web application

  • Allow / Deny users
Below code will deny access to the anonymous users and Windows users with a Guests Role. However, the user yyyy under xxx domain will be granted access.


allow users="xxx\yyyy">
deny users="?">
deny roles="Guests">

  • WARNING!
allow users="*">
deny users="yyyy">


Due to the design of .NET, the above will grant access to all authenticated users even though yyyy seems to be denied!

  • When WEB.CONFIG is updated (even with an insignificant change, like addition of a space character)
-The entire web application will be forced to restart This is an enhancement over .NET 1.1
-What if, I don't want my application to restart each time an update is made to WEB.CONFIG in .NET 2.0?
Solution: Create another configuration file like easy.config and put the most frequently changing setting in this file.

appsettings configsource="easy.config">

Good practice: Create a seperate config file for Connection Strings.

Adding a key & value pair in WEB.CONFIG:

appSettings>
add key="x" value="5"/>
/appSettings>

  • Publishing the site locally:
This is also an enhancement in .NET 2.0. Whenever we publish the site locally, the .NET framework creates a .dll and the .aspx files for our access.
This is especially useful if we don't have IIS (Internet Information Services) installed.
We can easily debug our web application in our local PC.

  • Reconfiguration of ASP.NET: (a real-time problem)
In some instances, we may need to reconfigure ASP.NET.
(For example, if the IIS was installed later than ASP.NET.)

run the ASP.NET reconfiguration utility:

> aspnet_regiis.exe -u (for uninstallation)
> aspnet_regiis.exe -i (for installation)

  • BONUS knowledge for IIS:
Suppose that somebody is continuously trying to access your ftp site.
The person is not granted access but the bandwidth will be used unwantedly.
I can get the IP address of this unwanted internet user by checking my log file. And I want to block this IP addresss from my ftp site.
IIS can not do this! (Even though it can not, it is good to know :) )
However, this IP-denial can be achieved in proxy-level. HOW??