在sql server里面建立一个存储过程,如下所示
CREATE PROCEDURE [dbo].[UserProcedureWithParameter]
@username nvarchar(50)
AS
select * from [user] where username like @username
GO
在ado.net里面这样的调用
private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "workstation id=OVERMIND;packet size=4096;user id=sa;password=sa;data source=OVERMIND;persist security info=False;initial catalog=wztj";
con.Open();
SqlCommand cm = new SqlCommand("UserProcedureWithParameter",con);
cm.CommandType= CommandType.StoredProcedure;
SqlParameter parameter1 = new SqlParameter("@username",SqlDbType.NVarChar,50);
parameter1.Value="%"+"aa"+"%";
cm.Parameters.Add(parameter1);
DataSet ds = new DataSet();
SqlDataAdapter ad = new SqlDataAdapter(cm);
ad.Fill(ds,"user");
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
// Put user code to initialize the page here
}
哈哈哈哈,原来就是这样简单!
CREATE PROCEDURE [dbo].[UserProcedureWithParameter]
@username nvarchar(50)
AS
select * from [user] where username like @username
GO
在ado.net里面这样的调用
private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "workstation id=OVERMIND;packet size=4096;user id=sa;password=sa;data source=OVERMIND;persist security info=False;initial catalog=wztj";
con.Open();
SqlCommand cm = new SqlCommand("UserProcedureWithParameter",con);
cm.CommandType= CommandType.StoredProcedure;
SqlParameter parameter1 = new SqlParameter("@username",SqlDbType.NVarChar,50);
parameter1.Value="%"+"aa"+"%";
cm.Parameters.Add(parameter1);
DataSet ds = new DataSet();
SqlDataAdapter ad = new SqlDataAdapter(cm);
ad.Fill(ds,"user");
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
// Put user code to initialize the page here
}
哈哈哈哈,原来就是这样简单!