如何在页面中使用ACCESS数据库访问类

哎算了 2011-02-28 11:45:11
初学不久,网上找来的ACCESS数据库访问类,但是不知道实际怎么使用,请给点范例,越具体越好,谢谢

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;


/// <summary>
/// DataAccess 的摘要说明
/// </summary>
public class DataAccess
{
protected static OleDbConnection conn = new OleDbConnection();
protected static OleDbCommand comm = new OleDbCommand();
public DataAccess()
{
//init
}
private static void openConnection()
{
if (conn.State == ConnectionState.Closed)
{
conn.ConnectionString = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + ConfigurationManager.AppSettings["myconn"];//web.config文件里设定。
comm.Connection = conn;
try
{
conn.Open();
}
catch (Exception e)
{ throw new Exception(e.Message); }

}

}//打开数据库

private static void closeConnection()
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
conn.Dispose();
comm.Dispose();
}
}//关闭数据库

public static void excuteSql(string sqlstr)
{
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
comm.ExecuteNonQuery();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{ closeConnection(); }
}//执行sql语句

public static OleDbDataReader dataReader(string sqlstr)
{
OleDbDataReader dr = null;
try
{
openConnection();
comm.CommandText = sqlstr;
comm.CommandType = CommandType.Text;

dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
{
try
{
dr.Close();
closeConnection();
}
catch { }
}
return dr;
}//返回指定sql语句的OleDbDataReader对象,使用时请注意关闭这个对象。
public static void dataReader(string sqlstr, ref OleDbDataReader dr)
{
try
{
openConnection();
comm.CommandText = sqlstr;
comm.CommandType = CommandType.Text;
dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
{
try
{
if (dr != null && !dr.IsClosed)
dr.Close();
}
catch
{
}
finally
{
closeConnection();
}
}
}//返回指定sql语句的OleDbDataReader对象,使用时请注意关闭

public static DataSet dataSet(string sqlstr)
{
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(ds);

}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
return ds;
}//返回指定sql语句的dataset

public static void dataSet(string sqlstr, ref DataSet ds)
{
OleDbDataAdapter da = new OleDbDataAdapter();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(ds);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}//返回指定sql语句的dataset

public static DataTable dataTable(string sqlstr)
{
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(dt);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
return dt;
}//返回指定sql语句的datatable
public static void dataTable(string sqlstr, ref DataTable dt)
{
OleDbDataAdapter da = new OleDbDataAdapter();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(dt);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
}//返回指定sql语句的datatable

public static DataView dataView(string sqlstr)
{
OleDbDataAdapter da = new OleDbDataAdapter();
DataView dv = new DataView();
DataSet ds = new DataSet();
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(ds);
dv = ds.Tables[0].DefaultView;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
return dv;
}
//返回指定sql语句的dataview

}
...全文
99 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
哎算了 2011-03-01
  • 打赏
  • 举报
回复
up DataAccess 的摘要说明
请大家帮写个读出数据的调用代码来给我参考下,谢谢
哎算了 2011-03-01
  • 打赏
  • 举报
回复
看糊涂了,
那里有完整的代码或者教程啊
D_dan 2011-03-01
  • 打赏
  • 举报
回复
被你搞糊涂了。你那不是已经有了数据HELP了么?直接配置你的数据库的连接.然后调用就可以了.
最后你那个容器控键绑定一下不就可以了么?
哎算了 2011-03-01
  • 打赏
  • 举报
回复
那位好心人,可以再一段具体点代码?
就是把数据调用显示出来的完整点的,我好好学习下,谢谢了
哎算了 2011-03-01
  • 打赏
  • 举报
回复
那位好心人,可以再一段具体点代码?
就是把数据调用显示出来的完整点的,我好好学习下,谢谢了
哎算了 2011-03-01
  • 打赏
  • 举报
回复
继续找人帮我写段完整调用代码,祈祷
哎算了 2011-03-01
  • 打赏
  • 举报
回复
帮帮我啊
wuyq11 2011-02-28
  • 打赏
  • 举报
回复
看看petshop,如同SQLhelper
ajq1989 2011-02-28
  • 打赏
  • 举报
回复
看每个方法的返回值就是你要的答案
子夜__ 2011-02-28
  • 打赏
  • 举报
回复
这个就相当于一个类 里面有好多方法

你用的时候直接调用方法就行了

比如调用这个方法
public static void excuteSql(string sqlstr)
{
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
comm.ExecuteNonQuery();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{ closeConnection(); }
}//执行sql语句


static 是静态方法 直接 类名.方法名(参数就行了)


你在前台调用的时候这样

DataAccess.excuteSql("select * from table");//table是你数据库中的表


它返回的是DataView 类型 你就定义一个DataView 来接收它就好啦。

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧