public partial class Welcome : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"] == null)
{
Response.Redirect("Login.aspx");
}
} protected void Btreseach_Click(object sender, EventArgs e)
{
string s = System.Configuration.ConfigurationManager.ConnectionStrings["LibraryManageSystem"].ToString();
SqlConnection conn = new SqlConnection(s);
string sqlstr = "select * from Book where bname=@name";
SqlCommand comm = new SqlCommand();
comm.Connection = conn ;
comm.CommandText = sqlstr;
comm.Parameters.AddWithValue("@name", Tbname.Text);
try
{
conn.Open();
SqlDataReader dr = comm.ExecuteReader();
if (dr.Read())
{
GridView1.DataSource = dr;
GridView1.DataBind();
}
else { Response.Write("<script>alert('查询失败')</script>"); }
}
catch (SqlException ex)
{
Response.Write(ex.Message);
} finally
{
conn.Close();
}
} }
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"] == null)
{
Response.Redirect("Login.aspx");
}
} protected void Btreseach_Click(object sender, EventArgs e)
{
string s = System.Configuration.ConfigurationManager.ConnectionStrings["LibraryManageSystem"].ToString();
SqlConnection conn = new SqlConnection(s);
string sqlstr = "select * from Book where bname=@name";
SqlCommand comm = new SqlCommand();
comm.Connection = conn ;
comm.CommandText = sqlstr;
comm.Parameters.AddWithValue("@name", Tbname.Text);
try
{
conn.Open();
SqlDataReader dr = comm.ExecuteReader();
if (dr.Read())
{
GridView1.DataSource = dr;
GridView1.DataBind();
}
else { Response.Write("<script>alert('查询失败')</script>"); }
}
catch (SqlException ex)
{
Response.Write(ex.Message);
} finally
{
conn.Close();
}
} }
SqlDataReader 这只是一个读取器,每次读取一行,不是数据集,怎么可以绑定在控件上。
2.datareader转换成 dataset或者datatable之后绑定转换数据 http://blog.csdn.net/draling/article/details/19203677
参考