我在下面的代码中显示了来自access database的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();
}
答案 0 :(得分:6)
您可以使用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];
}
}
答案 1 :(得分: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();
答案 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 :(得分:-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());
}