I am trying to connect my MS Access Db with my asp.net website. When I run my code(on localhost only) it does not show any error or exception but it just wait for server to response. I am following code :
我正在尝试将我的MS Access Db与我的asp.net网站连接。当我运行我的代码时(仅在localhost上),它不显示任何错误或异常,但它只是等待服务器响应。我正在关注代码:
string connection = "Provider =Microsoft.ACE.OLEDB.12.0; datasource=I:/Adittya/test.accdb";
string query = "select * from test";
OleDbConnection con = new OleDbConnection(connection);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
OleDbDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
testGrid.DataSource = dt;
testGrid.DataBind();
One more thing my DB is in my pen-drive whose location is static.
还有一件事我的数据库在我的笔式驱动器中,其位置是静态的。
Do I have problem with my code or we cannot connect to DB present in pen-drive.
我的代码有问题,或者我们无法连接到笔式驱动器中的数据库。
3 个解决方案
#1
1
try your data source as
尝试将您的数据源作为
datasource=I:\\Adittya\\test.accdb
#2
4
There is a problem in your code, You have not set the connection to the command object and also not open the connection.Below code is working for me. Try this
您的代码中存在问题,您没有设置与命令对象的连接,也没有打开连接.Below代码对我有用。试试这个
string connection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\Test1.accdb";
string query = "select * from [Table]";
OleDbConnection con = new OleDbConnection(connection);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
OleDbDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
con.Close();
testGrid.DataSource = dt;
testGrid.DataBind();
or Try this solution Using SqlDataSource
或尝试使用SqlDataSource的此解决方案
Example:I have created a test Acess database and I used Sql datasource it is working fine. Try this solution
示例:我已经创建了一个测试Acess数据库,并且我使用了Sql数据源,它工作正常。尝试此解决方案
<connectionStrings>
<add name="ConnectionString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Test1.accdb"
providerName="System.Data.OleDb" />
</connectionStrings>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [Table]"></asp:SqlDataSource>
<asp:GridView ID="GridView1" DataSourceID="SqlDataSource1" runat="server"></asp:GridView>
It is working fine for me.
它对我来说很好。
#3
0
use this
"Provider=Microsoft.Jet.OLEDB.4.0; " +
"Data Source=" + Server.MapPath("Database/TestDB.mdb"))
#1
1
try your data source as
尝试将您的数据源作为
datasource=I:\\Adittya\\test.accdb
#2
4
There is a problem in your code, You have not set the connection to the command object and also not open the connection.Below code is working for me. Try this
您的代码中存在问题,您没有设置与命令对象的连接,也没有打开连接.Below代码对我有用。试试这个
string connection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\Test1.accdb";
string query = "select * from [Table]";
OleDbConnection con = new OleDbConnection(connection);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
OleDbDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
con.Close();
testGrid.DataSource = dt;
testGrid.DataBind();
or Try this solution Using SqlDataSource
或尝试使用SqlDataSource的此解决方案
Example:I have created a test Acess database and I used Sql datasource it is working fine. Try this solution
示例:我已经创建了一个测试Acess数据库,并且我使用了Sql数据源,它工作正常。尝试此解决方案
<connectionStrings>
<add name="ConnectionString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Test1.accdb"
providerName="System.Data.OleDb" />
</connectionStrings>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [Table]"></asp:SqlDataSource>
<asp:GridView ID="GridView1" DataSourceID="SqlDataSource1" runat="server"></asp:GridView>
It is working fine for me.
它对我来说很好。
#3
0
use this
"Provider=Microsoft.Jet.OLEDB.4.0; " +
"Data Source=" + Server.MapPath("Database/TestDB.mdb"))