为什么从数据库中检索的值没有显示在文本框中?

时间:2022-08-27 16:51:55

Why the values I have retrieved from database are not shown in textbox? I have written the following code but it does not work. I have kept in mind that this code is written on form load event. Please do tell me the mistake I have done?

为什么我从数据库中检索到的值没有显示在文本框中?我写了以下代码,但它不起作用。我记得这段代码写在表单加载事件上。请告诉我我做的错误?

using (SqlConnection myConnection = new SqlConnection(Common.GetDBConnectionString()))
{
    using (SqlCommand cmd = new SqlCommand("P.ID from dbo.Tb_Patient", myConnection))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        myConnection.Open();

        //SqlParameter custId = (cmd.Parameters.AddWithValue("@dbo.Tb_Patient", 10));

        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            if (reader.Read())
            {
                string nameValue = reader["P.ID"].ToString();
                MessageBox.Show("value is:",nameValue);

                txtid.Text = nameValue;


            }
            else {

                MessageBox.Show("Data is not retrived");
            }
      }
  }
}

Update from comment:

评论更新:

CREATE TABLE [dbo].[Tb_Patient]( [P.Id] [int] IDENTITY(1,1) NOT NULL, [P.Name] [nvarchar](50) NOT NULL, [P.Age] [nvarchar](50) NOT NULL, [P.Contact] [nvarchar](50) NOT NULL, [P.Date] [datetime] NULL, [P.Occupation] [nvarchar](50) NOT NULL, [P.Gender] [nvarchar](50) NOT NULL, CONSTRAINT [PK_Tb_Patient] PRIMARY KEY CLUSTERED ( [P.Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]

CREATE TABLE [dbo]。[Tb_Patient]([P.Id] [int] IDENTITY(1,1)NOT NULL,[P.Name] [nvarchar](50)NOT NULL,[P.Age] [nvarchar]( 50)NOT NULL,[P.Contact] [nvarchar](50)NOT NULL,[P.Date] [datetime] NULL,[P.Occupation] [nvarchar](50)NOT NULL,[P.Gender] [nvarchar ](50)NOT NULL,CONSTRAINT [PK_Tb_Patient] PRIMARY KEY CLUSTERED([P.Id] ASC)WITH(PAD_INDEX = OFF,STATISTICS_NORECOMPUTE = OFF,IGNORE_DUP_KEY = OFF,ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON)ON [PRIMARY])ON [主]

3 个解决方案

#1


3  

This line doesn't seems to be a valid SQL query, nor the name of a stored procedure:

此行似乎不是有效的SQL查询,也不是存储过程的名称:

using (SqlCommand cmd = new SqlCommand("P.ID from dbo.Tb_Patient", myConnection))

Probably the correct text is:

可能正确的文字是:

using (SqlCommand cmd = new SqlCommand("select [P.ID] from dbo.Tb_Patient", myConnection))

And of course that it's not a stored procedure but a normal command text:

当然,这不是一个存储过程,而是一个普通的命令文本:

cmd.CommandType = CommandType.Text;

EDIT: After seeing the schema of your table I have corrected the answer. The only error is in the command text passed to the database engine lacking of the SELECT clause at the beginning. However you need to enclose in square brackets the field name P.ID.

编辑:看到你的表的架构后,我已经纠正了答案。唯一的错误是在传递给缺少SELECT子句的数据库引擎的命令文本中。但是,您需要在方括号中包含字段名称P.ID.

I suggest to change the field names. This naming schema is really confusing because it is used when you need to prefix a name present in more than one table joined together.

我建议更改字段名称。这种命名方案确实令人困惑,因为当您需要为连接在一起的多个表中存在的名称添加前缀时使用它。

See: ALIAS of a name

请参阅:ALIAS的名称

#2


0  

Use either a stored procedure or a valid SQL statement

使用存储过程或有效的SQL语句

select P.ID from dbo.Tb_Patient as P

#3


0  

  • If you're not sure whether the database is case insensitive, then always use the same name as in the creation. CREATE TABLE [dbo].[Tb_Patient]( [P.Id] [int] ...
  • 如果您不确定数据库是否不区分大小写,则始终使用与创建中相同的名称。 CREATE TABLE [dbo]。[Tb_Patient]([P.Id] [int] ...
  • Terminate the command string with ;.
  • 用;终止命令字符串。

try it with

尝试一下

using (SqlConnection myConnection = new SqlConnection(connectionString))
{
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "SELECT [P.Id] from dbo.Tb_Patient;";
    cmd.CommandType = CommandType.Text;
    cmd.Connection  = myConnection;
    myConnection.Open();
    SqlDataReader reader = cmd.ExecuteReader();

    if (reader.Read())
        {
            string nameValue = reader["P.Id"].ToString();
            MessageBox.Show("value is:",nameValue);
            txtid.Text = nameValue;
        }
        else {
            MessageBox.Show("Data is not retrieved");
        }

    reader.Close();
}

#1


3  

This line doesn't seems to be a valid SQL query, nor the name of a stored procedure:

此行似乎不是有效的SQL查询,也不是存储过程的名称:

using (SqlCommand cmd = new SqlCommand("P.ID from dbo.Tb_Patient", myConnection))

Probably the correct text is:

可能正确的文字是:

using (SqlCommand cmd = new SqlCommand("select [P.ID] from dbo.Tb_Patient", myConnection))

And of course that it's not a stored procedure but a normal command text:

当然,这不是一个存储过程,而是一个普通的命令文本:

cmd.CommandType = CommandType.Text;

EDIT: After seeing the schema of your table I have corrected the answer. The only error is in the command text passed to the database engine lacking of the SELECT clause at the beginning. However you need to enclose in square brackets the field name P.ID.

编辑:看到你的表的架构后,我已经纠正了答案。唯一的错误是在传递给缺少SELECT子句的数据库引擎的命令文本中。但是,您需要在方括号中包含字段名称P.ID.

I suggest to change the field names. This naming schema is really confusing because it is used when you need to prefix a name present in more than one table joined together.

我建议更改字段名称。这种命名方案确实令人困惑,因为当您需要为连接在一起的多个表中存在的名称添加前缀时使用它。

See: ALIAS of a name

请参阅:ALIAS的名称

#2


0  

Use either a stored procedure or a valid SQL statement

使用存储过程或有效的SQL语句

select P.ID from dbo.Tb_Patient as P

#3


0  

  • If you're not sure whether the database is case insensitive, then always use the same name as in the creation. CREATE TABLE [dbo].[Tb_Patient]( [P.Id] [int] ...
  • 如果您不确定数据库是否不区分大小写,则始终使用与创建中相同的名称。 CREATE TABLE [dbo]。[Tb_Patient]([P.Id] [int] ...
  • Terminate the command string with ;.
  • 用;终止命令字符串。

try it with

尝试一下

using (SqlConnection myConnection = new SqlConnection(connectionString))
{
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "SELECT [P.Id] from dbo.Tb_Patient;";
    cmd.CommandType = CommandType.Text;
    cmd.Connection  = myConnection;
    myConnection.Open();
    SqlDataReader reader = cmd.ExecuteReader();

    if (reader.Read())
        {
            string nameValue = reader["P.Id"].ToString();
            MessageBox.Show("value is:",nameValue);
            txtid.Text = nameValue;
        }
        else {
            MessageBox.Show("Data is not retrieved");
        }

    reader.Close();
}