Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Example of ASP & ADO and ASP.NET & ADO.NET

Status
Not open for further replies.

mmorancbt

IS-IT--Management
Nov 11, 2002
367
0
0
US
Prior life – with ADO and ASP

Assumption:
Function in an include file called DBConn creates a database connection

Code:
Set cnn = DBConn()
strSQL = “select * from table1”
strSQL2 = “Select * from table2 where id = 0” ‘ will be blank
Set rs1 = cnn.execute(strSQL)
Set rs2 = server.createobject(“adodb.recordset”)
Rs2.open strSQL2, cnn, 1, 3



While Not rs1.eof
  Rs2.addnew
  Rs2(“field1”) = rs1(“fieldname1”)
  Rs2(“field2”) = rs1(“fieldname2”)
  Rs2.update
  Rs2.movefirst
  intIdentityField = rs2(“ID”)
  rs1.movefirst
Wend

Assuming I am going to do something with that identity field, what is the ASP/ADO.NET equivalent?


Matthew Moran (career blog and podcast below)
Career Advice with Attitude for the IT Pro
 
(in C#)
First, there are now classes for accessing data in the different data sources. Some examples are:

System.Data.Odbc: for creating DSNs through ODBC drivers
System.Data.OleDb: MSFT Access style
System.Data.SqlClient: SQL Server
System.Data.OracleClient: Oracle
... etc.

Your code will differ based on the data source you're using.

With that out of the way, let's assume SQL Client.

Code:
System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection("<Connection string>");

try
{
	cn.Open();
	System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
	cmd.Connection = cn;
	cmd.CommandText = "Select * From YourTable";
	System.Data.SqlClient.SqlDataReader dr = cmd.ExecuteReader();
	while (dr.Read())
	{
		string value1 = dr[0].ToString();
		string value2 = dr[1].ToString();
	}

}
catch(System.Data.SqlClient.SqlException sqlEx)
{
	Messagebox.Show(sqlEx.ToString());
}
finally
{
	// always close your connection in the finally section of a try, catch, finally block
	cn.Close();
}

Of course, that's just the beginning. You have 4 execution types on a command, and you can additionally include things like DataAdapters and DataSets/DataTables, etc. Its pretty darned cool. And there's tons of web resources on it. I bought a WROX press Databases and ASP.NET book a couple of years ago that helped out a lot.

______________________________________________
When told, "goto hell", a programmer finds the method, not the destination as harmful.
 
Thanks. I got it working but your example will be helpful as well. I am thinking of compiling some - how I used to and how I do now documents to post on the web for developers who used to do classic .asp and want to do asp.net...

Matthew Moran (career blog and podcast below)
Career Advice with Attitude for the IT Pro
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top