我想要那个ASP.NET连接Access数据库的后台代码,以及登录注册的,配置web.config的,可以吗?万分感谢
展开全部
以下代码并非全部,只是连接数据库的部分,仅供参考学习。
web.config ----代码,请参考----
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="StrConnection" value="Provider=Microsoft.Jet.OLEDB.4.0; Data Source="/>
<add key="dbPath" value="~/App_Data/OaOle.mdb"/>
<add key="Password" value=""/>
</appSettings>
<system.web>
<globalization requestEncoding="utf-8" responseEncoding="utf-8"/>
<pages>
<controls>
</controls>
</pages>
<customErrors mode="Off"/>
<compilation defaultLanguage="c#" />
</system.web>
</configuration>
-----------
xxx.cs 类,用来连接数据库的。
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.OleDb;
/// <summary>
///数据库处理类
/// </summary>
public class OleDbHelp
{
private string strConnection;
private OleDbConnection OleConn;
private OleDbCommand OleCmd;
private OleDbDataAdapter Oleda;
private OleDbDataReader Oledr;
private DataSet Oleds;
public OleDbHelp()
{
//返回类型
strConnection = System.Configuration.ConfigurationManager.AppSettings["StrConnection"].ToString()
+ System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["dbPath"])
+ ";Jet OLEDB:Database Password=" + System.Configuration.ConfigurationManager.AppSettings["Password"].ToString();
OleConn = new OleDbConnection(strConnection);
OleCmd = new OleDbCommand();
Oleda = new OleDbDataAdapter();
Oleds = new DataSet();
}
public OleDbDataReader drSelect(string select_sql)
{
OleConn.Open();
OleCmd = new OleDbCommand(select_sql, OleConn);
Oledr = OleCmd.ExecuteReader();
return Oledr;
}
public DataSet dsSelect(string select_sql)
{
OleConn.Open();
Oleda.SelectCommand = new OleDbCommand(select_sql, OleConn);
Oleda.Fill(Oleds, "temp");
return Oleds;
}
public OleDbDataAdapter daSelect(string select_sql)
{
OleConn.Open();
Oleda.SelectCommand = new OleDbCommand(select_sql, OleConn);
return Oleda;
}
public void Close()
{
if (OleConn.State != ConnectionState.Closed)
{ OleConn.Close(); }
}
public void Update(string updat_sql)
{
OleCmd.CommandText = updat_sql;
OleCmd.Connection = OleConn;
OleConn.Open();
OleCmd.ExecuteNonQuery();
}
public void Insert(string insert_sql)
{
OleCmd.CommandText = insert_sql;
OleCmd.Connection = OleConn;
OleConn.Open();
OleCmd.ExecuteNonQuery();
}
public void Delete(string delete_sql)
{
OleCmd.CommandText = delete_sql;
OleCmd.Connection = OleConn;
OleConn.Open();
OleCmd.ExecuteNonQuery();
}
}
------------------
XXX.cs中使用方法
OleDbHelp mydb = new OleDbHelp(); //
OleDbDataReader mydr = mydb.drSelect("select max(cNum) as maxNum from [ContractData]"); //
try
{
tmpY = mydr["maxNum"].ToString(); //字段内容读取演示
catch (Exception sqlexc)
{
Response.Write("读取数据出错:" + sqlexc);
}
mydb.Close(); //关闭数据库
------------------------------------------------------
web.config ----代码,请参考----
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="StrConnection" value="Provider=Microsoft.Jet.OLEDB.4.0; Data Source="/>
<add key="dbPath" value="~/App_Data/OaOle.mdb"/>
<add key="Password" value=""/>
</appSettings>
<system.web>
<globalization requestEncoding="utf-8" responseEncoding="utf-8"/>
<pages>
<controls>
</controls>
</pages>
<customErrors mode="Off"/>
<compilation defaultLanguage="c#" />
</system.web>
</configuration>
-----------
xxx.cs 类,用来连接数据库的。
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.OleDb;
/// <summary>
///数据库处理类
/// </summary>
public class OleDbHelp
{
private string strConnection;
private OleDbConnection OleConn;
private OleDbCommand OleCmd;
private OleDbDataAdapter Oleda;
private OleDbDataReader Oledr;
private DataSet Oleds;
public OleDbHelp()
{
//返回类型
strConnection = System.Configuration.ConfigurationManager.AppSettings["StrConnection"].ToString()
+ System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["dbPath"])
+ ";Jet OLEDB:Database Password=" + System.Configuration.ConfigurationManager.AppSettings["Password"].ToString();
OleConn = new OleDbConnection(strConnection);
OleCmd = new OleDbCommand();
Oleda = new OleDbDataAdapter();
Oleds = new DataSet();
}
public OleDbDataReader drSelect(string select_sql)
{
OleConn.Open();
OleCmd = new OleDbCommand(select_sql, OleConn);
Oledr = OleCmd.ExecuteReader();
return Oledr;
}
public DataSet dsSelect(string select_sql)
{
OleConn.Open();
Oleda.SelectCommand = new OleDbCommand(select_sql, OleConn);
Oleda.Fill(Oleds, "temp");
return Oleds;
}
public OleDbDataAdapter daSelect(string select_sql)
{
OleConn.Open();
Oleda.SelectCommand = new OleDbCommand(select_sql, OleConn);
return Oleda;
}
public void Close()
{
if (OleConn.State != ConnectionState.Closed)
{ OleConn.Close(); }
}
public void Update(string updat_sql)
{
OleCmd.CommandText = updat_sql;
OleCmd.Connection = OleConn;
OleConn.Open();
OleCmd.ExecuteNonQuery();
}
public void Insert(string insert_sql)
{
OleCmd.CommandText = insert_sql;
OleCmd.Connection = OleConn;
OleConn.Open();
OleCmd.ExecuteNonQuery();
}
public void Delete(string delete_sql)
{
OleCmd.CommandText = delete_sql;
OleCmd.Connection = OleConn;
OleConn.Open();
OleCmd.ExecuteNonQuery();
}
}
------------------
XXX.cs中使用方法
OleDbHelp mydb = new OleDbHelp(); //
OleDbDataReader mydr = mydb.drSelect("select max(cNum) as maxNum from [ContractData]"); //
try
{
tmpY = mydr["maxNum"].ToString(); //字段内容读取演示
catch (Exception sqlexc)
{
Response.Write("读取数据出错:" + sqlexc);
}
mydb.Close(); //关闭数据库
------------------------------------------------------
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询