本文介绍了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 theprimary key. What I want to do is after I insert into into the ''sports'' table, I want toget 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 databaseSqlCommand 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-26 23:05