在.net中使用会话 [英] Using session in .net

查看:70
本文介绍了在.net中使用会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个社交网站上
我的问题是我要设计一个注册表单,该表单将连接到SQL中的两个表.
其中一个表是UserLoginDetails,其中包含字段Userid,用户名,电子邮件和密码.
另一个表是UserPersonalDetails,其中包含字段UpId,Userid,FullName,d.O.b,性别等...

当自动生成第一个表中的userid字段时,如何从UserLoginDetails获取Userid并为该特定会话的同一用户在UserPersonalDetails中插入相同的ID.

请帮帮我..

I am working on a social networking site,
My problem is that i am to design a registration form which is to be connected to two tables in SQL.
One table is UserLoginDetails, having fields Userid, username, email and password.
Other table is UserPersonalDetails having fields UpId, Userid, FullName, d.O.b, sex etc...

how can i get Userid from UserLoginDetails and insert the same id in UserPersonalDetails for same user for that particular session, when the userid field in first table is autogenerated.

Pls help me..

推荐答案

通过@@ identity从UserLoginDetails获取用户ID或从存储过程中选择scope_identity()[不在紧凑版中],这将返回最后插入的身份字段值(即Userid),然后在下一个插入中使用它.

来自SP

声明@userid bigint
插入UserLoginDetails(用户名,电子邮件,密码)
values(``用户名'',``电子邮件'',``密码'')
设置@userid = @@ identity
插入UserPersonalDetails(用户ID,全名,[d.O.b],性别)
值(@Userid,``FullName'',``d.o.b'',``sex'')



可以从像
这样的sqlcommand对象中调用
SqlCommand _cmd =新的SqlCommand(插入查询或SP_name",_connobj);
----------------
--------------
SqlTransaction _tran = _connobj.BeginTransaction();
_cmd.Transaction = tran;
int rowsAffected = _cmd.ExecuteNonQuery();
_cmd =新的SqlCommand("SELECT @@ IDENTITY AS userid",_ conn,_tran);

对象userid = _cmd.ExecuteScalar();

-下一个插入
tran.Commit();
Get Userid from UserLoginDetails by @@identity or select scope_identity()[not in compact edition] from stored procedure which return last inserted identity feild value(i.e Userid) then use it in next insert.

from SP

declare @userid bigint
insert into UserLoginDetails (username, email, password)
values(''username'',''email'',''password'')
set @userid =@@identity
insert into UserPersonalDetails (Userid, FullName, [d.O.b], sex)
values( @Userid, ''FullName'', ''d.o.b'', ''sex'')


or
can be call from sqlcommand object like

SqlCommand _cmd = new SqlCommand("insert query or SP_name", _connobj);
----------------
--------------
SqlTransaction _tran=_connobj.BeginTransaction();
_cmd.Transaction = tran;
int rowsAffected = _cmd.ExecuteNonQuery();
_cmd = new SqlCommand("SELECT @@IDENTITY AS userid", _conn, _tran);

object userid = _cmd.ExecuteScalar();

-- Next insert
tran.Commit();


这篇关于在.net中使用会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆