从Access数据库中读取数据,用TreeView显示数据名称列表:
using System.Data.OleDb; //引用命名空间
string sql1 = "select * from 客户";//编写sql语句
string sql2 = "select * from 订单";
System.Data.OleDb.OleDbConnection OleDbConnection1 = new System.Data.OleDb.OleDbConnection();
//OleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data //Source=F:\\SQL\\Sources\\Northwind.mdb;";//适用于2003版本Access
OleDbConnection1.ConnectionString = @"Provider=Microsoft.Ace.OLEDB.12.0;Data Source=F:\\SQL\\Sources\\Northwind.mdb;"; //新建数据库连接,适用于2007版本Access
OleDbConnection1.Open();
System.Data.OleDb.OleDbDataAdapter OleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter(sql1, OleDbConnection1);
System.Data.OleDb.OleDbDataAdapter OleDbDataAdapter2 = new System.Data.OleDb.OleDbDataAdapter(sql2, OleDbConnection1);
OleDbDataAdapter1.Fill(DataSet1, "客户"); //使用OleDbCommand类来执行Sql语句
OleDbDataAdapter2.Fill(DataSet1, "订单");
TreeNode Root = new TreeNode("Northwind 数据库");
TreeView1.Nodes.Add(Root); //显示数据名称列表
//循环查找数据集中的表对象
foreach (DataTable Table in DataSet1.Tables)
{
TreeNode n1 = new TreeNode(Table.TableName);
//循环查找每个表中的列对象
foreach (DataColumn column in DataSet1.Tables[Table.TableName].Columns)
{
//将表中的字段名加入到每个表的节点下
n1.Nodes.Add(column.ColumnName);
}
//将数据集中的所有表名加入到reeView的根节点下
Root.Nodes.Add(n1);
}
OleDbConnection1.Close(); //关闭数据库连接
ACCESS 连接,查询,编辑,更新:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Data;
class AccessHelper
{
private static string connStr = @"Provider = Microsoft.Ace.OLEDB.12.0;Data Source = d:\login.accdb";
public static OleDbConnection GetConn()
{
OleDbConnection tempconn = new OleDbConnection(connStr);
MessageBox.Show(tempconn.DataSource);
tempconn.Open();
MessageBox.Show(tempconn.State.ToString());
return (tempconn);
}
/// <summary>
/// 执行增加、删除、修改指令
/// </summary>
/// <param name="sql">增加、删除、修改的sql语句</param>
/// <param name="param">sql语句的参数</param>
/// <returns></returns>
public static int ExecuteNonQuery(string sql, params OleDbParameter[] param)
{
using (OleDbConnection conn = new OleDbConnection(connStr))
{
using (OleDbCommand cmd = new OleDbCommand(sql, conn))
{
if (param != null)
{
cmd.Parameters.AddRange(param);
}
conn.Open();
return (cmd.ExecuteNonQuery());
}
}
}
/// <summary>
/// 执行查询指令,获取返回的首行首列的值
/// </summary>
/// <param name="sql">查询sql语句</param>
/// <param name="param">sql语句的参数</param>
/// <returns></returns>
public static object ExecuteScalar(string sql, params OleDbParameter[] param)
{
using (OleDbConnection conn = new OleDbConnection(connStr))
{
using (OleDbCommand cmd = new OleDbCommand(sql, conn))
{
if (param != null)
{
cmd.Parameters.AddRange(param);
}
conn.Open();
return (cmd.ExecuteScalar());
}
}
}
/// <summary>
/// 执行查询指令,获取返回的datareader
/// </summary>
/// <param name="sql">查询sql语句</param>
/// <param name="param">sql语句的参数</param>
/// <returns></returns>
public static OleDbDataReader ExecuteReader(string sql, params OleDbParameter[] param)
{
OleDbConnection conn = new OleDbConnection(connStr);
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
if (param != null)
{
cmd.Parameters.AddRange(param);
}
conn.Open();
return (cmd.ExecuteReader(CommandBehavior.CloseConnection));
}
/// <summary>
/// 执行查询指令,获取返回datatable
/// </summary>
/// <param name="sql">查询sql语句</param>
/// <param name="param">sql语句的参数</param>
/// <returns></returns>
public static DataTable ExecuteDatable(string sql, params OleDbParameter[] param)
{
using (OleDbConnection conn = new OleDbConnection(connStr))
{
using (OleDbCommand cmd = new OleDbCommand(sql, conn))
{
if (param != null)
{
cmd.Parameters.AddRange(param);
}
DataTable dt = new DataTable();
OleDbDataAdapter sda = new OleDbDataAdapter(cmd);
sda.Fill(dt);
return (dt);
}
}
}
}