检索scope_identity时,特定转换无效

发布于 2024-09-05 04:51:08 字数 296 浏览 3 评论 0原文

我收到异常:“特定演员无效”,这是代码

con.Open();
string insertQuery = @"Insert into Tender (Name, Name1, Name2) values ('Val1','Val2','Val3');Select Scope_Identity();";

SqlCommand cmd = new SqlCommand(insertQuery, con);
cmd.ExecuteNonQuery();
tenderId = (int)cmd.ExecuteScalar();

I am getting exception: "Specific cast is not valid", here is the code

con.Open();
string insertQuery = @"Insert into Tender (Name, Name1, Name2) values ('Val1','Val2','Val3');Select Scope_Identity();";

SqlCommand cmd = new SqlCommand(insertQuery, con);
cmd.ExecuteNonQuery();
tenderId = (int)cmd.ExecuteScalar();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忆梦 2024-09-12 04:51:08

为了完整起见,您的代码示例存在三个问题。

1) 您通过调用 ExecuteNonQueryExecuteScalar 执行查询两次。因此,每次运行此函数时,您都会在表中插入两条记录。尽管您的 SQL 是两个不同的语句,但它们将一起运行,因此您只需调用 ExecuteScalar

2) Scope_Identity() 返回小数。您可以对查询结果使用 Convert.ToInt32,也可以将返回值转换为十进制,然后转换为 int。

3) 确保将连接和命令对象包装在 using 语句中,以便正确处理它们。

using (SqlConnection connection = new SqlConnection(connectionString))
{
    using (SqlCommand command = new SqlCommand(sql, connection))
    {
        connection.Open();
        int tenderId = (int)(decimal)command.ExecuteScalar();
    }
}

In the interests of completeness, there are three issues with your code sample.

1) You are executing your query twice by calling ExecuteNonQuery and ExecuteScalar. As a result, you will be inserting two records into your table each time this function runs. Your SQL, while being two distinct statements, will run together and therefore you only need the call to ExecuteScalar.

2) Scope_Identity() returns a decimal. You can either use Convert.ToInt32 on the result of your query, or you can cast the return value to decimal and then to int.

3) Be sure to wrap your connection and command objects in using statements so they are properly disposed.

using (SqlConnection connection = new SqlConnection(connectionString))
{
    using (SqlCommand command = new SqlCommand(sql, connection))
    {
        connection.Open();
        int tenderId = (int)(decimal)command.ExecuteScalar();
    }
}
倾`听者〃 2024-09-12 04:51:08

试试这个:-

con.Open();
string insertQuery = @"Insert into Tender (Name, Name1, Name2) values ('Val1','Val2','Val3');Select Scope_Identity();";

SqlCommand cmd = new SqlCommand(insertQuery, con);
tenderId = Convert.ToInt32(cmd.ExecuteScalar());

编辑

应该是这样,因为它正确指出了scope_identity() 返回一个数字(38,0) :-

tenderId = Convert.ToInt32(cmd.ExecuteScalar());

注意: 您仍然需要删除这:-

cmd.ExecuteNonQuery();

Try this:-

con.Open();
string insertQuery = @"Insert into Tender (Name, Name1, Name2) values ('Val1','Val2','Val3');Select Scope_Identity();";

SqlCommand cmd = new SqlCommand(insertQuery, con);
tenderId = Convert.ToInt32(cmd.ExecuteScalar());

EDIT

It should be this as it is correctly pointed out that scope_identity() returns a numeric(38,0) :-

tenderId = Convert.ToInt32(cmd.ExecuteScalar());

Note: You still need to remove the:-

cmd.ExecuteNonQuery();
全部不再 2024-09-12 04:51:08

首先测试以下内容:

object id = cmd.ExcuteScalar()

设置断点并查看 id 的类型。它可能是一个 Decimal 并且不能直接转换为 int

Test the following first:

object id = cmd.ExcuteScalar()

Set a break point and have a look at the type of id. It is probably a Decimal and cannot directly be casted to int.

吃颗糖壮壮胆 2024-09-12 04:51:08

它需要 Convert.ToInt32(cmd.ExecuteScalar());

it needs Convert.ToInt32(cmd.ExecuteScalar());

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文