繁体   English   中英

如何使oledb程序在另一台PC上运行

[英]how do I make my oledb program work on a different pc

嘿,我是编程新手,从来没有做过需要在另一台PC上运行的实际程序。 该程序与数据库连接。 当我在另一台PC上时,我更改了program.exe.config文件,以便我可以应用数据库的正确位置,但是它仍然不起作用。 这是我的代码,也许这里有问题。 app.config:

<connectionStrings>
    <add name="Program.Properties.Settings.InventoryDBConnectionString"
        connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\School\Stage\Program testen\Program\bin\Debug\InventoryDB.accdb"
        providerName="System.Data.OleDb" />
</connectionStrings>

在标准代码中:

    OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\School\Stage\Program testen\Program\bin\Debug\InventoryDB.accdb");

首先是要在应用程序配置文件中指定一个连接字符串:

<connectionStrings>
   <add name="Program.Properties.Settings.InventoryDBConnectionString"
        connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\School\Stage\Program testen\Program\bin\Debug\InventoryDB.accdb"
        providerName="System.Data.OleDb" />
</connectionStrings>

您没有使用Program.exe.config中的连接字符串,而是要复制并粘贴该连接字符串。

如果您稍微更改代码,也许您会明白我的意思:

//Get the connection string to use
String connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\School\Stage\Program testen\Program\bin\Debug\InventoryDB.accdb";

//Open a connection to the database
OleDbConnection con = new OleDbConnection(connectionString);

您应该做的是从应用程序的配置中提取连接字符串:

//Get our connection string setting, and the connectionString it contains
ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings["Program.Properties.Settings.InventoryDBConnectionString"];
String connectionString = cs.ConnectionString;

//Open a connection to the database
OleDbConnection con = new OleDbConnection(connectionString);

您还可以进行另一项更改。 您的连接字符串条目本身指定您要使用的提供程序:

providerName="System.Data.OleDb"

但是随后您可以继续使用该提供程序:

OleDbConnection con = new OleDbConnection(...);

如果您更改了连接字符串以使用其他提供程序:

providerName="System.Data.SqlClient"

您的代码仍将使用OleDbConnection ,而不是应用程序配置中提供的提供程序。

幸运的是,.NET框架具有一个方便的小助手功能,可以在其中您创建正确的提供程序:

public static DbConnection GetConnection()
{
    //Get the connection string info from our application config
    ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings["Program.Properties.Settings.InventoryDBConnectionString"];

    if (cs == null)
        throw new Exception("Could not find connection string settings");

    //Get the factory for the given provider (e.g. "System.Data.OleDbClient")
    DbProviderFactory factory = DbProviderFactories.GetFactory(cs.ProviderName);

    if (factory == null)
        throw new Exception("Could not obtain factory for provider \"" + cs.ProviderName + "\"");

    //Have the factory give us the right connection object
    DbConnection conn = factory.CreateConnection();

    if (conn == null)
        throw new Exception("Could not obtain connection from factory");

    //Knowing the connection string, open the connection
    conn.ConnectionString = cs.ConnectionString;
    conn.Open();

    return conn;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM