Array:
Using system;
Class Test
{
Static void main ()
{
Int [] tmep = new int [5];
For (INT I = 0; I <temp. length; I ++)
Console. writeline ("TEM [{0}] = {1}", I, TEM [I]);
}}
Reference parameter passing:
Using system;
Class Test
{
Static void swap (ref int A, ref int B ){
Int T =;
A = B;
B = T;
}
Static void main ()
{
Int x = 1, y = 2; // Initialization is required. You do not need
Swap (ref X, ref y );
}
}
Two Methods for connecting to sqlserver
Sqlconnection
String connectionstring = "Server = localhost; uid = sa; Pwd =; database = northwind ";
Sqlconnection myconn = new sqlconnection (connectionstring );
Myconn. open;
Adoconnection
String connectionstring = "provider = sqloledb.1; Data Source = localhost; uid = sa; Pwd =; initial catalog = northwind ";
Adoconnection myconn = new adoconnection (connectionstring );
Myconn. open;
Access the ACCESS database
Add this row to the using statement using system. Data. oledb at the beginning of webform1.aspx. CS;
Insert this code into the page_load method:
Private void page_load (Object sender, system. eventargs E)
{
If (! Ispostback) readrecords ();
}
Add the readrecords method to the petform class following the page_load method:
Private void readrecords ()
{
Oledbconnection conn = NULL;
Oledbdatareader reader = NULL;
Try
{
Conn = new oledbconnection (
"Provider = Microsoft. Jet. oledb.4.0 ;"
"Data Source =" server. mappath ("pets/pets. mdb "));
Conn. open ();
Oledbcommand cmd =
New oledbcommand ("select * From pettable", Conn );
Reader = cmd. executereader ();
DataGrid. datasource = reader;
DataGrid. databind ();
}
// Catch (exception E)
//{
// Response. Write (E. Message );
// Response. End ();
//}
Finally
{
If (reader! = NULL) reader. Close ();
If (Conn! = NULL) Conn. Close ();
}
}
Access the Excel database
Add the following statement to the namespace section at the top of the Code hiding page:
Using system. Data. oledb;
Using system. Data;
In webform1.aspx. CS, copy the code to the page_load event:
// Create connection string variable. Modify the "Data Source"
// Parameter as appropriate for your environment.
String sconnectionstring = "provider = Microsoft. Jet. oledb.4.0 ;"
"Data Source =" server. mappath ("../exceldata.xls ")";"
"Extended properties = Excel 8.0 ;";
// Create connection object by using the preceding connection string.
Oledbconnection objconn = new oledbconnection (sconnectionstring );
// Open connection with the database.
Objconn. open ();
// The code to follow uses a SQL SELECT command to display the data from the worksheet.
// Create new oledbcommand to return data from worksheet.
Oledbcommand objcmdselect = new oledbcommand ("select * From myrange1", objconn );
// Create new oledbdataadapter that is used to build a dataset
// Based on the preceding SQL SELECT statement.
Oledbdataadapter objadapter1 = new oledbdataadapter ();
// Pass the SELECT command to the adapter.
Objadapter1.selectcommand = objcmdselect;
// Create new dataset to hold information from the worksheet.
Dataset objdataset1 = new dataset ();
// Fill the dataset with the information from the worksheet.
Objadapter1.fill (objdataset1, "xldata ");
// Bind data to DataGrid Control.
Datagrid1.datasource = objdataset1.tables [0]. defaultview;
Datagrid1.databind ();
// Clean up objects.
Objconn. Close ();