如何从access数据库显示datagridview中的数据

时间:2021-05-14 19:30:46

I have the codes below here in displaying data in datagridview from access database. I have different rows but in only display the last row of data in database. I dont know what's wrong in my code.

在access数据库的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();
}

3 个解决方案

#1


6  

You can directly bind dataGridView1 using OleDbDataAdapter. Make sure that the names of the columns in the datagridview matches with the field names return by the query so it will contain blank columns and create another column for every fields.

可以使用OleDbDataAdapter直接绑定dataGridView1。确保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];
    }
}

#2


0  

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;
    }
}

#3


0  

Please use this code to display your data's to datagridview in c#.net

请使用此代码在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();

#1


6  

You can directly bind dataGridView1 using OleDbDataAdapter. Make sure that the names of the columns in the datagridview matches with the field names return by the query so it will contain blank columns and create another column for every fields.

可以使用OleDbDataAdapter直接绑定dataGridView1。确保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];
    }
}

#2


0  

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;
    }
}

#3


0  

Please use this code to display your data's to datagridview in c#.net

请使用此代码在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();