Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用sql查询结果填充datagridview_C#_Winforms_Datagridview - Fatal编程技术网

C# 使用sql查询结果填充datagridview

C# 使用sql查询结果填充datagridview,c#,winforms,datagridview,C#,Winforms,Datagridview,我试图显示查询结果,但我一直得到一个空白的数据网格。 这就好像数据本身是不可见的 这是我的密码: private void Employee_Report_Load(object sender, EventArgs e) { string select = "SELECT * FROM tblEmployee"; Connection c = new Connection(); SqlDataAdapter dataAdapter = new SqlDataAd

我试图显示查询结果,但我一直得到一个空白的数据网格。
这就好像数据本身是不可见的

这是我的密码:

 private void Employee_Report_Load(object sender, EventArgs e)
 {
     string select = "SELECT * FROM tblEmployee";
     Connection c = new Connection();
     SqlDataAdapter dataAdapter = new SqlDataAdapter(select, c.con); //c.con is the connection string
     SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

     DataTable table = new DataTable();
     table.Locale = System.Globalization.CultureInfo.InvariantCulture;
     dataAdapter.Fill(table);
     bindingSource1.DataSource = table;

     dataGridView1.ReadOnly = true;        
     dataGridView1.DataSource = bindingSource1;
}

此代码有什么问题?

您不需要
bindingSource1


只需设置
dataGridView1.DataSource=table

尝试将您的
DataGridView
绑定到
DataTable
DefaultView

dataGridView1.DataSource = table.DefaultView;

这是你的密码。下一步,忘记绑定源

 var select = "SELECT * FROM tblEmployee";
 var c = new SqlConnection(yourConnectionString); // Your Connection String here
 var dataAdapter = new SqlDataAdapter(select, c); 

 var commandBuilder = new SqlCommandBuilder(dataAdapter);
 var ds = new DataSet();
 dataAdapter.Fill(ds);
 dataGridView1.ReadOnly = true; 
 dataGridView1.DataSource = ds.Tables[0];

如果将数据源设置为添加到表单但未使用的数据集,则可能会得到一个空白数据网格。如果您是根据上述代码以编程方式设置数据源,请将此设置为无。

您可以尝试此示例,并始终检查连接字符串。,您可以使用此示例,也可以不使用bindingsource。您可以将数据加载到datagridview

private void Employee_Report_Load(object sender, EventArgs e)
{
        var table = new DataTable();

        var connection = "ConnectionString";

        using (var con = new SqlConnection { ConnectionString = connection })
        {
            using (var command = new SqlCommand { Connection = con })
            {

                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }

                con.Open();

                try
                {
                    command.CommandText = @"SELECT * FROM tblEmployee";
                    table.Load(command.ExecuteReader());

                    bindingSource1.DataSource = table;

                    dataGridView1.ReadOnly = true;
                    dataGridView1.DataSource = bindingSource1;

                }
                catch(SqlException ex)
                {
                    MessageBox.Show(ex.Message + " sql query error.");
                }

            }

        }

 }

必须将属性表添加到DataGridView数据源

 dataGridView1.DataSource = table.Tables[0];

如果您使用的是mysql,则可以使用此代码

string con = "SERVER=localhost; user id=root; password=; database=databasename";
    private void loaddata()
{
MySqlConnection connect = new MySqlConnection(con);
connect.Open();
try
{
MySqlCommand cmd = connect.CreateCommand();
cmd.CommandText = "SELECT * FROM DATA1";
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
datagrid.DataSource = dt;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}

这被认为是最安全和错误的pron查询:

    public void Load_Data()
        {
            using (SqlConnection connection = new SqlConnection(DatabaseServices.connectionString)) //use your connection string here
            {
                var bindingSource = new BindingSource();
                string fetachSlidesRecentSQL = "select top (50) * from dbo.slides order by created_date desc";
                using (SqlDataAdapter dataAdapter = new SqlDataAdapter(fetachSlidesRecentSQL, connection))
                {
                    try
                    {
                       SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

                        DataTable table = new DataTable();
                        dataAdapter.Fill(table);
                        bindingSource.DataSource = table;
                        recent_slides_grd_view.ReadOnly = true;
                        recent_slides_grd_view.DataSource = bindingSource;
                    }
                    catch (SqlException ex)
                    {
                       MessageBox.Show(ex.Message.ToString(), "ERROR Loading");
                    }
                    finally
                    {
                        connection.Close();
                    }
                }

            }
        }

几年后,但这里是最简单的为其他情况下

String connectionString = @"Data Source=LOCALHOST;Initial Catalog=DB;Integrated Security=true";

SqlConnection cnn = new SqlConnection(connectionString);
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM tblEmployee;", cnn);

DataTable data = new DataTable();
sda.Fill(data);

DataGridView1.DataSource = data;

使用
DataSet
是不必要的,而且
DataTable
应该足够好了
SQLCommandBuilder
也没有必要。

在datagridview中绑定数据您可以“填充”一个
DataTable
没有问题。不需要
数据集
。它确实可以工作,但是
数据集
是额外的开销,没有什么用处。@DonThomasBoyle在第二个示例代码中,使用的类是
SqlServerCe
而不是
SqlServer
。这可能会让人困惑,就像数据本身在我的案例中是不可见的一样,你看到datagridview中的列了吗?如果是,则查询不返回任何行!!我在SQLServer2008中检查了我的查询,它确实返回了employee Table的所有数据。已经有了可接受的答案,这提供了比您更多的信息。这是相当多余的。
String connectionString = @"Data Source=LOCALHOST;Initial Catalog=DB;Integrated Security=true";

SqlConnection cnn = new SqlConnection(connectionString);
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM tblEmployee;", cnn);

DataTable data = new DataTable();
sda.Fill(data);

DataGridView1.DataSource = data;