步骤一:数据库中创建存储过程(在你创建的数据库下选择可编程性;在可编程性下有存储过程;右击并创建)
-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE hellocc
@Name varchar(50)
AS
BEGIN
select * FROM [User] WHERE UserName=@Name;
END
GO
步骤二:调用存储过程(在代码中连接字符串省略)
//获取连接字符串
string connStrs =
ConfigurationManager.ConnectionStrings["connStr"].ConnectionString.ToString();
//创建连接
SqlConnection conn = new SqlConnection(connStrs);
//调用存储过程
SqlCommand cmd = new SqlCommand("hellocc", conn);
//设置存储过程类型
cmd.CommandType = CommandType.StoredProcedure;
//添加相应的传输参数;与存储过程的定义对应
cmd.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar, 50));
cmd.Parameters["@Name"].Value = "赋值";
//开启连接
cmd.Connection.Open();
//通过dataReader取值
SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
//遍历循环
while (dr.Read())
{
div1.InnerHtml +=
dr["Uid"] + "<br/>" + dr["Pwd"] + "<br/>" +
dr["Keyword"] + "<br/>" + dr["logtime"];
}
dr.Close();
cmd.Connection.Close();