No.2(重写OnItemDataBound 事件)
using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Session["rowcnt"] = 0; //将数据读到DataTable DataTable dt = new DataTable("ProductList"); string strCon = "Data Source=ahxh-02;Initial Catalog=PubBase;Integrated Security=True"; SqlConnection connection = new SqlConnection(strCon); connection.Open(); // string strSql ="select * from product"; // SqlCommand command = new SqlCommand("GetProInfo", connection); SqlCommand command1 = new SqlCommand(); command1.Connection = connection; command1.CommandText = "GetProInfo"; command1.CommandType = CommandType.StoredProcedure; SqlDataReader reader = command1.ExecuteReader(); dt.Load(reader); DataView dv = new DataView(); dv.Table = dt; dv.Sort = "productName desc"; this.GridView1.DataSource = dv; this.GridView1.DataBind(); this.GridView1.FooterRow.Cells[0].Text = ((int)Session["rowcnt"]).ToString(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { //e.Row.RowType == DataControlRowType.Header if (e.Row.RowType == DataControlRowType.DataRow) { Session["rowcnt"] = (int)Session["rowcnt"] + 1; } } } sqlyuju create database PubBase use PubBase create table Product ( productId int primary key identity(1,1), productName varchar(20) ) insert into product values('黄山牌香烟') insert into product values('贵州牌香烟') insert into product values('云南牌香烟') insert into product values('金龙王牌香烟') insert into product values('小熊猫香烟') --存储过程1 create proc GetProInfo as select * from Product exec GetProInfo --存储过程2 create proc GetProInfo2 ( @id int ) as select * from Product where productId=@id exec GetProInfo2 2
No.6(使用DataView对数据排序)
default using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string strCon = "Data Source=ahxh-02;Initial Catalog=PubBase;Integrated Security=True"; SqlConnection connection = new SqlConnection(strCon); connection.Open(); SqlCommand command = new SqlCommand("GetProInfo", connection); SqlDataAdapter adapter = new SqlDataAdapter(command); DataSet ds = new DataSet(); adapter.Fill(ds); DataTable dt = ds.Tables[0]; //DataView view = new DataView(); //view.Table = dt; //view.Sort = "productId desc"; GridView1.DataSource = dt; this.GridView1.DataBind(); } } 2.default1 using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string strCon = "Data Source=ahxh-02;Initial Catalog=PubBase;Integrated Security=True"; SqlConnection connection = new SqlConnection(strCon); connection.Open(); SqlCommand command = new SqlCommand("getinfo",connection); SqlDataAdapter adapter = new SqlDataAdapter(command); DataTable dt = new DataTable("table1"); adapter.Fill(dt); DataView dv = new DataView(); dv.Table = dt; dv.Sort = "productId desc"; dv.RowFilter = "productName ='黄山牌香烟'"; this.GridView1.DataSource = dv; this.GridView1.DataBind(); } } Sql create database PubBase use PubBase create table Product ( productId int primary key identity(1,1), productName varchar(20) ) insert into product values('黄山牌香烟') insert into product values('贵州牌香烟') insert into product values('云南牌香烟') insert into product values('金龙王牌香烟') insert into product values('小熊猫香烟') --存储过程1 create proc GetProInfo as select * from Product exec GetProInfo --存储过程2 create proc GetProInfo2 ( @id int ) as select * from Product where productId=@id exec GetProInfo2 2
No.26存储过程
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsFormsApplication12 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // @name varchar(50), //@pwd varchar(20), //@limit int, //@description varchar(50) } private void button1_Click(object sender, EventArgs e) { string strCon = "Server=.;database=users;uid=sa;pwd=123456"; SqlConnection connection = new SqlConnection(strCon); connection.Open(); SqlCommand command = new SqlCommand("InsetData", connection); command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@name", SqlDbType.VarChar, 20); command.Parameters.Add("@pwd", SqlDbType.VarChar, 20); command.Parameters.Add("@limit", SqlDbType.Int, 4); command.Parameters.Add("@description", SqlDbType.VarChar, 20); command.Parameters[0].Value = this.textBox1.Text; command.Parameters[1].Value = this.textBox2.Text; command.Parameters[2].Value = int.Parse(this.textBox3.Text); command.Parameters[3].Value = this.textBox4.Text; command.ExecuteNonQuery(); } } }
sql代码
use Users CREATE TABLE Users.dbo.PASSWORD ( -- 1 操作员口令表(OPERATOR.dbo.PASSWORD)(&2) name varchar(50) not null primary key, -- 用户名 pwd varchar(20) not null, --密码 limit int not null, -- 权限 description varchar(50) not null-- 描述 ) GO --定义存储过程 create proc InsetData @name varchar(50), @pwd varchar(20), @limit int, @description varchar(50) as insert into Password values(@name,@pwd,@limit,@description) go --调用测试存储过程 exec InsetData '程佳佳1','123',1,'aaa' --查询 select * from Password