1, the Connection object mainly provides the connection function with the database
Configuring the Web. config file
<appSettings>
<add key= "ConnectionString" value= "Server=10.136.*.*;d atabase=mtl;uid=sa;pwd=sa;" />
</appSettings>
Create a connection string to get the configuration section
Public SqlConnection getsqlconnection ()
{
String str = configurationmanager.appsettings["ConnectionString"]. ToString ();
SqlConnection sqlconn = new SqlConnection (str);
return sqlconn;
}
string " server= (local); Userid=sa;pwd=123456;database=sqlname " ; // Create a SqlConnection object and set the connection database string New SqlConnection (str); Sql. Open (); // Open a database connection // database-related operations ..... Sql. Close (); // Close the database connection (will consume memory resources without shutting down)
Sample Code
2, the command object mainly to the data source to increase, delete, check, change and other operations; Here are a few examples:
Query operations
if(Sqlcommandtext.text! ="") { Try{SqlConnection myconn=getsqlconnection (); MyConn.Open (); stringstrstring ="SELECT * from Table1 where [email protected]"; SqlCommand Sqlcmd=NewSqlCommand (strstring, myconn); SQLCMD.PARAMETERS.ADD ("@State", SqlDbType.NVarChar,2). Value = This. SqlCommandText.Text.Trim (); SqlDataAdapter Mydataadpater=NewSqlDataAdapter (Sqlcmd); DataSet DS=NewDataSet (); Mydataadpater.fill (DS); if(ds. tables[0]. Rows.Count >0) {Response.Write ("This information exists! "); } Else{Response.Write ("This information does not exist! "); } } Catch(Exception ex) {Response.Write (ex). Message); } }Sample Code
Add Action
if( This. SqlCommandText.Text.Trim ()! ="") { Try{SqlConnection sqlconn=getsqlconnection (); Sqlconn.open (); stringSTR ="INSERT INTO Table1 (state) VALUES ('"+ This. SqlCommandText.Text.Trim () +"')"; SqlCommand Sqlcomm=NewSqlCommand (STR, sqlconn); Sqlcomm.executenonquery (); Sqlconn.close (); Response.Write ("Add success! "); } Catch(Exception ex) {Response.Write (ex). Message); } }Sample Code
call a stored procedure
//stored procedure Code, inserting data into db_table;Use db_table go Create proc procclass (@Name varchar ( -)) asINSERT INTO Table1 (Name) VALUES (@Name) Go//stored procedures can make it easier to manage the database and display database information, which is a precompiled collection of SQL statements and optional control-flow statements.//stored in a database, which can be invoked through a SqlCommand object in a program that executes faster than the SQL statement blockSqlConnection sqlconn =getsqlconnection (); Sqlconn.open (); SqlCommand cmd=NewSqlCommand ("Procclass", sqlconn); Cmd.commandtype=CommandType.StoredProcedure; Cmd. Parameters.Add ("@Name", SqlDbType.VarChar, -). Value = This. SqlCommandText.Text.Trim (); Cmd. ExecuteNonQuery (); Cmd. Dispose (); Sqlconn.close ();Sample Code
to implement transaction processing
//transactions: A transaction is a unit of a set of related tasks, which either succeeds or fails all, and the result of the final execution of the transaction is two States, namely, Commit and terminate;SqlConnection sqlconn =getsqlconnection (); Sqlconn.open (); stringstr ="INSERT INTO Equipmentinfo (state) VALUES ('"+ This. SqlCommandText.Text.Trim () +"')"; SqlTransaction Sqltran=sqlconn.begintransaction (); SqlCommand cmd=NewSqlCommand (str, sqlconn); Cmd. Transaction=Sqltran; Try{cmd. ExecuteNonQuery (); Sqltran.commit (); Sqlconn.close (); Response.Write ("Add success! "); } Catch(Exception ex) {Response.Write ("Add failed! "); Throw; }Sample Code
3. DataAdapter data Adapters and dataset datasets
The SqlDataAdapter object is a bridge between the DataSet object and the data source;
Primarily retrieving data from a data source, populating a table in a DataSet object, or writing modifications made by a user to a DataSet object to a data source;
New DataSet (); string " SELECT * from Table1 " ; = getsqlconnection (); Conn. Open (); New SqlDataAdapter (STR, conn); Sqlda. Fill (DS);
Sample Code
4. DataReader Object Reader
The reader reads the data in a connected, fast, unbuffered, and forward-only way.
Reads one record at a time and then traverses the entire result set;
SqlConnection sqlconn =getsqlconnection (); stringstr ="SELECT * from Table1"; SqlCommand cmd=NewSqlCommand (str, sqlconn); Cmd.commandtype=CommandType.Text; Try{sqlconn.open (); //executes the SQL statement and returns the DataReader objectSqlDataReader Reader =cmd. ExecuteReader (); This. Sqlcommandtext.text ="serial number, news content"; while(Reader.read ()) { This. Sqlcommandtext.text + = reader["NewsID"] +","+ reader["newscontent"]; } reader.close (); } Catch(Exception ex) {Response.Write (ex). ToString ()); } finally{sqlconn.close (); }Sample Code
----------------individual learning process of some summary, write the wrong place also please advise---------------------
Asp. NET summary of the five main objects of the ADO operation database