在DataGridView中显示来自sql的表 [英] Display tables from sql in DataGridView

查看:50
本文介绍了在DataGridView中显示来自sql的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我创建了一个名为charityah的数据库,其中包含5个表

他们的名字在一个组合框中列出

当我选择其中一个时我想在datagridview中显示它的内容



我试过的是

首先我将datagridview链接到这个数据库

并尝试了我发现的代码



 SqlConnection connection =  new  SqlConnection(); 

private void comboBox1_SelectedIndexChanged( object sender,EventArgs e)
{
string s = comboBox1.Text;

connection.ConnectionString = @ 数据源=(LocalDB)\ v11.0; AttachDbFilename = C:\ Users \Downloads\charityah.mdf; Integrated Security = True;

使用(连接)
{
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter( select *来自 + s,连接);
DataSet ds = new DataSet();
adapter.Fill(ds,s);
dataGridView1.DataSource = ds;
dataGridView1.DataMember = s;
dataGridView1.Refresh();
}







此方法不会给我任何错误,它会发现表格,但没有在datagridview中看到

解决方案

 SqlConnection con =  new  SqlConnection(  Data Source =(LocalDB)\ v11.0; AttachDbFilename = C:\ Users \Downloads \\ \\ charityah.mdf; Integrated Security = True); 
DataSet ds = new DataSet();
SqlDataAdapter DataAdapter1 = new SqlDataAdapter();
SqlCommand command = new SqlCommand();


command.CommandText =( select * from + s,连接);
command.CommandType = CommandType.Text;
command.Connection = con;

DataAdapter1.SelectCommand = command;
DataAdapter1.Fill(ds,s);
DataGridView1.DataSource = ds.Tables [ 0 ];







试试这个代码,看看。它可能有效。


试试 -



 SqlConnection connection = new SqlConnection(); 

private void comboBox1_SelectedIndexChanged(object sender,EventArgs e)
{
string s = comboBox1.Text;

connection.ConnectionString = @Data Source =(LocalDB)\ v11.0; AttachDbFilename = C:\ Users \Downloads\charityah.mdf; Integrated Security = True;

使用(连接)
{
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter(select * from+ s,connection);
DataSet ds = new DataSet();
adapter.Fill(ds,s);
dataGridView1.DataSource = ds;
dataGridView1.DataMember = s;

adapter.Fill(ds);
dataGridView1.DataSource = ds;
dataGridView1.DataMember = ds.Tables [0] .TableName;
dataGridView1.Refresh();
}


我会改变你的代码的一小部分来自



< pre lang =c#> dataGridview1.DataSource = ds;





to

 dataGridView1.DataSource = ds。表[ 0 ] 





更新:



  string  TableName = comboBox1.Text; 
string Query = Select * from + TableName;
DataSet ds = new DataSet();

使用(SqlConnection connection = new SqlConnection( @ Data Source =(LocalDB)\ v11.0; AttachDbFilename = C:\ Users \Downloads\charityah.mdf; Integrated Security = True))
{
SqlDataAdapter da = new SqlDataAdapater();
da.SelectCommand = new SqlCommand(查询,连接);
da.fill(ds,TableName);
}

DataGridview1.DataSource = ds.Tables [ 0 ];


Hello
I created a DataBase named charityah containing 5 tables
their names are listed in a combobox
when i choose one of them i want to display its content in a datagridview

What i tried is
first i linked the datagridview to this database
and tried this code that i found

SqlConnection connection = new SqlConnection();
 
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    string s = comboBox1.Text;
     
    connection.ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Downloads\charityah.mdf;Integrated Security=True";
         
    using (connection)
    {
    connection.Open();
    SqlDataAdapter adapter = new SqlDataAdapter("select * from "+s, connection);
    DataSet ds = new DataSet();
    adapter.Fill(ds, s);
    dataGridView1.DataSource = ds;
    dataGridView1.DataMember = s;
    dataGridView1.Refresh();   
}




this method doesn''t give me any error and it finds the tables but nothing is seenin the datagridview

解决方案

SqlConnection con = new SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Downloads\charityah.mdf;Integrated Security=True");
DataSet ds = new DataSet();
SqlDataAdapter DataAdapter1 = new SqlDataAdapter();
SqlCommand command = new SqlCommand();


command.CommandText = ("select * from"+s, connection);
command.CommandType = CommandType.Text;
command.Connection = con;

DataAdapter1.SelectCommand = command;
DataAdapter1.Fill(ds, s);
DataGridView1.DataSource = ds.Tables[0];




Try this code and see.It might work.


Try -

SqlConnection connection = new SqlConnection();
 
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    string s = comboBox1.Text;
     
    connection.ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Downloads\charityah.mdf;Integrated Security=True";
         
    using (connection)
    {
    connection.Open();
    SqlDataAdapter adapter = new SqlDataAdapter("select * from "+s, connection);
    DataSet ds = new DataSet();
    adapter.Fill(ds, s);
    dataGridView1.DataSource = ds;
    dataGridView1.DataMember = s;
    adapter.Fill(ds);
    dataGridView1.DataSource = ds;
    dataGridView1.DataMember = ds.Tables[0].TableName;
    dataGridView1.Refresh();   
}


I would change a small part of your code from

dataGridview1.DataSource = ds;



to

dataGridView1.DataSource = ds.Tables[0]



UPDATE:

string TableName = comboBox1.Text;
string Query = "Select * from " + TableName;
DataSet ds = new DataSet();

using(SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Downloads\charityah.mdf;Integrated Security=True"))
{
  SqlDataAdapter da = new SqlDataAdapater();
  da.SelectCommand = new SqlCommand(Query, connection);
  da.fill(ds, TableName);
}

DataGridview1.DataSource = ds.Tables[0];


这篇关于在DataGridView中显示来自sql的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆