如何使用.net访问Access数据库

如何使用.net访问Access数据库,第1张

使用.net访问Access数据库

1.BL层:新增一个DataAccess类。

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

namespace Haley.FrameWork

{

/// <summary>

/// DataAccess 的摘要说明

/// </summary>

public class DataAccess

{

protected static OleDbConnection conn = new OleDbConnection()

protected static OleDbCommand comm = new OleDbCommand()

public DataAccess()

{

//init

}

/// <summary>

/// 打开数据库

/// </summary>

private static void openConnection()

{

if (conn.State == ConnectionState.Closed)

{

conn.ConnectionString = @"Provider=Microsoft.Jet.OleDb.4.0Data Source=" + ConfigurationManager.AppSettings["myconn"]//web.config文件里设定。

comm.Connection = conn

try

{

conn.Open()

}

catch (Exception e)

{ throw new Exception(e.Message)}

}

}

/// <summary>

/// 关闭数据库

/// </summary>

private static void closeConnection()

{

if (conn.State == ConnectionState.Open)

{

conn.Close()

conn.Dispose()

comm.Dispose()

}

}

/// <summary>

/// 执行sql语句

/// </summary>

/// <param name="sqlstr"></param>

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()}

}

/// <summary>

/// 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭这个对象。

/// </summary>

/// <param name="sqlstr"></param>

/// <returns></returns>

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

}

/// <summary>

/// 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭

/// </summary>

/// <param name="sqlstr"></param>

/// <param name="dr"></param>

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()

}

}

}

/// <summary>

/// 返回指定sql语句的dataset

/// </summary>

/// <param name="sqlstr"></param>

/// <returns></returns>

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

}

/// <summary>

/// 返回指定sql语句的dataset

/// </summary>

/// <param name="sqlstr"></param>

/// <param name="ds"></param>

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()

}

}

/// <summary>

/// 返回指定sql语句的datatable

/// </summary>

/// <param name="sqlstr"></param>

/// <returns></returns>

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

}

/// <summary>

/// 返回指定sql语句的datatable

/// </summary>

/// <param name="sqlstr"></param>

/// <param name="dt"></param>

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()

}

}

/// <summary>

/// 返回指定sql语句的dataview

/// </summary>

/// <param name="sqlstr"></param>

/// <returns></returns>

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

}

}

}

2.Web.Config:

在Config文件中添加Access文件的路径:

<appSettings>

<!-- 这里是存放Access文件的地址。用Access文件存放的路径替换下边的路径。-->

<add key="myconn" value="D:\Test\Test.mdb"/>

</appSettings >

3.UI层。

在页面添加控件,在cs文件中使用上边的类。

protected void Button1_Click(object sender, EventArgs e)

{

string name = TextBox1.Text

string strSql = "insert into Table1(Name) values('" + name + "')"

DataAccess.excuteSql(strSql)

}

使用.net访问Access数据库:

1.BL层:新增一个DataAccess类。

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

namespace Haley.FrameWork

{

/// <summary>

/// DataAccess 的摘要说明

/// </summary>

public class DataAccess

{

protected static OleDbConnection conn = new OleDbConnection()

protected static OleDbCommand comm = new OleDbCommand()

public DataAccess()

{

//init

}

/// <summary>

/// 打开数据库

/// </summary>

private static void openConnection()

{

if (conn.State == ConnectionState.Closed)

{

用一个<asp:TextBox >控件,设置id=content,然后在本页面xxxx.aspx.cs中用SQL查询出要显示的文本,然后赋值给textbox控件的text属性即可。比如:

protected void Page_Load(object sender, EventArgs e)

{

this.content.Text = "今天是个好日子!"

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9958756.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-03
下一篇 2023-05-03

发表评论

登录后才能评论

评论列表(0条)

保存