C# 使用 select 语句后将值存储在变量中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1555320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:48:14  来源:igfitidea点击:

Store value in a variable after using select statement

c#asp.netsql-servervisual-studio

提问by user175084

How to store the value of the PolicyIDreturned from database in an integer variable in C#?

如何PolicyID将从数据库返回的值存储在整数变量中C#

I am using SQL server 2005.

我正在使用 SQL Server 2005。

        System.Data.SqlClient.SqlConnection dataConnection = new SqlConnection();
        dataConnection.ConnectionString =
            @"Data Source=JAGMIT-PC\SQLEXPRESS;Initial Catalog=SumooHAgentDB;Integrated Security=True";

        System.Data.SqlClient.SqlCommand dataCommand = new SqlCommand();
        dataCommand.Connection = dataConnection;
        dataCommand.CommandText = ("select PolicyID from Policies where PolicyID=(select max(PolicyID) from Policies)");



        dataConnection.Open();
        dataCommand.ExecuteNonQuery();
        dataConnection.Close();

Please suggest.

请建议。

Thanks.

谢谢。

采纳答案by SLaks

Use the SqlCommand.ExecuteScalarmethod, like this:

使用SqlCommand.ExecuteScalar方法,如下所示:

command.CommandText = @"select max(PolicyID) from Policies";
int maxPolicyId = (int)command.ExecuteScalar();

Also, if you're doing this to insert a new Policy row with a unique ID, you must not do it like this, because it's entirely possible that a different Policies row will be inserted between the select and the insert.

此外,如果您这样做是为了插入一个具有唯一 ID 的新 Policy 行,则不能这样做,因为完全有可能在 select 和 insert 之间插入不同的 Policies 行。

Instead, use an IDENTITYcolumn or a UNIQUEIDENTIFIERcolumn.

而是使用一IDENTITY列或一UNIQUEIDENTIFIER列。

EDIT: To use this in your code, do this:

编辑:要在您的代码中使用它,请执行以下操作:

int maxId;
using (SqlConnection dataConnection = new SqlConnection(@"Data Source=JAGMIT-PC\SQLEXPRESS;Initial Catalog=SumooHAgentDB;Integrated Security=True"))
using (SqlCommand dataCommand = 
        new SqlCommand("select max(PolicyID) from Policies", dataConnection)) {

    dataConnection.Open();
    maxId = Convert.ToInt32(dataCommand.ExecuteScalar());
}

回答by Gary McGill

DECLARE @id INTEGER
SELECT @id=PolicyID FROM ...

回答by Hans Ke?ing

Did you just insert a new record into that policy table and now you want the ID given? Then instead of a max(), use

您是否刚刚在该策略表中插入了一条新记录,现在您想要给定 ID?然后,而不是 max(),使用

SELECT SCOPY_IDENTITY()

to get the value that was assigned to yourrecord, not to a record that happened to be inserted moments later.

获取分配给您的记录的值,而不是稍后插入的记录。

回答by oscaro

You can also use SELECT IDENT_CURRENT('Policies')to get the value that was created for your record. To learn more about the differences between SCOPE_IDENTITY()AND IDENT_CURRENT('tablename'), check out this link.

您还可以使用SELECT IDENT_CURRENT('Policies')获取为您的记录创建的值。要了解有关SCOPE_IDENTITY()AND之间差异的更多信息IDENT_CURRENT('tablename'),请查看此链接