0

我想在 datagridview 中显示一个指定的列值。示例:从表中user_account,我想通过accountdatagridview 中的列中的文本框数据显示和搜索

这是到目前为止的代码:

private void button3_Click(object sender, EventArgs e)
{
        string connectionString = @"Data Source=" + textBox4.Text + ";" + "Initial Catalog=" + textBox1.Text + ";" + "User ID=" + textBox2.Text + ";" + "Password=" + textBox3.Text;
        string sql = "SELECT * FROM user_account";
        SqlConnection connection = new SqlConnection(connectionString) ;
        SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
        DataSet ds = new DataSet();
        connection.Open();
        dataadapter.Fill(ds, "account");
        connection.Close();
        dataGridView1.DataSource = ds;
        dataGridView1.DataMember = "account";
    }
4

2 回答 2

1
private void button3_Click(object sender, EventArgs e)
    {
        string connectionString = @"Data Source=" + textBox4.Text + ";" + "Initial Catalog=" + textBox1.Text + ";" + "User ID=" + textBox2.Text + ";" + "Password=" + textBox3.Text;
        string sql = "SELECT account FROM user_account";
        SqlConnection connection = new SqlConnection(connectionString) ;
        SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
        DataSet ds = new DataSet();
        connection.Open();
        dataadapter.Fill(ds, "account");
        connection.Close();
        dataGridView1.DataSource = ds;

    }
于 2013-09-05T04:46:05.807 回答
0

解决方案:

        private void button3_Click(object sender, EventArgs e)
    {
        string connectionString = @"Data Source=" + textBox4.Text + ";" + "Initial Catalog=" + "lin2db" + ";" + "User ID=" + textBox2.Text + ";" + "Password=" + textBox3.Text;
        string sql = "SELECT account ,pay_stat FROM user_account where account='" + textBox7.Text + "' ";          
        SqlConnection connection = new SqlConnection(connectionString) ;
        SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
        DataSet ds = new DataSet();
        connection.Open();
        dataadapter.Fill(ds, "pay_stat");
        connection.Close();
        dataGridView1.DataSource = ds;
        dataGridView1.DataMember = "pay_stat";


    }

谢谢你的帮助。

于 2013-09-05T09:17:47.603 回答