在asp.net中的标签中显示SQL查询结果

Exi*_*mus 3 c# sql asp.net executescalar

我正在尝试在标签中显示SQL查询结果,但它没有显示.这是我的代码:

     string result = "SELECT ACTIVE FROM [dbo].[test] WHERE ID = '" + ID.Text + "' ";
     SqlCommand showresult = new SqlCommand(result, conn);
     conn.Open();
     showresult.ExecuteNonQuery();
     string actresult = ((string)showresult.ExecuteScalar());
     ResultLabel.Text = actresult;
     conn.Close();
Run Code Online (Sandbox Code Playgroud)

需要帮助.谢谢!

hal*_*lie 9

试试这个吧.

   string result = "SELECT ACTIVE FROM [dbo].[test] WHERE ID = '" + ID.Text + "' ";
   SqlCommand showresult = new SqlCommand(result, conn);
   conn.Open();
   ResultLabel.Text = showresult.ExecuteScalar().ToString();
   conn.Close();
Run Code Online (Sandbox Code Playgroud)

  • 请不要将您的查询连接为字符串.使用带有`SqlCommand`的参数可以防止sql注入. (4认同)