在做用户注册的时候,往往需要返回用户注册的ID,并把ID保存在COOKIE中,下面的例子就是如何获取注册用户ID的例子。
1、建立数据库test,新建一张表 users:
CREATE TABLE [dbo].[testSP] (
[userID] [int] IDENTITY (1, 1) NOT NULL ,
[userName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[userPassword] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
2、编写一个存储过程,如下:
CREATE PROCEDURE UserRegister
(
@UserName nvarchar(50),
@UserPassword nvarchar(50)
)
AS
insert into testSp(userName,userPassword) values(@userName,@userPassword)
return @@Identity
3、新建一个ASPX页面,放置一个BUTTON,在CLICK事件中,添加如下代码:
try
{
//将yourdb修改为你自己的数据库
SqlConnection conn = new SqlConnection("server=localhost;database=yourdb;integrated security=SSPI");
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = "UserRegister"; //刚才建立的存储过程的名字
comm.CommandType =CommandType.StoredProcedure;
SqlParameter pName = new SqlParameter("@UserName",SqlDbType.NVarChar,50);
pName.Value ="yourName";//输入你的注册名称
comm.Parameters.Add(pName);
SqlParameter pPassword = new SqlParameter("@UserPassword",SqlDbType.NVarChar,50);
pPassword.Value ="yourPassword";//输入你的密码
comm.Parameters.Add(pPassword);
SqlParameter pRet = new SqlParameter("@ret",SqlDbType.Int,4); //定义返回值参数
pRet.Direction = ParameterDirection.ReturnValue;
comm.Parameters.Add(pRet);
conn.Open();
comm.ExecuteNonQuery();
//id就是你要取得id号
int id = Convert.ToInt32(pRet.Value);
conn.Close();
}
catch(SqlException ex)
{
//出错处理
}
4、别网了在aspx页面添加命名空间的应用:
using System.Data;
using System.Data.SqlClient;
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/shwf/archive/2007/03/26/1541913.aspx
1.获取Return返回值
程序代码
//存储过程
//Create PROCEDURE MYSQL
// @a int,
// @b int
//AS
// return @a + @b
//GO
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString());
conn.Open();
SqlCommand MyCommand = new SqlCommand("MYSQL", conn);
MyCommand.CommandType = CommandType.StoredProcedure;
MyCommand.Parameters.Add(new SqlParameter("@a", SqlDbType.Int));
MyCommand.Parameters["@a"].Value = 10;
MyCommand.Parameters.Add(new SqlParameter("@b", SqlDbType.Int));
MyCommand.Parameters["@b"].Value = 20;
MyCommand.Parameters.Add(new SqlParameter("@return", SqlDbType.Int));
MyCommand.Parameters["@return"].Direction = ParameterDirection.ReturnValue;
MyCommand.ExecuteNonQuery();
Response.Write(MyCommand.Parameters["@return"].Value.ToString());
2.获取Output输出参数值
程序代码
//存储过程
//Create PROCEDURE MYSQL
// @a int,
// @b int,
// @c int output
//AS
// Set @c = @a + @b
//GO
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString());
conn.Open();
SqlCommand MyCommand = new SqlCommand("MYSQL", conn);
MyCommand.CommandType = CommandType.StoredProcedure;
MyCommand.Parameters.Add(new SqlParameter("@a", SqlDbType.Int));
MyCommand.Parameters["@a"].Value = 20;
MyCommand.Parameters.Add(new SqlParameter("@b", SqlDbType.Int));
MyCommand.Parameters["@b"].Value = 20;
MyCommand.Parameters.Add(new SqlParameter("@c", SqlDbType.Int));
MyCommand.Parameters["@c"].Direction = ParameterDirection.Output;
MyCommand.ExecuteNonQuery();
Response.Write(MyCommand.Parameters["@c"].Value.ToString());
----------------------存储过程共有三种返回值-------------------
/*(一)、参数返回值@parm2;
(二)、过程返回值return 100;
(三)、数据集返回值select * from table1 where age = @parm1
请问,如何得到三类返回值?
*/
create table table1(
name varchar(30),
age integer,
primary key(name, age)
)
create procedure proc1
@parm1 integer,
@parm2 varchar(30) output
as
select @parm2 = 'zhongguo'
select * from table1 where age = @parm1
return 100
--(一)、参数返回值@parm2;
declare @re varchar(30)
exec proc1 1,@re out
select 返回结果=@re
--(二)、过程返回值return 100;
declare @re int,@parm2 varchar(30)
exec @re=proc1 1,@parm2 out
select 返回结果=@re
--(三)、数据集返回值select * from table1 where age = @parm1
--定义返回结果保存的临时表(因为是返回记录集,所以要用临时表)
--调用返回结果
--declare @re varchar(30)
--insert #re exec proc1 1,@re out
select * into #re from OPENROWSET('SQLOLEDB',
'SERVER=zhou;uid=sa;pwd=zhou;Database=pubs','SET FMTONLY OFF;SET NOCOUNT ON declare @re varchar(30) exec proc1 ''1'',@re out') as a
--显示结果
select * from #re
--处理完成后删除临时表
drop table #re
--返回数据集方式一:
/*补充*/
execute sp_executesql
N'select * from pubs.dbo.employee where job_lvl = @level',
N'@level tinyint',
@level = 35
或者多条件
execute sp_executesql
N'select * from IndividualSaleHouse where user_ID = @user_ID and Area_ID=@Area_ID ',
N'@user_ID int,@Area_ID int',
@user_ID = 1,@Area_ID=1
--返回单个值:
/* 补充 */
declare @sql nvarchar(200)
declare @cnt int
set @sql=N'select @cnt=count(*) from WQ_STINFO_B '
exec sp_executesql @sql,N'@cnt int output',@cnt output