Data browsing
Simple binding of textbox
Private void page_load (Object sender, system. eventargs E)
{
If (! Ispostback)
{
Session ["recordpos"] = 0;
}
Sqldataadapter1.fill (dsstudent1.tbstudentinfo );
This. databind ();
}
Private void btnprev_click (Object sender, system. eventargs E)
{
Int recordpos = (INT) session ["recordpos"];
If (recordpos> 0)
{
Recordpos --;
}
Session ["recordpos"] = recordpos;
This. databind ();
}
Private void btnnext_click (Object sender, system. eventargs E)
{
Int recordpos = (INT) session ["recordpos"];
Recordpos ++;
If (recordpos> = dsstudent1.tbstudentinfo. Rows. Count)
{
Recordpos = 0;
}
Session ["recordpos"] = recordpos;
This. databind ();
}
Private void tbid_databinding (Object sender, system. eventargs E)
{
Int recordpos = (INT) session ["recordpos"];
Tbid. Text = dsstudent1.tbstudentinfo. Rows [recordpos] ["studentid"]. tostring ();
}
Private void tbname_databinding (Object sender, system. eventargs E)
{
Int recordpos = (INT) session ["recordpos"];
Tbname. Text = dsstudent1.tbstudentinfo. Rows [recordpos] ["studentname"]. tostring ();
}
Private void tbscore_databinding (Object sender, system. eventargs E)
{
Int recordpos = (INT) session ["recordpos"];
Tbscore. Text = dsstudent1.tbstudentinfo. Rows [recordpos] ["score"]. tostring ();
}
Simple connection example
Simply bind a label
<% @ Import namespace = "system. Drawing" %> call a namespace
Private void page_load (Object sender, system. eventargs E)
{
If (! Ispostback)
{
Arraylist AR = new arraylist ();
Ar. Add ("red ");
Ar. Add ("blue ");
Ar. Add ("green ");
Ar. Add ("orange ");
Ddlcolor. datasource = Ar;
}
This. databind ();
}
Private void btnadd_click (Object sender, system. eventargs E)
{
String strsql = "insert into tbuserinfo values ('" + tbname. text + "','" + tbpass. text + "'," + "'" + trblsex. selecteditem. text + "'" + ",";
Strsql + = "'" + cddate. selecteddate. tostring () + "', '" + tbzhiwei. Text + "', '" + tbnotes. Text + "')";
Excutesql (strsql );
}
Private void excutesql (string strsql)
{
Try
{
Sqlconnection con = new sqlconnection ();
Con. connectionstring = system. configuration. configurationsettings. etettings ["DSN"];
Con. open ();
// Add a record...
Sqlcommand COM = new sqlcommand (strsql, con );
Com. executenonquery ();
Con. Close ();
Response. Write ("<script language = 'javascript '> alert (' inserted successfully! ') </SCRIPT> ");
}
Catch (exception ee)
{
Response. Write ("error:" + ee. Message );
}
}
Private void btnlogin_click (Object sender, system. eventargs E)
{
Sqlconnection con = new sqlconnection ();
Con. connectionstring = system. configuration. configurationsettings. etettings ["DSN"];
Con. open ();
String strsql = "select username, userpass from tbuserinfo where username = '" + tbname. Text + "' and userpass = '" + tbpass. Text + "'";
Sqlcommand COM = new sqlcommand (strsql, con );
Sqldatareader DR = com. executereader ();
// Execute the following query:
Bool bexist = false;
While (dr. Read ())
{
Bexist = true;
Session ["username"] = dr. getstring (0 );
Session ["userpass"] = dr. getstring (1 );
}
If (bexist)
Response. Redirect ("023query. aspx ");
Else
Response. Write ("<script language = 'javascript '> alert ('user name or password is incorrect! ') </SCRIPT> ");
Con. Close ();
}
Private void page_load (Object sender, system. eventargs E)
{
Lbusername. Text = session ["username"]. tostring ();
}
Private void btnquery_click (Object sender, system. eventargs E)
{
Sqlconnection con = new sqlconnection ();
Con. connectionstring = system. configuration. configurationsettings. etettings ["DSN"];
Con. open ();
String strsql = "select enterdate from tbuserinfo where username = '" + tbname. Text + "'";
Sqlcommand COM = new sqlcommand (strsql, con );
Com. Parameters. Add ("@ name", sqldbtype. varchar, 50, "username ");
Com. Parameters ["@ name"]. value = tbname. text;
Com. commandtext = "select enterdate from tbuserinfo where username = @ name ";
Object OBJ = com. executescalar ();
If (OBJ! = NULL)
Lbhiredate. Text = obj. tostring ();
Else
Lbhiredate. Text = "this person is not in the database! ";
Con. Close ();
}
Private void page_load (Object sender, system. eventargs E)
{
Sqldataadapter1.fill (dsuser1.tbuserinfo );
If (! Ispostback)
{
Session ["recordpos"] = 0;
This. databind ();
}
}
Private void excutesql (string strsql)
{
Try
{
Sqlconnection con = new sqlconnection ();
Con. connectionstring = system. configuration. configurationsettings. etettings ["DSN"];
Con. open ();
// Add a record...
Sqlcommand COM = new sqlcommand (strsql, con );
Com. executenonquery ();
Con. Close ();
// Response. Write ("<script language = 'javascript '> alert ('Operation successful! ') </SCRIPT> ");
}
Catch (exception ee)
{
Response. Write ("error:" + ee. Message );
}
}
// Next page
Private void btnnext_click (Object sender, system. eventargs E)
{
Int recordpos = (INT) session ["recordpos"];
Recordpos ++;
If (recordpos> = dsuser1.tbuserinfo. Rows. Count)
{
Recordpos = 0;
}
Session ["recordpos"] = recordpos;
This. databind ();
}
// Previous Page
Private void btnprev_click (Object sender, system. eventargs E)
{
Int recordpos = (INT) session ["recordpos"];
If (recordpos> 0)
{
Recordpos --;
}
Session ["recordpos"] = recordpos;
This. databind ();
}
// Data Binding
Private void tbname_databinding (Object sender, system. eventargs E)
{
Int recordpos = (INT) session ["recordpos"];
Tbname. Text = dsuser1.tbuserinfo. Rows [recordpos] ["username"]. tostring ();
}
Private void tbpass_databinding (Object sender, system. eventargs E)
{
Int recordpos = (INT) session ["recordpos"];
Tbpass. Text = dsuser1.tbuserinfo. Rows [recordpos] ["userpass"]. tostring ();
}
Private void tbsex_databinding (Object sender, system. eventargs E)
{
Int recordpos = (INT) session ["recordpos"];
Tbsex. Text = dsuser1.tbuserinfo. Rows [recordpos] ["sex"]. tostring ();
}
Private void tbzhiwei_databinding (Object sender, system. eventargs E)
{
Int recordpos = (INT) session ["recordpos"];
Tbzhiwei. Text = dsuser1.tbuserinfo. Rows [recordpos] ["Zhiwei"]. tostring ();
}
Private void tbnotes_databinding (Object sender, system. eventargs E)
{
Int recordpos = (INT) session ["recordpos"];
Tbnotes. Text = dsuser1.tbuserinfo. Rows [recordpos] ["Notes"]. tostring ();
}
Private void tbdate_databinding (Object sender, system. eventargs E)
{
Int recordpos = (INT) session ["recordpos"];
Tbdate. Text = dsuser1.tbuserinfo. Rows [recordpos] ["enterdate"]. tostring ();
}
// Delete
Private void btndelete_click (Object sender, system. eventargs E)
{
String strsql = "delete tbuserinfo where username = '" + tbname. Text + "'";
Excutesql (strsql );
}
// Update
Private void btnupdate_click (Object sender, system. eventargs E)
{
String strsql = "Update tbuserinfo" +
"Set" +
"Userpass = '" + tbpass. Text + "'," +
"Sex = '" + tbsex. Text + "'," +
"Enterdate = '" + tbdate. Text + "'," +
"Zhiwei = '" + tbzhiwei. Text + "'," +
"Notes = '" + tbnotes. Text + "'" +
"Where username = '" + tbname. Text + "'";
Excutesql (strsql );
}
Stored Procedures
// Execute the Stored Procedure
Private void btndatareader_click (Object sender, system. eventargs E)
{
// Create a table to store the query results
Datatable table = new datatable ("test ");
// Add columns to a table
Table. Columns. Add ("employeeid ");
Table. Columns. Add ("orderdate ");
Table. Columns. Add ("requireddate ");
// Execute the query
Sqldatareader rdremployees;
Sqlconnection. open (); // open the database connection
Rdremployees = sqlcommand. executereader (); // execute the statement
// Read the following data
Datarow Dr;
While (rdremployees. Read ())
{
Dr = table. newrow (); // create a row
// Assign values to each column of the current row
Dr [0] = rdremployees. getint32 (0 );
Dr [1] = rdremployees. getdatetime (1 );
Dr [2] = rdremployees. getdatetime (2 );
Table. Rows. Add (DR); // Add the current row.
}
// Set the data source as follows
Sqlconnection. Close (); // close the connection
Datagridreader. datasource = table; // set the data source to display data
Datagridreader. databind ();
}
// Execute the stored procedure with Parameters
Private void button#click (Object sender, system. eventargs E)
{
// Create a table to store the query results
Datatable table = new datatable ("test ");
// Add columns to a table
Table. Columns. Add ("orderid ");
Table. Columns. Add ("orderdate ");
Table. Columns. Add ("requredate ");
Table. Columns. Add ("shippeddate ");
// Execute the query
Sqldatareader rdremployees;
Sqlconnection. open (); // open the database connection
Sqlcommand1.parameters ["@ customerid"]. value = "victe ";
Sqlcommand1.parameters ["@ return_value"]. value = 1;
Rdremployees = sqlcommand1.executereader (); // execute the statement
// Read the following data
Datarow Dr;
While (rdremployees. Read ())
{
Dr = table. newrow (); // create a row
// Assign values to each column of the current row
Dr [0] = rdremployees. getint32 (0 );
Dr [1] = rdremployees. getdatetime (1 );
Dr [2] = rdremployees. getdatetime (2 );
Dr [3] = rdremployees. getdatetime (3 );
Table. Rows. Add (DR); // Add the current row.
}
// Set the data source as follows
Sqlconnection. Close (); // close the connection
Datagridreader. datasource = table; // set the data source to display data
Datagridreader. databind ();
}