1. Create Class Datasource.cs
2. Add SQL Namespace (you can use SQL database statements): Using System.Data.SqlClient
3. The declared object in the class: (string) connstring connection string, (SqlConnection) sqlconn Connection SQL database, (SqlCommand) SQLCMD invokes the SQL statement, (SqlDataAdapter) sqladapter connect the SQL data adapter, (DataSet) DS to establish the dataset, (SqlDataReader) sqlreader read the database.
4. Initialize the instance object in the constructor:
public datasource()
{
ConnString = ConfigurationManager.ConnectionStrings["某数据源"].ConnectionString;
SqlConn = new SqlConnection(ConnString);
SqlCmd = new SqlCommand();
SqlAdpter = new SqlDataAdapter();
ds = new DataSet();
}
5. How to create some datasets and database SQL operations
(1) Create a memory dataset, using the incoming SQL query statement parameters
public DataSet Tds(string QueryString)
{
SqlAdpter.SelectCommand = new SqlCommand(QueryString, SqlConn);
SqlAdpter.Fill(ds, "temp"); //temp是一个内存数据表
return ds;
}
(2) Create a set of memory data without using any parameters.
public DataSet Tds()
{
//使用SQL语句来代替这个参数
SqlAdpter.SelectCommand = new SqlCommand("select * from abc", SqlConn);
//temp是一个内存数据表
SqlAdpter.Fill(ds,"temp");
return ds;
}
(3) Return a read-only lightweight data source, note that it does not support paging on the server side, and that data connections must be open all the time
public SqlDataReader Tsdr()
{
//SqlCommand属性CommandText是一条SQL语句
SqlCmd.CommandText = "select * from abc";
//SqlCommand属性Connection 就是sql的连接语句
SqlCmd.Connection = SqlConn;
SqlConn.Open();
//当数据读取随着数据连接的关闭而关闭
SqlReader = SqlCmd.ExecuteReader(CommandBehavior.CloseConnection);
return SqlReader;
}
(4) SQL UPDATE statement:
public void Update(string 字段id, string 字段1, string 字段2...string 字段N)
{
SqlCmd.CommandText = "update 数据表 set [字段名1]=@字段名1,[字段名2]='" +
字段2 + "',..,[字段名N]='" + 字段N + "' where [字段id]='" + 字段id + "'";
// SqlCmd.Parameters.AddWithValue("@lname", au_lname); 更新的字段可以有单引号
SqlCmd.Connection = SqlConn; //连接
SqlConn.Open(); //打开
SqlCmd.ExecuteNonQuery(); //执行
}
(5) SQL DELETE statement: (use as above)
public void Delete(string 字段id)
{
SqlCmd.CommandText = "delete from [abc] where [字段id]='"+字段id+"'";
SqlCmd.Connection = SqlConn;
SqlConn.Open();
SqlCmd.ExecuteNonQuery();
}
6. Create a new ASPX page: Add a GridView Table
(1) in the configuration file (Web.config), add a phrase
(2) Create a new data source for the GridView: Object-> Business Object Datasource->select\delete\update\insert method of calling related objects-> set parameter SQL query statements, such as SELECT * FROM Abc.
Paging Settings (Allowpage in property pagination?) , Pagesize paging pages. Whether the enablesortingandpagingcallbacks in the property behavior is paged back