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

时间:2018-06-13 19:25:21

标签: 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=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();
}

2 个答案:

答案 0 :(得分:2)

我在ASP.NET中重新创建了您的解决方案(如果您使用的是WinForms,代码背后也是类似的)。后面的代码按照您提供的方式工作,因此可能是您的查询未返回数据库中的结果,或者click事件未绑定到后面的代码:

<强> SqlExample.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SqlExample.aspx.cs" Inherits="SqlExample" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="lblResultado" runat="server" Text="?"></asp:Label>
        </div>
        <div>
            <asp:TextBox ID="tbZip" runat="server" placeholder="zip"></asp:TextBox>
            <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" />
        </div>
    </form>
</body>
</html>

SqlExample.cs(代码隐藏)

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();
    }
}

示例数据和结果:

WebApp

Data

Table

答案 1 :(得分:0)

我会这样做可能没什么区别===&gt;假设在运行查询时在数据库中返回数据,如预期的那样。

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; 
        }
    }
}