从C#.net中的数据库中检索数据

时间:2014-02-07 01:37:19

标签: sql sqldatasource sqldatareader

美好的一天!,我想从用户将在文本框中输入的数据库中检索数据,当用户点击搜索按钮时,它将显示在表单中,但是当我执行下面的代码时,什么都不会发生。谁能帮我。

    namespace booksreviews
    {
    public partial class search : System.Web.UI.Page
    {
    private SqlConnection connection;
    private SqlCommand command;
    private int count = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
       SqlConnection Con = new SqlConnection("connectionstring");


    }
    public  SqlConnection con { get; set; }

    protected void btnSearch_Click(object sender, EventArgs e)
    {

       try{
       con.Open()
        int result = command.ExecuteNonQuery();
        if (txtTitle.Text == string.Empty)
    {
    SqlCommand cmd = new SqlCommand("
    Select                                                                
    title,authors,publisher,price,nopages,pubdate from book ", con);
    }
    else
    {
    SqlCommand cmd = new SqlCommand("
    select title,authors,publisher,price,nopages,pubdate from book where
     id= '" +  txtTitle.Text + "'", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    dlBooks.DataSource = ds;
    dlBooks.DataBind();
}catch (ArgumentException argumentEx)
    {
        Response.Write(argumentEx.Message);
    }
    catch (SqlException ex)
    {
        Response.Write(ex.Message);
    }
    catch (IndexOutOfRangeException bound)
    {
        Response.Write(bound.Message);
    }
    finally
    {
        connection.Close();
    }
         }
    }
    }
    }

1 个答案:

答案 0 :(得分:0)

如果单击按钮但没有任何反应,很可能您没有使用按钮控件挂接事件处理程序,否则至少黄页将会出现。