好吧,我无法找出错误。没有数据来自变量SqlDataReader。变量SqlDataReader检索的数据存储在Label2中。
代码:
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Server=(local);Database=records;User Id=sasfddsf;Password=12345");
try
{
con.Open();
SqlCommand cmd = new SqlCommand("select id,name,referencename from records where name = '" + Label1.Text.ToString() + "'", con);
var SqlDataReader = cmd.ExecuteReader();
while (SqlDataReader.Read())
{
Label2.Text += Convert.ToString(SqlDataReader["name"]) + Convert.ToString(SqlDataReader["referenceName"]);
}
SqlDataReader.Close();
}
catch (Exception e1)
{
Label2.Text = "Error: " + e1.Message;
}
finally
{
con.Close();
}
}
}
答案 0 :(得分:1)
试试这个
SqlCommand cmd = new SqlCommand("select id,name,referencename from records where name = @TextBoxName", con);
com.Parameters.AddWithValue("@TextBoxName",Label1.Text.ToString());
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Label2.Text += Convert.ToString(rdr["name"]) + Convert.ToString(rdr["referenceName"]);
}
rdr.Close();