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 开发者_JAVA百科page.in debug mode I can see that the database property of conn is empty "" what would it be the problem?
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
I can see that the database property of conn is empty "" what would it be the problem
No that's expected. If you used the OleDbConnection on an RDMS that had multiple database for one server then it would have a value.
For example SQL Server, Sybase, MySQL basically any thing that has a connection that looks like
Provider = ; Server = ; Database = ;
Well I am not very sure if this would work.......but u may try this. Check the value of AutoEventWireUp in the .aspx page. If it is set to "false"....set it to "true".
精彩评论