连接access数据库小试

来源:互联网 发布:linux vi指令编辑 编辑:程序博客网 时间:2024/06/08 06:57

建立一个webform文件connaccess,添加一个label1控件.

双击进入 connaccess.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;

namespace DotNetTest.database
{
 /// <summary>
 /// accessconn 的摘要说明。
 /// </summary>
 public class accessconn : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Label Label1;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   string sqlStr="Provider=Microsoft.Jet.OleDb.4.0;Data source="+Server.MapPath(@"data/dotnettest.mdb");//@的作用是防止后面的字符串中的/转义
   OleDbConnection conn=new OleDbConnection(sqlStr);
   conn.Open();
   if(conn.State==ConnectionState.Open)Label1.Text+="Databased has been opened <br>";
   Label1.Text+="Database:"+conn.Database+"<br>"+
    "Datasource:"+conn.DataSource+"<br>"+
    "State:"+conn.State+"<br>";
   conn.Close();
   if(conn.State==ConnectionState.Closed)Label1.Text+="Databased has been closed <br>";
  }

  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion
 }
}

原创粉丝点击