C# SQL Server Select查询不执行任何操作

C# SQL Server Select查询不执行任何操作,c#,sql-server,C#,Sql Server,我正在尝试使用文本框中输入的ID运行Select查询。然后在标签上显示结果。但我有这个代码,当我按下按钮时,它什么也不做。请帮忙 private void button1_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("Data Source=DESKTOP-JO0MC7T\\SQLEXPRESS;Initial Catalog=Prueba;Integrated Security=T

我正在尝试使用文本框中输入的ID运行Select查询。然后在标签上显示结果。但我有这个代码,当我按下按钮时,它什么也不做。请帮忙

private void button1_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection("Data Source=DESKTOP-JO0MC7T\\SQLEXPRESS;Initial Catalog=Prueba;Integrated Security=True;User ID=****;Password=***");
    conn.Open();

    SqlCommand command = new SqlCommand("Select nombre from Prueba where Id=@zip", conn);
    command.Parameters.AddWithValue("@zip", txtdataBase.Text);

    using (SqlDataReader reader = command.ExecuteReader())
    {
        if (reader.Read())
        {
            lbResultado.Text = reader.GetValue(0).ToString();
        }
    }

    conn.Close();
}

我会这样做,可能没有太大的区别==>假设在数据库中运行查询时返回的数据与预期的一样

Using(SqlConnection conn = new SqlConnection("Data Source=DESKTOP-JO0MC7T\\SQLEXPRESS;Initial Catalog=Prueba;Integrated Security=True;User ID=****;Password=***"))
{
    conn.Open();

    SqlCommand command = new SqlCommand("Select nombre from Prueba where Id=@zip", conn);
    command.Parameters.AddWithValue("@zip", txtdataBase.Text);

    SqlDataReader reader = command.ExecuteReader())
    {
        if (reader.Read())
        {
            string myresult = reader.GetString(0);// what ever datatype
            lbResultado.Text = myresult; 
        }
    }
}

我会这样做,可能没有太大的区别==>假设在数据库中运行查询时返回的数据与预期的一样

Using(SqlConnection conn = new SqlConnection("Data Source=DESKTOP-JO0MC7T\\SQLEXPRESS;Initial Catalog=Prueba;Integrated Security=True;User ID=****;Password=***"))
{
    conn.Open();

    SqlCommand command = new SqlCommand("Select nombre from Prueba where Id=@zip", conn);
    command.Parameters.AddWithValue("@zip", txtdataBase.Text);

    SqlDataReader reader = command.ExecuteReader())
    {
        if (reader.Read())
        {
            string myresult = reader.GetString(0);// what ever datatype
            lbResultado.Text = myresult; 
        }
    }
}

我在ASP.NET中重新创建了您的解决方案。如果您使用WinForms,则代码隐藏与此类似。代码隐藏按照您提供的方式工作,因此可能是查询没有从数据库返回结果,或者单击事件没有绑定到代码隐藏:

SqlExample.aspx

样本数据和结果:


我在ASP.NET中重新创建了您的解决方案。如果您使用WinForms,则代码隐藏与此类似。代码隐藏按照您提供的方式工作,因此可能是查询没有从数据库返回结果,或者单击事件没有绑定到代码隐藏:

SqlExample.aspx

样本数据和结果:


您是否已使用调试器查看发生了什么?可能您的查询没有返回任何行。您在该文本框中输入了什么?这是winforms还是webforms?如果是webforms,您是否在回发期间更改了lbResultado的文本?作为一项健全性检查,还可以使用SQL Server Management Studio it's free或其他查询工具验证查询是否独立于您的程序返回行。将您的Select nombre from Prueba(其中Id=以及由单引号括起的Id值)复制并粘贴到数据库上打开的新查询窗口中,然后执行它。例如,从Prueba中选择nombre,其中Id='12345'。如果您编写了一个格式不好的查询,那么再多的编码也无法解决您的问题。数据库中的id是什么数据类型?还有txtdataBase.Text中有什么?您是否已使用调试器查看发生了什么?可能您的查询没有返回任何行。您在该文本框中输入了什么?这是winforms还是webforms?如果是webforms,您是否在回发期间更改了lbResultado的文本?作为一项健全性检查,还可以使用SQL Server Management Studio it's free或其他查询工具验证查询是否独立于您的程序返回行。将您的Select nombre from Prueba(其中Id=以及由单引号括起的Id值)复制并粘贴到数据库上打开的新查询窗口中,然后执行它。例如,从Prueba中选择nombre,其中Id='12345'。如果您编写了一个格式不好的查询,那么再多的编码也无法解决您的问题。数据库中的id是什么数据类型?txtdataBase.Text中有什么?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class SqlExample : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(@"MyConnectionData");
        SqlCommand command = new SqlCommand("SELECT nombre FROM Prueba WHERE Id=@zip", conn);

        command.Parameters.AddWithValue("@zip", this.tbZip.Text);

        conn.Open();

        using (SqlDataReader reader = command.ExecuteReader())
        {
            if (reader.Read())
            {
                this.lblResultado.Text = reader.GetValue(0).ToString();
            }
        }

        conn.Close();
    }
}