尝试使用scope_identity传递最近的ID,它的错误是:指定的强制转换无效

发布于 2024-12-24 19:26:59 字数 2532 浏览 2 评论 0原文

我试图传递我刚刚用 Scope_Identity 插入的最近 ID, 它给了我一些奇怪的东西。 它说:

:指定的演员无效

并引用该行:

int IdOfRecentHistoryGame = (Int32)commandquery.ExecuteScalar();

这是代码的其余部分:

string username = Session["Session"].ToString();

            con = new SqlConnection("Data Source=MICROSOF-58B8A5\\SQL_SERVER_R2;Initial Catalog=Daniel;Integrated Security=True");
            con.Open();

            string knowWhichOne = "SELECT ID FROM Users WHERE Username='" + UserOrGuest.Text + "'";

            SqlCommand comm = new SqlCommand(knowWhichOne, con);
            int userID = (Int32)comm.ExecuteScalar();

            knowWhichOne = "SELECT ClassID FROM Users WHERE Username='" + UserOrGuest.Text + "'";

            comm = new SqlCommand(knowWhichOne, con);
            int classID = (Int32)comm.ExecuteScalar();

            knowWhichOne = "SELECT SchoolID FROM Users WHERE Username='"+UserOrGuest.Text + "'";

            comm = new SqlCommand(knowWhichOne, con);
            int schoolID = (Int32)comm.ExecuteScalar();

            if (RadioWords.Checked == true)
            {
                 game = 1;
            }
            else
            {
                 game = 2;
            }
            string arr = "";
            for (int i = 0; i < ListBox.Items.Count; i++)
            {
                arr += ListBox.Items[i] +",";
            }

            string sqlqueryString = "INSERT INTO HistoryOfGames (GameID, UserID, LengthOfArray, NumberOfErrors, ClassID, SchoolID,Arrayarray) VALUES (@GameID, @UserID, @LengthOfArray, @NumberOfErrors, @ClassID, @SchoolID, @Arrayarray);" + "SELECT SCOPE_IDENTITY()";

            SqlCommand commandquery = new SqlCommand(sqlqueryString, con);

            commandquery.Parameters.AddWithValue("GameID", game);
            commandquery.Parameters.AddWithValue("UserID", userID);
            commandquery.Parameters.AddWithValue("LengthOfArray", HowMany.Text);
            commandquery.Parameters.AddWithValue("NumberOfErrors", 0);
            commandquery.Parameters.AddWithValue("ClassID", classID);
            commandquery.Parameters.AddWithValue("SchoolID", schoolID);
            commandquery.Parameters.AddWithValue("Arrayarray", arr);

            commandquery.ExecuteNonQuery();





            int IdOfRecentHistoryGame = (Int32)commandquery.ExecuteScalar();
            con.Close();

            Response.Redirect("NowPlay.aspx?ID="+ IdOfRecentHistoryGame);

有人有线索吗?

谢谢!

Im trying to pass the recent ID that I just insert with Scope_Identity,
and its giving me some weird stuff.
it says :

: Specified cast is not valid

and refering to the line :

int IdOfRecentHistoryGame = (Int32)commandquery.ExecuteScalar();

this is the rest of the code :

string username = Session["Session"].ToString();

            con = new SqlConnection("Data Source=MICROSOF-58B8A5\\SQL_SERVER_R2;Initial Catalog=Daniel;Integrated Security=True");
            con.Open();

            string knowWhichOne = "SELECT ID FROM Users WHERE Username='" + UserOrGuest.Text + "'";

            SqlCommand comm = new SqlCommand(knowWhichOne, con);
            int userID = (Int32)comm.ExecuteScalar();

            knowWhichOne = "SELECT ClassID FROM Users WHERE Username='" + UserOrGuest.Text + "'";

            comm = new SqlCommand(knowWhichOne, con);
            int classID = (Int32)comm.ExecuteScalar();

            knowWhichOne = "SELECT SchoolID FROM Users WHERE Username='"+UserOrGuest.Text + "'";

            comm = new SqlCommand(knowWhichOne, con);
            int schoolID = (Int32)comm.ExecuteScalar();

            if (RadioWords.Checked == true)
            {
                 game = 1;
            }
            else
            {
                 game = 2;
            }
            string arr = "";
            for (int i = 0; i < ListBox.Items.Count; i++)
            {
                arr += ListBox.Items[i] +",";
            }

            string sqlqueryString = "INSERT INTO HistoryOfGames (GameID, UserID, LengthOfArray, NumberOfErrors, ClassID, SchoolID,Arrayarray) VALUES (@GameID, @UserID, @LengthOfArray, @NumberOfErrors, @ClassID, @SchoolID, @Arrayarray);" + "SELECT SCOPE_IDENTITY()";

            SqlCommand commandquery = new SqlCommand(sqlqueryString, con);

            commandquery.Parameters.AddWithValue("GameID", game);
            commandquery.Parameters.AddWithValue("UserID", userID);
            commandquery.Parameters.AddWithValue("LengthOfArray", HowMany.Text);
            commandquery.Parameters.AddWithValue("NumberOfErrors", 0);
            commandquery.Parameters.AddWithValue("ClassID", classID);
            commandquery.Parameters.AddWithValue("SchoolID", schoolID);
            commandquery.Parameters.AddWithValue("Arrayarray", arr);

            commandquery.ExecuteNonQuery();





            int IdOfRecentHistoryGame = (Int32)commandquery.ExecuteScalar();
            con.Close();

            Response.Redirect("NowPlay.aspx?ID="+ IdOfRecentHistoryGame);

somebody has a clue ?

Thanks!

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

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

发布评论

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

评论(2

美胚控场 2024-12-31 19:26:59

SCOPE_IDENTITY() 返回 numeric(38,0),因为 IDENTITY 可以在除 int 之外的类型列上定义(参见SQL Server - IDENTITY 列支持哪些数据类型?)。

ExecuteScalar 将返回 decimal 作为 object,您无法将其直接转换为 int。相反,首先转换为 decimal,然后转换为 int

int IdOfRecentHistoryGame = (Int32)(Decimal)commandquery.ExecuteScalar();

SCOPE_IDENTITY() returns numeric(38,0) since an IDENTITY can be defined on columns of types besides int (see SQL Server - What are the data types supported in IDENTITY columns?).

ExecuteScalar is returning a decimal as an object, which you cannot cast directly to int. Instead, cast to decimal first, then to int:

int IdOfRecentHistoryGame = (Int32)(Decimal)commandquery.ExecuteScalar();
聚集的泪 2024-12-31 19:26:59

SCOPE_IDENTITY() 显然实际上并没有在 SQL Server 中返回 Int。我不知道它不能像你尝试的那样进行投射,但显然它不能。

下面的链接说,如果源列是整数,则根本不必强制转换它,但这无论如何都值得一试 -

如何将 Scope_Identity() 强制转换为 Int? 展示了如何在 SQL 查询中强制转换它以获取 Integer:(

礼貌的Jose Basilio)

DECLARE @NewIdent Int
SET @NewIdent = SCOPE_IDENTITY()

尝试将变量分配给其输出。

SCOPE_IDENTITY() apparently doesn't actually return an Int in SQL server. I didn't know it couldn't be cast like you tried to, but apparently it can't.

The below link says that if the source column is an integer you shouldn't have to cast it at all, but this is worth a shot at any rate -

How do I cast Scope_Identity() to Int? shows how to cast this within the SQL query in order to get an Integer out:

(Courtesy of Jose Basilio)

DECLARE @NewIdent Int
SET @NewIdent = SCOPE_IDENTITY()

Try assigning your variable to the output of that.

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