C#Database @@ identity [英] C# Database @@identity

查看:61
本文介绍了C#Database @@ identity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不使用C#中的存储过程的情况下获取@@ identity值?


我有一个名为''sports''的表,其中包含@@ identity的值

主键。


我想要做的是在插入''sports''表后,我想要

获取我刚刚插入的@@ identity值(主键)。


如果有帮助,我必须关注代码,提前感谢:


------------------------------------------- --------------


string sql =" insert into sports(name ......." ;;


conn.Open();


//将记录插入数据库

SqlCommand cmd = new SqlCommand(sql,conn) );

SqlDataReader result = cmd.ExecuteReader();

result.Close();


// ???得到什么是刚刚插入的身份???

SqlCommand ident = new SqlCommand(" select @@ identity from sports" conn);

SqlDataReader ident_result = ident.ExecuteReader();


int sport_id = -1;

if(ident_result.Read()){ident_result .GetDecimal(0); }


ident_result.Close();

conn.Dispose();

How do I get the @@identity value without using stored procedures in C#?

I have the a table named ''sports'' that has an @@identity value for the
primary key.

What I want to do is after I insert into into the ''sports'' table, I want to
get the @@identity value (primary key) for what I just inserted.

I have to following code if this helps, thanks in advance:

---------------------------------------------------------

string sql = "insert into sports (name.......";

conn.Open();

// insert record into the database
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader result = cmd.ExecuteReader();
result.Close();

// ??? GET THE IDENTITY OF WHAT WAS JUST INSERTED ???
SqlCommand ident = new SqlCommand("select @@identity from sports", conn);
SqlDataReader ident_result = ident.ExecuteReader();

int sport_id = -1;
if (ident_result.Read()) { ident_result.GetDecimal(0); }

ident_result.Close();
conn.Dispose();

推荐答案

而不是使用:

string sql =" insert into sports(name .......); SELECT @@ IDENTITY;"

Now使用你的SqlDataReader result =获取第一列的值,这是新的身份


-

Swanand Mokashi

Microsoft认证专家
http://www.swanandmokashi.com/

股票报价的主页,当日报价和星座运势网络服务

Micheal < FR **** @ sbcglobal.net>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...
rather use :
string sql = "insert into sports (name.......);SELECT @@IDENTITY;"
Now use your SqlDataReader result = to getvalue of the first column which is
the new identity

--
Swanand Mokashi
Microsoft Certified Professional
http://www.swanandmokashi.com/
Home of the Stock Quotes, Quote of the day and Horoscope web services
"Micheal" <fr****@sbcglobal.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
怎么做我在不使用C#中的存储过程的情况下获得了@@ identity值?

我有一个名为''sports'的表,它具有
主键的@@标识值。

我想要做的是在插入''sports''表后,我想要
来获取我刚插入的@@ identity值(主键)。 br />
如果有帮助,我必须关注代码,提前谢谢:

---------------------- -----------------------------------

string sql =" insert into sports(name ......." ;;

conn.Open();

//将记录插入数据库
SqlCommand cmd = new SqlCommand(sql,conn);
SqlDataReader result = cmd.ExecuteReader();
result.Close();

// ???获取什么是正确的身份INSERTED ???
SqlCommand ident = new SqlCommand(" select @@ identity from sports",conn);
SqlDataReader ident_result = ident.ExecuteReader();

int sport_id = -1;
if(ident_result.Read()){ident_result.GetDecimal( 0); }

ident_result.Close();
conn.Dispose();
How do I get the @@identity value without using stored procedures in C#?

I have the a table named ''sports'' that has an @@identity value for the
primary key.

What I want to do is after I insert into into the ''sports'' table, I want to get the @@identity value (primary key) for what I just inserted.

I have to following code if this helps, thanks in advance:

---------------------------------------------------------

string sql = "insert into sports (name.......";

conn.Open();

// insert record into the database
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader result = cmd.ExecuteReader();
result.Close();

// ??? GET THE IDENTITY OF WHAT WAS JUST INSERTED ???
SqlCommand ident = new SqlCommand("select @@identity from sports", conn);
SqlDataReader ident_result = ident.ExecuteReader();

int sport_id = -1;
if (ident_result.Read()) { ident_result.GetDecimal(0); }

ident_result.Close();
conn.Dispose();



Micheal,


如果您只是在插入

语句中选择@@ identity,它将起作用。


然后使用ExecuteScalar来解决它。


像这样:


strSQL =" INSERT INTO sports(name)VALUES(@name); SELECT @@ Identity"

SQLCommand.CommandText = strSQL

IdReturned = SQLCommand.ExecuteScalar


我希望这会有所帮助。 />
-

S. Justin Gengo,MCP

Web开发人员


免费代码库位于:
www.aboutfortunate.com


出于混乱而来的顺序。

Nietzche

" Micheal" < FR **** @ sbcglobal.net>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...
Micheal,

It will work if you just do the select @@identity within your insert
statement.

Then use ExecuteScalar to get it out.

Like this:

strSQL = "INSERT INTO sports (name) VALUES (@name);SELECT @@Identity"
SQLCommand.CommandText = strSQL
IdReturned = SQLCommand.ExecuteScalar

I hope this helps.
--
S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
"Micheal" <fr****@sbcglobal.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
怎么做我在不使用C#中的存储过程的情况下获得了@@ identity值?

我有一个名为''sports'的表,它具有
主键的@@标识值。

我想要做的是在插入''sports''表后,我想要
来获取我刚插入的@@ identity值(主键)。 br />
如果有帮助,我必须关注代码,提前谢谢:

---------------------- -----------------------------------

string sql =" insert into sports(name ......." ;;

conn.Open();

//将记录插入数据库
SqlCommand cmd = new SqlCommand(sql,conn);
SqlDataReader result = cmd.ExecuteReader();
result.Close();

// ???获取什么是正确的身份INSERTED ???
SqlCommand ident = new SqlCommand(" select @@ identity from sports",conn);
SqlDataReader ident_result = ident.ExecuteReader();

int sport_id = -1;
if(ident_result.Read()){ident_result.GetDecimal( 0); }

ident_result.Close();
conn.Dispose();
How do I get the @@identity value without using stored procedures in C#?

I have the a table named ''sports'' that has an @@identity value for the
primary key.

What I want to do is after I insert into into the ''sports'' table, I want to get the @@identity value (primary key) for what I just inserted.

I have to following code if this helps, thanks in advance:

---------------------------------------------------------

string sql = "insert into sports (name.......";

conn.Open();

// insert record into the database
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader result = cmd.ExecuteReader();
result.Close();

// ??? GET THE IDENTITY OF WHAT WAS JUST INSERTED ???
SqlCommand ident = new SqlCommand("select @@identity from sports", conn);
SqlDataReader ident_result = ident.ExecuteReader();

int sport_id = -1;
if (ident_result.Read()) { ident_result.GetDecimal(0); }

ident_result.Close();
conn.Dispose();



yes executioncalar是一个更好的选择


-

Swanand Mokashi

微软认证专家
http://www.swanandmokashi.com/

股票报价之家,报价当天和星座网络服务

" S。 Justin Gengo < SJ ***** @ aboutfortunate.com>在消息中写道

news:uM ************* @ TK2MSFTNGP09.phx.gbl ...
yes executescalar is a even better option

--
Swanand Mokashi
Microsoft Certified Professional
http://www.swanandmokashi.com/
Home of the Stock Quotes, Quote of the day and Horoscope web services
"S. Justin Gengo" <sj*****@aboutfortunate.com> wrote in message
news:uM*************@TK2MSFTNGP09.phx.gbl...
Micheal,

如果您只是在插入
语句中选择@@ identity,它将起作用。

然后使用ExecuteScalar将其删除。

喜欢这个:

strSQL =" INSERT INTO sports(name)VALUES(@name); SELECT @@ Identity"
SQLCommand.CommandText = strSQL
IdReturned = SQLCommand.ExecuteScalar

我希望这会有所帮助。
-
S. Justin Gengo,MCP
Web开发人员

免费代码库:
www.aboutfortunate.com

出于混乱而来命令。
Nietzche
Micheal < FR **** @ sbcglobal.net>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...
Micheal,

It will work if you just do the select @@identity within your insert
statement.

Then use ExecuteScalar to get it out.

Like this:

strSQL = "INSERT INTO sports (name) VALUES (@name);SELECT @@Identity"
SQLCommand.CommandText = strSQL
IdReturned = SQLCommand.ExecuteScalar

I hope this helps.
--
S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
"Micheal" <fr****@sbcglobal.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
我如何获得@没有在C#中使用存储过程的@identity值?

我有一个名为''sports'的表,它具有
主键的@@标识值。

我想要做的是在插入''sports'表后,我想
How do I get the @@identity value without using stored procedures in C#?

I have the a table named ''sports'' that has an @@identity value for the
primary key.

What I want to do is after I insert into into the ''sports'' table, I want


获取@@ identity值(主键) )对于我刚刚插入的内容。

如果有帮助,我必须关注代码,提前感谢:

-------------- -------------------------------------------
string sql =" insert into sports(name ......." ;;

conn.Open();

//将记录插入数据库
SqlCommand cmd = new SqlCommand(sql,conn);
SqlDataReader result = cmd.ExecuteReader();
result.Close();

// ?? ?获取刚刚插入的内容???
SqlCommand ident = ne w SqlCommand(" select @@ identity from sports",
conn); SqlDataReader ident_result = ident.ExecuteReader();

= sport(id_result。 }

ident_result.Close();
conn.Dispose();
get the @@identity value (primary key) for what I just inserted.

I have to following code if this helps, thanks in advance:

---------------------------------------------------------

string sql = "insert into sports (name.......";

conn.Open();

// insert record into the database
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader result = cmd.ExecuteReader();
result.Close();

// ??? GET THE IDENTITY OF WHAT WAS JUST INSERTED ???
SqlCommand ident = new SqlCommand("select @@identity from sports", conn); SqlDataReader ident_result = ident.ExecuteReader();

int sport_id = -1;
if (ident_result.Read()) { ident_result.GetDecimal(0); }

ident_result.Close();
conn.Dispose();




这篇关于C#Database @@ identity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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