Tag: exec character Eve string query statement get text span pass
1. Create a public class db--4 methods. Getcon ()//Connect database, sqlex//perform database operation, redt//return data table, redr//return SqlDataReader Object Dr
///<summary>connecting to a database</summary>returns the SqlConnection object PublicSqlConnection Getcon ()//Connection Database, appsettings property value of the ConfigurationManager object gets the string instantiation SqlConnection object of the connection database in the configuration section, and returns the object{return NewSqlConnection (system.configuration.configurationmanager.appsettings["ConnectionString"]. ToString ());} ///<summary>Execute SQL statement</summary>///<param name= "Cmdstr" >SQL statements</param>///returns int type, 1: Success, 0: Failed Public intSqlex (stringCMDSTR)//performing database operations with SqlCommand objects{SqlConnection con= Getcon ();//connecting to a databaseCon. Open ();//Open ConnectionTry{SqlCommand cmd=NewSqlCommand (Cmdstr, con); cmd. ExecuteNonQuery ();//executes the SQL statement and returns the number of rows affectedreturn 1;}Catch(Exception e) {return 0;}finally{con. Dispose ();}} ///<summary>Execute SQL query statement</summary>///return DataTable data Table PublicDataTable Redt (stringCMDSTR)//queries the data in the database through SQL statements and stores the query results in a DataSet dataset, eventually returning the data table for the query results in that dataset{Try{SqlConnection con=Getcon (); SqlDataAdapter da=NewSqlDataAdapter (Cmdstr, con);D ataset ds=NewDataSet ();d A. Fill (DS);return(ds. tables[0]);//returns a DataSet object that can be used as the data source for a data-bound control, where the data can be edited}Catch(Exception) {Throw;}}///<summary>Execute SQL query statement</summary>///<param name= "str" >Query Statements</param>///return SqlDataReader Object Dr PublicSqlDataReader RedR (stringStr//The result of executing this statement is stored in a SqlDataReader object, and finally the SqlDataReader object is returned to the call{Try{SqlConnection conn=Getcon (); Conn. Open (); SqlCommand com=NewSqlCommand (STR, conn); SqlDataReader Dr=com. ExecuteReader (commandbehavior.closeconnection);returnDr;}Catch(Exception) {Throw;}}
2. Using the DB method, operate the database (here to log in as an example)
protected voidBtlogin_click (Objectsender, EventArgs e) {db db=NewDB (); stringstrUserName = This. Textusername. Text.trim ();//Gets the user name and password enteredstringstrpassword = This. Textpassword. Text.trim (); SqlDataReader Dr=db.redr ("SELECT * from UserInfo where username= '"+strusername+"' and password= '"+strpassword+"'");//Select Dr in the database. Read ();
Dr Object read Data setif(Dr. HasRows) {session["username"] = Dr. GetValue (1); session["role"] = Dr. GetValue (3); Response.Redirect ("~/selectobject.aspx"); } Else{Response.Write ("<script>alert (' Login failed! '); location= ' Login.aspx ' </script>"); } Dr. Close (); }
3. Add the following code in the Web. config file
Login user name for connection to SQL
Login password to connect to SQL
Database name
Server IP
Fill in according to actual situation
<configuration> <appSettings> <add key="ConnectionString" Value="user id= login username to connect to SQL; password= Connection SQL login password; database= database name; server= server IP; Connect timeout=50; Max Pool size=200; Min Pool size=5"/> </appSettings> <system.web> <compilation debug= " true " targetframework="4.0"/> </system.web> </ Configuration>
ASP. NET connection database and operational database--Getting Started