我试图加载和显示一个表从sql到我的数据网格在c#,但它不显示

本文关键字:显示 网格 数据网 数据 我的 sql 加载 一个 | 更新日期: 2023-09-27 18:12:10

//instantiating connection          
dbConn = new SqlConnection("Data Source = local host; initial catalog=Project; Integrated Security = SSPI ");
ds = new DataSet();
//select everything from the table 
dbCommand = new SqlCommand("SELECT * FROM Shops;", dbConn);
dbAdapter = new SqlDataAdapter(dbCommand);
//filling the datagrid
dbAdapter.Fill(ds, "All Shops");
dataGridView1.DataSource = ds.Tables["All Shops"];

我试图加载和显示一个表从sql到我的数据网格在c#,但它不显示

要将数据显示到数据网格中,您需要分配数据源,然后使用DataBind()绑定数据。

dataGridView1.DataSource = ds.Tables["All Shops"];
dataGridView1.DataBind(); // use this after assigning the datasource
 private void GetResults()
        {
            //Establishing the MySQL Connection
             MySqlConnection conn = new MySqlConnection("Database=potentiality_live;Data Source=eu;User Id=ptly;Password=phat40");
            string query;
            MySqlCommand SqlCommand;
            MySqlDataReader reader;
            MySqlDataAdapter adapter = new MySqlDataAdapter();
//Open the connection to db
            conn.Open();
//Generating the query to fetch the contact details
            query = "SELECT id,date_time,link FROM'sdfsdfsdf'";
 SqlCommand = new MySqlCommand(query, conn);
            adapter.SelectCommand = new MySqlCommand(query, conn);
//execute the query
            reader = SqlCommand.ExecuteReader();
//Assign the results 
            GridView1.DataSource = reader;
//Bind the data
            GridView1.DataBind();
}
参考:

http://www.codeproject.com/Questions/453091/how-to-get-data-from-sql-database-to-gridview-by-c