簡體   English   中英

在另一台計算機上運行包含.sdf文件的項目時出錯

[英]Error in running the project containing .sdf file on another computer

我在Visual Studio 2010中創建了包含一些數據庫連接的ac#項目。 問題是當我運行正確運行的項目時,我復制exe文件和DLL文件以及bin / debug文件夾中的所有文件並放在另一個位置。 並刪除作為c#項目的項目並嘗試運行exe文件,表單正確打開但是打開數據庫表單時出錯了

該路徑無效。 檢查數據庫的目錄。
[Path = E:\\ WindowsFormsApplication1 \\ WindowsFormsApplication1 \\ Database1.sdf]

它仍然需要我在c#代碼中編寫的目錄。

在第二種形式錯誤的上述錯誤我編寫了這段代碼文件路徑是連接字符串是相同的,這是錯誤的。 如果它從同一個文件夾中獲取路徑然后如何在另一台計算機上運行該項目,為什么會發生這種情況呢?

     connectionString = @"E:\WindowsFormsApplication1\
    WindowsFormsApplication1\Database1.sdf";
                sqlConnection = new SqlCeConnection(connectionString);
                selectQueryString = "SELECT * FROM table2";
                sqlConnection.Open();
                sqlDataAdapter = new SqlCeDataAdapter(selectQueryString,
 sqlConnection);
                sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
                dataTable = new DataTable();
                sqlDataAdapter.Fill(dataTable);
                bindingSource = new BindingSource();
                bindingSource.DataSource = dataTable;
                dataGridView1.DataSource = bindingSource;

第1步 :如果您的項目尚未包含app.config - 請添加一個:

  • Solution Explorer ,轉到項目
  • 然后從上下文菜單中選擇Add > New Item
  • 轉到General模板並選擇Application Configuration File

第2步 :將您的連接字符串(包含.sdf文件的路徑)放入該配置文件中:

  • 打開從步驟1獲得的新app.config文件
  • <configuration> ..... </configuration>標記內添加這些行:

     <connectionStrings> <add name="SomeNameHere" connectionString="E:\\WindowsFormsApplication1\\WindowsFormsApplication1\\Database1.sdf"/> </connectionStrings> 

第3步:在您的程序中,從該配置中讀取連接字符串:

connectionString = ConfigurationManager.ConnectionStrings["SomeNameHere"].ConnectionString;

第4步:將應用程序復制到新計算機時,根據需要在配置文件中調整該路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM