您的位置:首页 > 数据库

sql server 2008 执行insert返回主键ID

2017-01-19 15:08 309 查看
1、string connStr = "server = 127.0.0.1;database=Baosteel_pvs;uid=sa;pwd=123456";

2、string sql_ren = "insert into t_checkrecord(列1,列2,...) values(value1,value2,...) SELECT SCOPE_IDENTITY();";

3、 using (SqlConnection myConnection = new SqlConnection(connStr))

                        {

                            myConnection.Open();

                            SqlCommand myCommand = new SqlCommand(sql_ren, myConnection);

                            primaryKey = Convert.ToInt32(myCommand.ExecuteScalar());//主键

                            myConnection.Close();

                            myCommand.Dispose();

                        }

4、SCOPE_IDENTITY,IDENT_CURRENT,和 @@IDENTITY 是类似的函数,因为它们返回插入到标识列的值。
https://msdn.microsoft.com/zh-sg/library/ms190315.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: