连接到数据库(MS接入)在asp.net C#(托管网站) [英] Connecting to database(MS Access) in asp.net c#(hosting website)

查看:121
本文介绍了连接到数据库(MS接入)在asp.net C#(托管网站)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个托管网站,我需要用密码和登录信息添加数据库。 (MS Access数据库),我尝试了很多,但不能用它连接(本机工作原理上)。我试图改变连接字符串,但它仍然无法正常工作。数据库在文件夹中的App_Data。下面是我输入的Login.aspx 页:

I have a hosted website, and I need to add a database with password and log-in information. (MS Access DB), I tried a lot but can't connect with it (on local machine it works). I tried to change connection string but it still doesn't work. Database is in folder App_Data. Here's what I type in Login.aspx page:

OleDbConnection con = new OleDbConnection();
con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
   + Server.MapPath("~\\App_Data\\WebSiteDatabase.accdb");
con.Open();

这是行不通的。我需要做什么改变?我已经把我的网站上somee.com

This does not work. What do I need to change? I've put my web site on somee.com

推荐答案

你检查SOMEE​​帮助页面连接到Access数据库?

Did you check the help page on SOMEE for connecting to an access database?

引用自Somee.com帮助:

QUOTE FROM Somee.com help:

连接到MS Access数据库全光照DSN-less连接结果
  只有多卡
  提供了对Access数据库DSN-less连接,因为他们是
  快得多并且没有可能的名称冲突。大部分的
  问题是在选择正确的连接字符串。下面是一个例子
  测试连接字符串到MS Access数据库:结果
          我们假设您的数据库位于数据库子文件夹,它的名字叫TestDB.mdb。结果
  你将不得不使用
  使用Server.Mappath(数据库\\ TestDB.mdb)为了获得物理
  数据库的位置。

Connect to MS access database usin DSN-less connection
Doka only provides DSN-less connection to the Access databases, because they are much faster and there is no possible names conflict. Most of the problems are in choosing right connection string. Here is an example of tested connection string to MS Access database:
We suppose that your database resides in "Database" subfolder and it name is "TestDB.mdb".
You’ll have to use Server.MapPath("Database\TestDB.mdb") in order to get physical location of database.

所以,连接字符串将是:

So connection string would be:

"PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" & Server.MapPath("Database\TestDB.mdb") 

和利用的方式:

OleDbConnection conn = null;
OleDbDataReader reader = null;
try
{
    conn = new OleDbConnection(
        "Provider=Microsoft.Jet.OLEDB.4.0; " + 
        "Data Source=" + Server.MapPath("Database/TestDB.mdb"));
    conn.Open();

    OleDbCommand cmd = 
        new OleDbCommand("Select * FROM Table1", conn);
    reader = cmd.ExecuteReader();

    datagrid.DataSource = reader;
    datagrid.DataBind();
}
    //        catch (Exception e)
    //        {
    //            Response.Write(e.Message);
    //            Response.End();
    //        }
finally
{
    if (reader != null)  reader.Close();
    if (conn != null)  conn.Close();
}

这篇关于连接到数据库(MS接入)在asp.net C#(托管网站)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆