C#中textbox1中输入的内容在表 shang 中查询,点击button,在datagridview中显示查询的结果

该怎么做,最好能有代码,谢谢!
2024-12-08 11:21:39
推荐回答(1个)
回答1:

下面是B/S 模式的gridview的例子,用它推一下C/S 模式的datagridview 原理是一样的

 public System.Data.DataSet GetDataSet(string sqlStr, string TableName)

    {

        SqlConnection myConn = GetConnection();

        myConn.Open();

        SqlDataAdapter adapt = new SqlDataAdapter(sqlStr, myConn);

        DataSet ds = new DataSet();

        adapt.Fill(ds, TableName);

        myConn.Close();

        return ds;

    }

 protected void btnChaxun_Click(object sender, EventArgs e)   //查询

    {

        this.Label3.Visible = true;

        this.Label5.Visible = true;

        this.lblShow.Visible = true;

        SqlConnection conn = cc.GetConnection();

        SqlCommand mycomm = new SqlCommand("select 权限 from tb_Usertab", conn);

        conn.Open();

        string power = Convert.ToString(mycomm.ExecuteScalar());

        if (power == "1")

        {

            this.ddlZHT.SelectedIndex = 0;

        }

        else

        {

            this.ddlZHT.SelectedIndex = 1;

        }

        try

        {

            int i = 0;

            for (; i <= this.GridView1.Rows.Count - 1; i++)

            {

                this.GridView1.DataSourceID = null;

                string strSql = "select * from tb_Usertab where 用户编号='" + this.txtUsersBH.Text.ToString().Trim() + "'";

                strSql += " or(( 用户名称 = '" + this.txtName.Text.ToString().Trim() + "')";

                strSql += " or(性别='" + this.ddlSex.SelectedValue.ToString() + "')";

                strSql += "or(部门='" + this.ddlManage.SelectedValue.ToString() + "'))";

                //strSql += "or(权限='" + this.ddlZHT.SelectedValue.ToString() + "'))";

                this.GridView1.DataSource = cc.GetDataSet(strSql, "tb_Usertab");

                this.GridView1.DataKeyNames = new string[] { "用户编号" };

                this.GridView1.DataBind();

               

            }

            this.lblShow.Text = Convert.ToString(i);

        }

        catch (SqlException ex)

        {

            Response.Write("alert('" + ex.Message.ToString() + "')");

        }

        

    }