Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection String to access 2007

Im using this code to connect to access 2007 database :

   public void RetrieveData(){

        OleDbConnection conn=null;
        OleDbDataReader reader=null;
   string strConnection= @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\PC\\Documents\\School.accdb;Persist Security Info=False;"; 

        try
        {
            conn = new OleDbConnection(strConnection);
            conn.Open();
            OleDbCommand cmd = new OleDbCommand("select * from Class", conn);
            reader = cmd.ExecuteReader();
            DataList1.DataSource = reader;
            DataList1.DataBind();
        }
        catch (Exception e)
        {
            Response.Write(e.Message);
            Response.End();

        }
        finally
        {
            if (reader != null) reader.Close();
            if (conn != null) conn.Close();
        }

    }

but when i run it it jus give blank page.in debug mode I can see that the database property of conn is empty "" what would it be the problem?

like image 737
sally Avatar asked Jul 19 '26 21:07

sally


1 Answers

in the web.config

<connectionStrings>
    <!-- connection string declared for connecting the web application with MS Access 2007 -->
    <!-- connection string for MS Access 2007 accdb file -->
    <add name="AccessConnectionString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|db1.accdb;Persist Security Info=False;Jet OLEDB:Database Password=;" providerName="System.Data.OleDb" />
</connectionStrings>

and from the code behind use this

import namespaces
using system.data.oledb;

// to access the connection string stored in the web.config file.
OleDbConnection myDataConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString);

reply if it is useful or not

like image 98
Tan Avatar answered Jul 22 '26 10:07

Tan