Method 1 :::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::
Note:
<Asp: textbox id = "textbox1" runat = "server" width = "98px"> </ASP: textbox>
<Asp: button id = "button1" runat = "server" onclick = "button#click" text = "Update"/>
<Asp: gridview id = "GV" runat = "server">
Source code:
// Program name: 13-05.aspx.cs
// Program function: Code of the 13-05.aspx function to update data
Public partial class _ 13_05: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
// Create a connection object
Sqlconnection sqlcon = new sqlconnection ();
// Database connection statement
Sqlcon. connectionstring = "Server = localhost; database = test; uid = Wedy; Pwd = '000000 '";
Sqlcon. open ();
// Query statement
String strselect = "select * from article ";
// Create a command object
Sqlcommand sqlcmd = new sqlcommand (strselect, sqlcon );
Sqldatareader DR = sqlcmd. executereader ();
GV. datasource = Dr;
GV. databind ();
Sqlcon. Close ();
}
Protected void button#click (Object sender, eventargs E)
{
Sqlconnection sqlcon1 = new sqlconnection ();
Sqlcon1.connectionstring = "Server = localhost; database = test; uid = Wedy; Pwd = '000000 '";
Sqlcon1.open ();
String strtitle = textbox1.text. tostring ();
String strcontent = textbox2.text. tostring ();
String Strid = textbox3.text. tostring ();
// Construct an SQL statement for updating data
// String strupdate = string. Format ("update Article set content = '{0}' Where title = '{1}'", strcontent, strtitle );
String strupdate = string. format ("update Article set content = '{0}', Title = '{1}' Where id = '{2}'", strcontent, strtitle, Strid );
// Create a command object
Sqlcommand sqlcmd1 = new sqlcommand (strupdate, sqlcon1 );
Sql00001.executenonquery ();
Sqlcon1.close ();
Response. Redirect ("13-05.aspx ");
}
}
------------------------------------------------------------
// Program name: 13-07.aspx.cs
// Program function: 13-07.aspx function code, output a single value
Public partial class _ 13_07: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Sqlconnection sqlcon = new sqlconnection ();
Sqlcon. connectionstring = "Server = localhost; database = test; uid = Wedy; Pwd = '000000 '";
Sqlcon. open ();
// This SQL statement is used to query the total number of data records in a data table.
String strselect = "select count (*) from article ";
Sqlcommand sqlcmd = new sqlcommand (strselect, sqlcon );
// The executescalar () method returns a single value for executing an aggregate function.
Int x = (INT) sqlcmd. executescalar ();
Label1.text = "the total number of data records in the article table is :";
Label1.text + = x. tostring ();
}
}
------------------------------------------------------------
Method 2 :::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::
Note:
Source code:
// Program name: 13-09.aspx.cs
// Program function: 13-09.aspx function code, add a datatable to Dataset
Public partial class _ 13_09: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
// Create a connection object
Sqlconnection sqlcon = new sqlconnection ();
// Create a DataSet object
Dataset DS = new dataset ();
Sqlcon. connectionstring = "Data Source = localhost; initial catalog = test; user id = Wedy; Password = '000000 '";
// If the connection is not enabled, the fill () method of the dataadapter object opens a connection by itself and closes automatically after use.
Sqldataadapter SDA = new sqldataadapter ("select * from Class", sqlcon );
SDA. Fill (DS, "class"); // Add a class table to dataset.
If (Ds. Tables ["class"]. isinitialized = true)
{
// Determine whether the table is added and then output the relevant information
Response. Write ("datasable with the name" + Ds. Tables ["class"]. tablename + "has been added to dataset successfully ");
}
}
}
------------------------------------------------------------
Method 3 :::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::
Note:
// Configure application settings in Web. config:
<Connectionstrings>
<Add name = "connectionstring" connectionstring = "Data Source = localhost; initial catalog = test; user id = Wedy; Password = '000000'" providername = "system. data. sqlclient "/>
</Connectionstrings>
<Asp: gridview id = "gridview1" runat = "server"> </ASP: gridview>
Source code:
// Program name: 13-10.aspx.cs
// Program function: Add a able to the dataset and bind it to the gridview
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
// Introduce the required namespace
Using system. Data. sqlclient;
Public partial class _ 13_10: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
// Create a connection object
Sqlconnection sqlcon = new sqlconnection ();
// Create a DataSet object
Dataset DS = new dataset ();
Sqlcon. connectionstring = configurationmanager. connectionstrings ["connectionstring"]. connectionstring;
// Sqlcon. connectionstring = "Data Source = localhost; initial catalog = test; user id = Wedy; Password = '000000 '";
// If the connection is not enabled, the fill () method of the dataadapter object opens a connection by itself and closes automatically after use.
Sqldataadapter SDA = new sqldataadapter ("select * from Class", sqlcon );
// Add a class table to Dataset
SDA. Fill (DS, "class ");
// Specify the data source of gridview1 as the datatable created by the front edge.
Gridview1.datasource = Ds. Tables ["class"];
// Data Binding
Gridview1.databind ();
}
}
------------------------------------------------------------
// Program name: 13-12.aspx.cs
// Program function: cache Dataset
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;
Public partial class _ 13_12: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Dataview DV;
DV = (dataview) cache ["article"];
// Check whether the cache is empty
If (DV = NULL)
{
Dataset DS = new dataset ();
// Create a connection object
Sqlconnection sqlcon = new sqlconnection ();
Sqlcon. connectionstring = "Data Source = localhost; initial catalog = test; user id = Wedy; Password = '000000 '";
// Create a dataadapter object
Sqldataadapter SDA = new sqldataadapter ("select * from article", sqlcon );
// Add an article table to Dataset
SDA. Fill (DS, "article ");
DV = new dataview (Ds. Tables ["article"]);
// Cache data
Cache ["article"] = DV;
}
// Bind the cache to the gridview and display it
Gridview1.datasource = DV;
Gridview1.databind ();
// Add By Wedy :--------------------------------------------------------
Dataview dv_1;
Dv_1 = (dataview) cache ["class"];
// Check whether the cache is empty
If (dv_1 = NULL)
{
Sqlconnection sqlcon = new sqlconnection ();
Sqlcon. connectionstring = "Data Source = localhost; initial catalog = test; user id = Wedy; Password = '000000 '";
Dataset ds_1 = new dataset ();
Sqldataadapter sda_1 = new sqldataadapter ("select * from Class", sqlcon );
Sda_1.fill (ds_1, "class ");
Dv_1 = new dataview (ds_1.tables ["class"]);
Cache ["class"] = dv_1;
}
Gridview2.datasource = dv_1;
Gridview2.databind ();
}
}
------------------------------------------------------------
// Program name: 13-13.aspx.cs
// Program function: Multiple dataadapter objects can be filled with the same dataset.
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data;
Using system. Data. sqlclient;
Public partial class _ 13_13: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
// Create a connection object
Sqlconnection sqlcon = new sqlconnection ();
// Create a DataSet object
Dataset DS = new dataset ();
// Set the connection string
Sqlcon. connectionstring = "Server = localhost; database = test; uid = Wedy; Pwd = '000000 '";
// Create the instance sda1 of the dataadapter object
Sqldataadapter sda1 = new sqldataadapter ("select * from article", sqlcon );
// Fill in Dataset
Sda1.fill (DS, "article ");
// Create the instance sda2 of the dataadapter object
Sqldataadapter sda2 = new sqldataadapter ("select * from people", sqlcon );
// Fill in Dataset
Sda2.fill (DS, "people ");
// Set the "article" table in dataset as the data source of gridview1
Gridview1.datasource = Ds. Tables ["article"]. defaultview;
// Data Binding
Gridview1.databind ();
// Set the "people" table in dataset as the data source of gridview2
Gridview2.datasource = Ds. Tables ["people"]. defaultview;
// Data Binding
Gridview2.databind ();
}
}
------------------------------------------------------------