在后面的代码中使用sqldatasource返回范围标识 [英] Return scope identity using sqldatasource in code behind

查看:72
本文介绍了在后面的代码中使用sqldatasource返回范围标识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在通过sqldatasource通过以下方式从代码隐藏代码插入数据库.

Currently I'm doing an insert into the db from codebehind with a sqldatasource in the following way.

SqlDataSource13.InsertParameters["name"].DefaultValue = ufn;
            SqlDataSource13.InsertParameters["mime_type"].DefaultValue = FileUpload1.PostedFile.ContentType;
            SqlDataSource13.InsertParameters["size"].DefaultValue = FileUpload1.PostedFile.ContentLength.ToString();
            SqlDataSource13.InsertParameters["extension"].DefaultValue = "";
            SqlDataSource13.InsertParameters["date"].DefaultValue = dt.ToShortDateString();
            SqlDataSource13.InsertParameters["type"].DefaultValue = ddlDocType.SelectedValue;
            SqlDataSource13.InsertParameters["description"].DefaultValue = tbDescription.Text;
            SqlDataSource13.Insert();

这很好.但是,我现在需要获取插入ID.我将select scope_identity添加到插入查询的末尾.并将参数设置为带有方向=输出的insertparameters.

This is working fine. However I need to now get the insert id. I added select scope_identity to the end of the insert query. and the parameter to the insertparameters with direction=ouput.

SELECT @id = SCOPE_IDENTITY()
<asp:Parameter Name="id" Direction="Output" />

但是我需要弄清楚如何使用上面的设置获取输出参数.

however I need to figure out how to get the output parameter with the setup i have above.

推荐答案

为了使事情更轻松,我改用了linq to sql查询.

In order to make things easier on myself I switched to a linq to sql query.

tableDataContext tdc = new tableDataContext();
doc d = new doc();
d.dpi = qs;
d.dn = ufn;
etc...
tdc.docs.InsertOnSubmit(d);
tdc.SubmitChanges();

// get current row insert id
int dID = d.id;

因此,现在我有了最近插入的文档ID以供使用.容易.

So now I have the recently inserted doc id to use. Easy.

感谢谁撰写了这个示例

这篇关于在后面的代码中使用sqldatasource返回范围标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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