📄 myconnoledb.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Configuration;
namespace Example_4_3
{
/// <summary>
/// Summary description for ConnOleDB.
/// </summary>
public class MyConnOleDB : System.Web.UI.Page
{
private readonly string OLECONNECTIONSTRING = ConfigurationSettings.AppSettings["OLECONNECTIONSTRING"].ToString();
private void Page_Load(object sender, System.EventArgs e)
{
String OleDbConnectionString = OLECONNECTIONSTRING + Server.MapPath("accessDB.mdb");
OleDbConnection oleDbConnection = new OleDbConnection(OleDbConnectionString);
try
{
///打开OLEDB数据库的链接
oleDbConnection.Open();
Response.Write("连接Access数据源成功!!!");
//显示链接成功信息
Response.Write("<br>OLEDB数据库连接的链接字符串:" + oleDbConnection.ConnectionString);
Response.Write("<br>OLEDB数据库连接的数据库名称:" + oleDbConnection.Database);
Response.Write("<br>OLEDB数据库连接的数据源:" + oleDbConnection.DataSource);
Response.Write("<br>OLEDB数据库连接的数据库驱动:" + oleDbConnection.Provider);
Response.Write("<br>OLEDB数据库连接的服务器版本:" + oleDbConnection.ServerVersion);
Response.Write("<br>OLEDB数据库连接的打开状态:" + oleDbConnection.State.ToString());
///执行数据库的操作
///如选择、插入、删除和更新等
///关闭OLEDB数据库的链接
oleDbConnection.Close();
Response.Write("<br><br>OLEDB数据库连接的关闭状态:" + oleDbConnection.State.ToString());
}
catch(Exception ex)
{
//抛出数据库连接异常
Response.Write(ex.Message);
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -