DataAccess类是数据库中的一个类,可以用来获取服务器上的数据,程序编译。
用法如下:
* User: CoderLu * Date: 2010/10/28 * Time: 21:16 * */ using System using System.Text using System.Data using System.Data.SqlClient using System.Configuration using System.Collections.Generic namespace DAL { /// <summary> /// 执行数据基础 *** 作的数据库访问类 /// </summary> public class DataAccess { #region 定义连接字符串connStr /// <summary> /// 定义连接字符串connStr /// </summary> private string connStr = ConfigurationManager.ConnectionStrings["conn"].ToString() #endregion #region 声明连接对象,暂不实例化 private SqlConnection connection #endregion #region 默认构造方法,不打开连接 /// <summary> /// 默认构造方法,不打开连接 /// </summary> public DataAccess() { //OpenConn() } #endregion #region 当连接对象资源被系统回收时,调用关闭连接方法 /// <summary> /// 当连接对象资源被系统回收时,调用关闭连接方法 /// </summary> ~DataAccess() { try { if (this.connection != null) this.connection.Close() } catch (Exception) { } try { Dispose() } catch { } } #endregion 可以是可以,但是你这样访问会出问题,ACCESS是以独占形式存在的,当那边在使用,你这边就只能只读了,如果你不会编程哪我教你个简单办法:在局域网范围随便那台电脑安装个SQL Server
用ACCESS在局域网范围新建一个连接表,让连接表去链接SQL Server的表,这教网络连接表
然后在局域网范围你的ACCESS就可以作为客户端任意使用,SQL Server变成了数据服务器,前提条件SQL Server电脑是开启的,并且开启用户访问权限
使用.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)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)