6

我在下面的代码中显示来自访问数据库的 datagridview 中的数据。我有不同的行,但只显示数据库中的最后一行数据。我不知道我的代码有什么问题。

    dataGridView1.Columns.Add("UserID", "UserID");
    dataGridView1.Columns.Add("FirstName", "FirstName");
    dataGridView1.Columns.Add("MI", "MI");
    dataGridView1.Columns.Add("LastName", "LastName");
    dataGridView1.Columns.Add("Birthdate", "Birthdate");
    dataGridView1.Columns.Add("Address", "Address");
    dataGridView1.Columns.Add("UserName", "UserName");
    dataGridView1.Columns.Add("UserPassword", "UserPassword");
    dataGridView1.Columns.Add("Rights", "Rights");


    OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\SISC-STRONGHOLD\MIS!\wilbert.beltran\SEEDBucksDbase.accdb");
    conn.Open();
    OleDbCommand cmd = new OleDbCommand();
    cmd.Connection = conn;
    cmd.CommandText = "SELECT * From TableAcct";
    OleDbDataReader reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        dataGridView1.Rows.Add();

        dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["UserID"].Value = reader[0].ToString();
        dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["FirstName"].Value = reader[1].ToString();
        dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["MI"].Value = reader[2].ToString();
        dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["LastName"].Value = reader[3].ToString();
        dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Birthdate"].Value = reader[4].ToString();
        dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Address"].Value = reader[5].ToString();
        dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["UserName"].Value = reader[7].ToString();
        dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["UserPassword"].Value = reader[8].ToString();
        dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Rights"].Value = reader[9].ToString();
    }
    conn.Close();
}
4

4 回答 4

7

可以直接dataGridView1使用绑定OleDbDataAdapter。确保 datagridview 中列的名称与查询返回的字段名称匹配,以便它将包含空白列并为每个字段创建另一列。

string connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;
        Data Source=\\SISC-STRONGHOLD\MIS!\wilbert.beltran\SEEDBucksDbase.accdb";
string query = "SELECT * From TableAcct";
using(OleDbConnection conn = new OleDbConnection(connStr))
{
    using(OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
    {
        DataSet ds = new DataSet();
        adapter.Fill(ds);
        DataGridView1.DataSource= ds.Tables[0];
    }
}
于 2013-03-01T02:42:52.760 回答
1
public partial class WebForm1 : System.Web.UI.Page
{
    public String name,type,rvw;


    public void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Uz!\Documents\Data_Ware.mdf;Integrated Security=True;Connect Timeout=30");
        SqlDataAdapter sda = new SqlDataAdapter("Select * From CoffeeDB  ", con);
        DataTable dt = new DataTable();
        sda.Fill(dt);

        GridView.DataSource = dt;
    }
}
于 2016-03-13T09:18:01.887 回答
0

请使用此代码在 c#.net 中将您的数据显示到 datagridview

 OleDBConnection con=new      OleDBConnection("Copy your database database path and paste it");
    con.open();
    Dataset ds=new Dataset();
         OleDBDataAdapter sdr=new      OleDBDataAdapter("select * from table_name",con);
    sdr.Fill(ds);
    datagridview1.Datasource=ds.tables[0];
    con.close();
于 2015-12-15T11:30:13.153 回答
-1
string con= "SERVER=localhost; user id=root; password=; database=dbname";

MySqlConnection connect = new MySqlConnection(con);
connect.Open();
try
{
string sqlQuery = "SELECT * FROM DATA WHERE date(date) = date(now())";
MySqlDataAdapter da = new MySqlDataAdapter(sqlQuery, connect);
DataTable ds = new DataTable();
da.Fill(ds);
Datagrid.DataSource = ds;
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
于 2019-01-23T03:06:50.623 回答