Basic operation of database calls Asp.Net2.0

First, set the Web.Config. Delete <connectionStrings /> add the following code
<ConnectionStrings>
<Add name = “zgdxConn” connectionString = “Provider = Microsoft.Jet.OLEDB.4.0; Data Source = | DataDirectory | / newsData.mdb”providerName = “System.Data.OleDb” />
</ ConnectionStrings>
 
Read of asp.net2.0 in web.config database connection string two methods
Method One:
string myConn = System.Configuration. ConfigurationManager. ConnectionStrings [“zgdxConn”]. ConnectionString;
Method 2:
string myConn = System.Web.Configuration. WebConfigurationManager. ConnectionStrings [“zgdxConn”]. ToString ();
 
, Pour namespace
         If the database is ACCESS:
using System.Data.OleDb;
If the database is SQL Server;
using System.Data.SqlClient;
 
Increase the size of the database data (increase News)
/ / MyConnStr database connection string; establish a connection object myConn
string myConnStr = System.Configuration. ConfigurationManager. ConnectionStrings [“zgdxConn”]. ConnectionString;
OleDbConnection myConn = new OleDbConnection (myConnStr);
 
/ / QueryStr writing SQL commands inserted to the table news message recorded; establish Adapter object to execute SQL commands
string queryStr = “insert into news (lanmuId, title, name, content) values ??(” + DropDownList1.SelectedItem.Value + “, ‘” + TextBox1.Text + “‘, ‘” + TextBox2.Text + “‘, ‘” + TextBox3.Text + “‘);
OleDbCommand myCom = new OleDbCommand (queryStr, myConn);
 
myCom.Connection.Open (); / / open the database connection;
myCom.ExecuteNonQuery (); / / execute SQL commands to the database;
myCom.Connection.Close (); / / close the database connection
 
, Delete the data in the database (delete news)
/ / MyConnStr database connection string; establish a connection object myConn
string myConnStr = System.Configuration. ConfigurationManager. ConnectionStrings [“zgdxConn”]. ConnectionString;
OleDbConnection myConn = new OleDbConnection (myConnStr);
 
/ / QueryStr writing SQL commands inserted to the table news message recorded; establish Adapter object to execute SQL commands
string queryStr = “delete from news where id =” + Request.QueryString [“id”];
OleDbCommand myCom = new OleDbCommand (queryStr, myConn);
 
myCom.Connection.Open (); / / open the database connection;
myCom.ExecuteNonQuery (); / / execute SQL commands to the database;
myCom.Connection.Close (); / / close the database connection
 
Update the data in the database (update fill News)
/ / MyConnStr database connection string; establish a connection object myConn
string myConnStr = System.Configuration. ConfigurationManager. ConnectionStrings [“zgdxConn”]. ConnectionString;
OleDbConnection myConn = new OleDbConnection (myConnStr);
 
/ / QueryStr writing SQL commands inserted to the table news message recorded; establish Adapter object to execute SQL commands
string queryStr = “update news set title = ‘” + TextBox1.Text + “‘, name = ‘” + TextBox2.Text + “‘, content = ‘” + TextBox3.Text + “‘ where id =” + int. Parse (Request.QueryString [“id”]. ToString ());
OleDbCommand myCom = new OleDbCommand (queryStr, myConn);
 
myCom.Connection.Open (); / / open the database connection;
myCom.ExecuteNonQuery (); / / execute SQL commands to the database;
myCom.Connection.Close (); / / close the database connection
 
Display the data in the database (display is fill in the News)
, / / MyConnString get database connection string; establish a connection object MyConn
string myConnStr = System.Configuration. ConfigurationManager. ConnectionStrings [“zgdxConn”]. ConnectionString;
OleDbConnection MyConn = new OleDbConnection (myConnStr);
 
/ / QueryStr writing select all messages SQL commands; establish Adapter object to execute SQL commands
string queryStr = “select * from news where id =” + int. Parse (Request.QueryString [“id”]. ToString ());
OleDbDataAdapter MyAdapter = new OleDbDataAdapter (queryStr, MyConn);
 
/ / Create a DataSet object ds; Adapter Fill method of the result of the execution of SQL commands into the DataSet object
DataSet ds = new DataSet ();
MyAdapter.Fill (ds, “news”);
TextBox1.Text = ds.Tables [“news”]. Rows [0] [“title”]. ToString (); / / remove the memory results in the text box displays
TextBox2.Text = ds.Tables [“news”]. Rows [0] [“name”]. ToString (); / / remove the memory results in the text box displays
TextBox3.Text = ds.Tables [“news”]. Rows [0] [“content”]. ToString (); / / remove the memory results in the text box displays
 
 
 
Data operation summary
First, insert, delete, update according to the following procedure:
1, to establish a connection object: OleDbConnection

                          (Writing SQL statements) 
2, create a command object: OleDbCommand

3, execute the command: ExecuteNonQuery  
Experience: insert, delete, update operation. Code are the same, just not the same SQL statement   
Second, the display data is according to the following steps
1, to establish a connection object: OleDbConnection

                            (Writing SQL statements) 
2, the establishment of the Adapter object: the OleDbDataAdapter

3, the establishment of a collection of tables object: DataSet
Mapping

4, the server-side table mapped to the local table collection: adapter.Fill (local collection of tables, the server table “);