2.0中 Access数据库 连接

来源:互联网 发布:学好c语言能做什么 编辑:程序博客网 时间:2024/05/22 14:02

方案<一>

配置文件中:

<connectionStrings>

    <add name="StrData" connectionString="App_Data/movie.mdb"/>
    </connectionStrings>

代码调用:

 string ACConnectionString = "Provider = Microsoft.Jet.OleDb.4.0; Data Source = " + HttpContext.Current.Server.MapPath(ConfigurationManager.ConnectionStrings["StrData"].ConnectionString);
        OleDbConnection conn = new OleDbConnection(ACConnectionString);
        conn.Open();
        string sql = "select * from USERS where uname='" + TxtName.Text + "' and pass='" + TxtPwd.Text + "' ";
        OleDbCommand cmd = new OleDbCommand(sql, conn);
        OleDbDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            Response.Redirect("reg.aspx");
        }
        else
        {
            Response.Write("Roung");
        }
        conn.Close(); 

方案<二>

配置文件中:

<connectionStrings>
    <add name="StrData" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source =C:/Documents and Settings/SupperSdr/My Documents/Visual Studio 2005/WebSites/流媒体/App_Data/movie.mdb" providerName="System.Data.OleDb"/>
   
  </connectionStrings>

代码调用:

 OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["StrData"].ConnectionString );
        conn.Open();
        string sql = "select * from USERS where uname='" + TxtName.Text + "' and pass='"+ TxtPwd.Text  +"' ";
        OleDbCommand cmd = new OleDbCommand(sql, conn);
        OleDbDataReader dr = cmd.ExecuteReader ();
        if (dr.Read())
        {
            Response.Redirect("reg.aspx");
        }
        else
        {
            Response.Write("Roung");
        }
        conn.Close();