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)
}
oledb的使用网上的确很多.\x0d\x0a如果只是查询. 写入. 删除,我们常用的就是\x0d\x0aOleDbConnection '用于写数据库的连接\x0d\x0aOleDbCommand '用于数据的 *** 作 接收SQL语句 并执行\x0d\x0aOleDbDataReader '查询了,我们就用这个类来读取查询的对象\x0d\x0a1).OleDbConnection \x0d\x0a dim conn as new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0Data Source =数据库的路径")'连接字符串一般就这样 \x0d\x0a 查询前在打开 \x0d\x0a conn.open\x0d\x0a 2).OleDbCommand \x0d\x0a dim cmd as new OleDbCommand ("SQL语句",conn)'SQL语句决定了你是查询写入还是更新删除\x0d\x0a '''\x0d\x0a如果是查询,你还要用到OleDbDataReader \x0d\x0a 这个是不用新建的不能用New \x0d\x0adim rd as OleDbDataReader=cmd.ExecuteReader'cmd.ExecuteReader 方法就是读取查询的内容\x0d\x0acmd.ExecuteNonQuery用于执行 *** 作 返回变动的条数 ,一般用于 添加 删除 更新 等其它 *** 作\x0d\x0a \x0d\x0a其实就这些了,就是要学点SQL语句就行了.\x0d\x0a标准执行过程就是\x0d\x0aDim conn As New OleDbConnection("连接字符串")\x0d\x0aconn.Open()\x0d\x0aDim cmd As New OleDbCommand("SQL语句", conn)\x0d\x0a'如果是写入 更新 删除 则\x0d\x0acmd.ExecuteNonQuery()\x0d\x0a 否则\x0d\x0aDim rd As OleDbDataReader = cmd.ExecuteReader\x0d\x0a\x0d\x0aIf rd.Read Then\x0d\x0aDim d As Object = rd.Item("字段名")\x0d\x0aEnd If\x0d\x0a结束如果\x0d\x0ard.Close()\x0d\x0acmd.Dispose()\x0d\x0aconn.Close()\x0d\x0a \x0d\x0a上面要有 Imports System.Data.OleDb欢迎分享,转载请注明来源:内存溢出
评论列表(0条)