幻境2层回1坐标:求ASP.NET中访问ACCESS数据库的代码

来源:百度文库 编辑:高考快车 时间:2020/02/26 01:25:05
求ASP.NET中访问ACCESS数据库的代码,那位有啊?

下面是C#的代码,决对可用!
using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Web;
namespace MCF
{
/// <summary>
/// Cdatabase 的摘要说明。
/// </summary>
public class Cdatabase
{
private OleDbConnection AccessConn=new System.Data.OleDb.OleDbConnection();
public Cdatabase()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

#region"用于关闭连接"
private void ConnClose()
{
if(ConnState())
{
AccessConn.Close();
}
}
#endregion

#region"ConnState 用于返回当前连接的状态"
private bool ConnState()
{
if( AccessConn.State.ToString()=="Open")
{
return true;
}
else
{
return false;
}
}

#endregion

#region"Open 用于打开数据库的方法"
/// <summary>
/// 用于打开Access数据库
/// </summary>
/// <returns>返回类型为 Bool 类型</returns>
private bool Open()
{
try
{
string dbm=HttpContext.Current.Server.MapPath( ConfigurationSettings.AppSettings["DataBaseName"]);
AccessConn.ConnectionString=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbm; //+ ";User ID=Admin;Database Password=";
AccessConn.Open();
return true;
}
catch
{
return false;
}
//Msgbox._Show("数据库已经打开!",FireTiger_ControlLibrary.FireTiger_Dialog.MsgBoxStyle.Information,false,this.Msgbox_Title );
//System.Windows.Forms.MessageBox.Show("数据库已经打开!");
}
#endregion

#region"RunSQLcmd 用于运行SQL语句的方法"
/// <summary>
/// 用于运行SQL语句的方法
/// 返回值为一个bool
/// </summary>
/// <param name="CmdStr">要运行的 SQL 语句</param>
/// <returns>返回值为 bool 类型 </returns>
public bool RunSQLcmd(string CmdStr)
{
try
{
if(Open())
{
OleDbCommand cmd1=new OleDbCommand("",AccessConn);
cmd1.CommandText=@CmdStr;
cmd1.ExecuteNonQuery() ;//运行SQL语句
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
finally
{
ConnClose();
}
}
#endregion

#region "RunSQLcmdDataSet 用于运行SQL语句的方法"
/// <summary>
/// 用于运行SQL语句的方法
/// 返回值为一个DataSet
/// 注意此方法与 RunSQLcmd 的不同
/// </summary>
/// <param name="CmdStr">要运行的 SQL 语句</param>
/// <returns>返回值为 DataSet 类型 </returns>
public DataSet RunSQLcmdDataSet( string CmdStr)
{
try
{
if(Open())
{
DataSet MyDataSet=new DataSet();
OleDbDataAdapter myaccessdp=new OleDbDataAdapter();
myaccessdp.SelectCommand=new OleDbCommand( @CmdStr,AccessConn);
myaccessdp.Fill(MyDataSet);
return MyDataSet;
}
return null;
}
catch(Exception ex)
{
// Msgbox._Show(Ex.Message,FireTiger_ControlLibrary.FireTiger_Dialog.MsgBoxStyle.Information,false,"在runsqlcmd的方法中出错");
//System.Windows.Forms.MessageBox.Show(Ex.Message,"在runsqlcmd的方法中出错");
return null;
}
finally
{
ConnClose();
}
}
#endregion

#region "用于把一个表的数据绑定到一个下拉列表框中去"
/// <summary>
/// 用于把一个表的数据绑定到一个下拉列表框中去
/// </summary>
/// <param name="YouDropDownList">要绑定的下拉列表框</param>
/// <param name="Sqlcmd">SQL命令</param>
/// <param name="ShowText">下列要显示的值</param>
/// <param name="ShowValue">下列实际的值</param>
public void bingDataToDropDownList(System.Web.UI.WebControls.DropDownList YouDropDownList, string Sqlcmd,string ShowText,string ShowValue)
{
DataTable temptable=new DataTable();
temptable=RunSQLcmdDataSet(Sqlcmd).Tables[0];
YouDropDownList.DataSource=temptable;
YouDropDownList.DataTextField=ShowText;
YouDropDownList.DataValueField=ShowValue;
YouDropDownList.DataBind();
}
#endregion

}
}

我给你写一个.
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.oledb" %>
<script language=vb runat=server>
sub page_load(sender as object , e as eventargs)
dim conn as new oledbconnection()
conn.connectionstring = "provider=microsoft.jet.oledb.4.0;data source = " & server.mappath("数据库名")
conn.open()
dim sql as string = "这是sql 语句"
dim com as new oledbcommand(sql,conn)
dim datareader as oledbdatareader = com.executereader()
while datareader.read()
response.write (datareader.item("想输出的字段名"))
end while
end sub
</script>

<%
' FileName="Connection_ado_conn_string.htm"
' Type="ADO"
' DesigntimeType="ADO"
' HTTP="true"
' Catalog=""
' Schema=""

'Dim MM_jd_STRING
'MM_jd_STRING = "Provider=Microsoft.Jet.OLEDB.4.0;数据库目录;"

dim conn
dim connstr
dim db
'Access连接方法如下
' db="数据库目录"
' Set conn = Server.CreateObject("ADODB.Connection")
'MM_jd_STRING="Provider=Microsoft.Jet.OLEDB.4.0;Data Source='数据库目录'"

'sqlserver数据库连接
Set conn = Server.CreateObject("ADODB.Connection")
MM_jd_STRING="driver={SQL SERVER};server=服务名;database=数据库名称;uid=sa(口令);pwd=(密码);"

'function CloseDatabase
' Conn.close
' Set conn = Nothing
'End Function
%>