First, what is a stored procedure
The stored procedure is in large databases, a complete specific function SQL语句集
; a compilation permanent, and finally returns the results users want, so in the more complex business processes, usually using stored procedures query package.
Use SqlCommand
when there object to call a stored procedure and execute SQL statements command a big difference, mainly includes the transfer of the stored procedure parameters, and output parameters to obtain the stored procedure returns a fundamentally different values and so on.
Second, the example: use the Command object to call a stored procedure to achieve search function
This example is based on the content of a blog, the blog part of my point Go to page! !
First look at the results:
Then look at the gradual implementation process:
Step 1: create a stored procedure, first click on School
the database, and then open 可编程性
, right-click 存储过程
, last click 新建
, like this:
Then written Sql
statement:
create proc GetStudentList
@SearchValue varchar(256)=''
as
begin
if @SearchValue<>''
begin
select * from [dbo].[Student] where Name like '%'+@SearchValue+'%'
end
else
begin
select * from [dbo].[Student]
end
end
Step 2: create a new site, create Default.aspx
pages, add text boxes and input box controls in the page:
<div>
<div>
<asp:TextBox ID="TextBox1" runat="server" class="TxtSearch"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="搜索" CssClass="BtnSearch" OnClick="Button1_Click" />
</div>
<div id="tabList" runat="server">
</div>
</div>
Step 3: Set the table and search for the build CSS
style:
<style type="text/css">
.TxtSearch,.BtnSearch{
border-width:1px;
border-style:solid;
}
table,table th,table td{
border:1px;
border-style:solid;
border-color:#22bbad;
}
table th{
border-color:white;
}
table{
border-collapse:collapse;
}
table th{
width:150px;
height:30px;
text-align:center;
background-color:#22bbad;
color:white;
}
table td{
height:30px;
text-align:center;
}
</style>
Step 4: to button
bind the event handler method, and display all data directly on page load:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string Res = GetSqlData();
this.tabList.InnerHtml = Res;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string Res = GetSqlData();
this.tabList.InnerHtml = Res;
}
Step 5: write GetSqlData
method:
private string GetSqlData()
{
string SearchValue = this.TextBox1.Text;
StringBuilder Res = new StringBuilder();
Res.Append("<table><tr><th>序号</th><th>姓名</th><th>性别</th><th>年龄</th><th>班级</th></tr>");
using(SqlConnection conn = new SqlConnection("Server=DESKTOP-9FCSCD4;DataBase=School;Trusted_Connection=SSPI"))
{
conn.Open();
SqlCommand comm = new SqlCommand();
//指定发送到数据库的执行命令为调用存储过程
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = "GetStudentList"; //存储过程的名称
comm.Connection = conn;
//定义存储过程中的参数
SqlParameter Param = new SqlParameter("SearchValue", SqlDbType.VarChar, 256);
Param.Value = SearchValue; //设置参数值
comm.Parameters.Add(Param);
using(SqlDataReader DataReader = comm.ExecuteReader())
{
while (DataReader.Read())
{
int ID = (int)DataReader["ID"];
string Name = (string)DataReader["Name"];
string Sex = (string)DataReader["Sex"];
int Age = (int)DataReader["Age"];
string Class = (string)DataReader["Class"];
Res.Append("<tr><td>" + ID + "</td>");
Res.Append("<td>" + Name + "</td>");
Res.Append("<td>" + Sex + "</td>");
Res.Append("<td>" + Age + "</td>");
Res.Append("<td>" + Class + "</td></tr>");
}
}
comm.Dispose();
}
Res.Append("</table>");
return Res.ToString();
}
carry out!