求C# winform中 access连接、读取、添加、删除和修改的通用类 的使用案例。
展开全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1
{
using System.Data;
using System.Data.OleDb;
public class AccessHelp
{
// 连接数据源
private OleDbConnection conn = null;
/// <summary>
/// 数据源
/// </summary>
/// <param name="conStr">数据源连接字符串</param>
public OleDbConnection DataBase(string conStr)
{
conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ conStr +";Jet OLEDB:System Database=system.mdw;");
this.Open();
return conn;
}
/// <summary>
/// 根据SQL查询返回DataSet对象,如果没有查询到则返回NULL
/// </summary>
/// <param name="sql">查询语句</param>
/// <returns>DataSet</returns>
public DataSet GetDataSet(string sql)
{
DataSet ds = new DataSet();
try
{
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.CommandTimeout = 20;
System.Data.OleDb.OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(ds, "tempTable");
}
catch (Exception e)
{
ds = null;
}
return ds;
}
/// <summary>
/// 根据SQL查询返回DataSet对象,如果没有查询到则返回NULL
/// </summary>
/// <param name="sql">查询语句</param>
/// <param name="sRecord">开始记录数</param>
/// <param name="mRecord">最大记录数</param>
/// <returns>DataSet</returns>
public DataSet GetDataSet(string sql, int sRecord, int mRecord)
{
DataSet ds = new DataSet();
try
{
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.CommandTimeout = 20;
System.Data.OleDb.OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(ds, sRecord, mRecord, "tempTable");
}
catch (Exception e)
{
ds = null;
}
return ds;
}
/// <summary>
/// 对数据库的增,删,改的操作
/// </summary>
/// <param name="sql">SQL语句</param>
/// <returns>是否成功</returns>
public bool ExecuteDataBase(string sql)
{
bool succeed = false;
int cnt = 0;
try
{
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.CommandTimeout = 20;
if (this.Open())
cnt = cmd.ExecuteNonQuery();
}
catch (Exception e)
{
e.ToString();
}
finally
{
if (cnt > 0)
{
succeed = true;
}
}
return succeed;
}
/// <summary>
/// 获得该SQL查询返回的第一行第一列的值,如果没有查询到则返回NULL
/// </summary>
/// <param name="sql">查询语句</param>
/// <returns>返回的第一行第一列的值</returns>
public string GetScalar(string sql)
{
string str = null;
try
{
OleDbCommand cmd = new OleDbCommand(sql, conn);
if (this.Open())
str = cmd.ExecuteScalar().ToString();
}
catch (Exception e)
{
e.ToString();
}
return str;
}
/// <summary>
/// 获得该SQL查询返回DataTable,如果没有查询到则返回NULL
/// </summary>
/// <param name="sql">查询语句</param>
/// <returns></returns>
public DataTable GetDataTable(string sql)
{
DataTable tb = null;
DataSet ds = this.GetDataSet(sql);
if (ds != null)
{
tb = ds.Tables["tempTable"];
}
return tb;
}
/// <summary>
/// 打开数据库连接.
/// </summary>
private bool Open()
{
bool succeed = false;
try
{
if (conn.State == System.Data.ConnectionState.Closed)
{
conn.Open();
succeed = true;
}
else if (conn.State == System.Data.ConnectionState.Broken)
{
conn.Close();
conn.Open();
succeed = true;
}
else if (conn.State == System.Data.ConnectionState.Open)
{
succeed = true;
}
}
catch (Exception e)
{
e.ToString();
}
return succeed;
}
/// <summary>
/// 关闭数据库连接
/// </summary>
public void Close()
{
if (conn != null)
{
conn.Close();
}
}
/// <summary>
/// 释放数据库连接资源
/// </summary>
public void Dispose()
{
if (conn != null)
{
conn.Dispose();
conn = null;
}
}
}
}
你只要把Access数据库的存在的目录地址,调用DataBase这个方法就行了
仔细看看这个help类,都有中文解释的,和SqlHelper类一样调用,很简单的
项目我先发到你的QQ邮箱,今天加你QQ,可能你不在线吧
你邮箱设置了禁止接收陌生邮件的,我发离线文件给你吧
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1
{
using System.Data;
using System.Data.OleDb;
public class AccessHelp
{
// 连接数据源
private OleDbConnection conn = null;
/// <summary>
/// 数据源
/// </summary>
/// <param name="conStr">数据源连接字符串</param>
public OleDbConnection DataBase(string conStr)
{
conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ conStr +";Jet OLEDB:System Database=system.mdw;");
this.Open();
return conn;
}
/// <summary>
/// 根据SQL查询返回DataSet对象,如果没有查询到则返回NULL
/// </summary>
/// <param name="sql">查询语句</param>
/// <returns>DataSet</returns>
public DataSet GetDataSet(string sql)
{
DataSet ds = new DataSet();
try
{
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.CommandTimeout = 20;
System.Data.OleDb.OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(ds, "tempTable");
}
catch (Exception e)
{
ds = null;
}
return ds;
}
/// <summary>
/// 根据SQL查询返回DataSet对象,如果没有查询到则返回NULL
/// </summary>
/// <param name="sql">查询语句</param>
/// <param name="sRecord">开始记录数</param>
/// <param name="mRecord">最大记录数</param>
/// <returns>DataSet</returns>
public DataSet GetDataSet(string sql, int sRecord, int mRecord)
{
DataSet ds = new DataSet();
try
{
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.CommandTimeout = 20;
System.Data.OleDb.OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(ds, sRecord, mRecord, "tempTable");
}
catch (Exception e)
{
ds = null;
}
return ds;
}
/// <summary>
/// 对数据库的增,删,改的操作
/// </summary>
/// <param name="sql">SQL语句</param>
/// <returns>是否成功</returns>
public bool ExecuteDataBase(string sql)
{
bool succeed = false;
int cnt = 0;
try
{
OleDbCommand cmd = new OleDbCommand(sql, conn);
cmd.CommandTimeout = 20;
if (this.Open())
cnt = cmd.ExecuteNonQuery();
}
catch (Exception e)
{
e.ToString();
}
finally
{
if (cnt > 0)
{
succeed = true;
}
}
return succeed;
}
/// <summary>
/// 获得该SQL查询返回的第一行第一列的值,如果没有查询到则返回NULL
/// </summary>
/// <param name="sql">查询语句</param>
/// <returns>返回的第一行第一列的值</returns>
public string GetScalar(string sql)
{
string str = null;
try
{
OleDbCommand cmd = new OleDbCommand(sql, conn);
if (this.Open())
str = cmd.ExecuteScalar().ToString();
}
catch (Exception e)
{
e.ToString();
}
return str;
}
/// <summary>
/// 获得该SQL查询返回DataTable,如果没有查询到则返回NULL
/// </summary>
/// <param name="sql">查询语句</param>
/// <returns></returns>
public DataTable GetDataTable(string sql)
{
DataTable tb = null;
DataSet ds = this.GetDataSet(sql);
if (ds != null)
{
tb = ds.Tables["tempTable"];
}
return tb;
}
/// <summary>
/// 打开数据库连接.
/// </summary>
private bool Open()
{
bool succeed = false;
try
{
if (conn.State == System.Data.ConnectionState.Closed)
{
conn.Open();
succeed = true;
}
else if (conn.State == System.Data.ConnectionState.Broken)
{
conn.Close();
conn.Open();
succeed = true;
}
else if (conn.State == System.Data.ConnectionState.Open)
{
succeed = true;
}
}
catch (Exception e)
{
e.ToString();
}
return succeed;
}
/// <summary>
/// 关闭数据库连接
/// </summary>
public void Close()
{
if (conn != null)
{
conn.Close();
}
}
/// <summary>
/// 释放数据库连接资源
/// </summary>
public void Dispose()
{
if (conn != null)
{
conn.Dispose();
conn = null;
}
}
}
}
你只要把Access数据库的存在的目录地址,调用DataBase这个方法就行了
仔细看看这个help类,都有中文解释的,和SqlHelper类一样调用,很简单的
项目我先发到你的QQ邮箱,今天加你QQ,可能你不在线吧
你邮箱设置了禁止接收陌生邮件的,我发离线文件给你吧
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在百度搜“SQL语句大全”,下载看一下,就都可以了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询