为什么我从数据库中检索到的值没有显示在文本框中?我已经编写了以下代码,但它不起作用。我记住,这段代码是在表单加载事件上编写的。请告诉我我做错了什么?
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");
}
}
}
}
从评论更新:
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]