简体   繁体   English

查询在Visual Studio 2017中不起作用,但直接在SQL Server中起作用

[英]Query doesn't work in Visual Studio 2017, but works directly in SQL Server

I want to retrieve the last inserted id in the user_table, but when I try it in the code in Visual Studio, it says that it get nothing from the query. 我想检索user_table中最后插入的id,但是当我在Visual Studio的代码中尝试使用它时,它说它从查询中什么也没有。

I tried this code in my webform in Visual Studio 2017. but it can't get the data that I want. 我在Visual Studio 2017的Web窗体中尝试了此代码。但是无法获取所需的数据。 When I tried this code in the SQL Server 2014, it shows the data that I want. 当我在SQL Server 2014中尝试此代码时,它显示了所需的数据。

//This is the code in visual studio
string tklastid = "SELECT TOP 1 id_user FROM user_table ORDER BY id_user DESC".ToString();
    SqlCommand lastid = new SqlCommand(tklastid, con);
    SqlDataReader dr2;
    dr2 = lastid.ExecuteReader();
    try
    {
        while (dr2.Read())
        {
            Label1.Text = dr2.ToString();
            userlast = dr2.ToString();
        }
    }
    catch (Exception exe)
    {
        Label1.Text = exe.Message;
    }
    dr2.Close();
    con.Close();

//this is the code when I make the table
create table user_table
(
    int identity(1,1) primary key,
    user_email varchar(50) not null,
    user_name varchar(50)
)

Your SQL for creating the table is missing the name of the primary key, "id-user", should look like this: 用于创建表的SQL缺少主键名称“ id-user”,应如下所示:

   create table user_table
   (
       id_user int identity(1,1) primary key,
       user_email varchar(50) not null,
       user_name varchar(50)
   )

Also, as @hassan.ef pointed out, dr2 needs an index even though you are only selecting one column, so you could use either dr2[0].ToString() or dr2["id_user"].ToString() . 另外,正如@ hassan.ef所指出的,即使您仅选择一列, dr2需要一个索引,因此您可以使用dr2[0].ToString()dr2["id_user"].ToString()

use this one: 使用这个:

  Label1.Text = dr2["id_user"].ToString();
  userlast = dr2["id_user"].ToString();

istead of: 代替:

    Label1.Text= dr2.ToString();
    userlast = dr2.ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Visual Studio 2017无法使用Unity Documentation Access - Unity Documentation Access doesn't work from Visual Studio 2017 SignalR在Visual Studio 2017下不起作用 - SignalR doesn't work under Visual Studio 2017 SQL查询不适用于SQL Server - SQL query doesn't work on SQL Server 我的WebApi在Visual Studio上运行良好,但在IIS上不起作用 - My WebApi works fine on Visual Studio but doesn't work on IIS SQL查询可在SQL Server 2008中使用,但不适用于C# - SQL query works in SQL Server 2008 but doesn't work in C# 为什么 AutogenerateBindingRedirects 不适用于 Visual Studio 2017 中的 Web.config - Why doesn't AutogenerateBindingRedirects work for a Web.config in Visual Studio 2017 为什么在VS2017中,Visual Studio 2010不允许在linq查询中使用“is null”? - Why doesn't Visual Studio 2010 allow the use of “is null” in a linq query when VS2017 does? Sql 命令在 Visual Studio 中无法正常工作 - oracle - Sql command doesn't work correctly in visual studio - oracle SQL 查询适用于 EA,但不适用于 API 上的 C# - SQL query works in EA but doesn't work with C# on the API Visual Studio 2008无法连接到SQL Server 2008 - Visual Studio 2008 doesn't connect to SQL Server 2008
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM