-2

下面的代碼用於在運行時更改App.config中的連接字符串,我發現它爲here,但此代碼在Visual Studio 2010和SQL Server 2008上不適用於我,我可以不打開連接到Northwind數據庫。在運行時更改App.config中的連接字符串

using System; 
using System.Configuration; 
using System.Data; 
using System.Data.SqlClient; 
using System.Text; 
using System.Windows.Forms; 
using System.Xml; 

namespace MyNameSpace 
{ 
    public partial class FrmConnectionTest : Form 
    { 
     public FrmConnectionTest() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       //Constructing connection string from the inputs 
       StringBuilder Con = new StringBuilder("Data Source="); 
       Con.Append(TxtServer.Text); 
       Con.Append(";Initial Catalog="); 
       Con.Append(TxtDatabase.Text); 
       Con.Append(";Integrated Security=SSPI;"); 
       string strCon = Con.ToString(); 
       updateConfigFile(strCon); 
       //Create new sql connection 
       SqlConnection Db = new SqlConnection(); 
       //to refresh connection string each time else it will use    previous connection string 
       ConfigurationManager.RefreshSection("connectionStrings"); 
       Db.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ToString(); 
       //To check new connection string is working or not 
       SqlDataAdapter da = new SqlDataAdapter("select * from employee"); 
       DataTable dt = new DataTable(); 
       da.Fill(dt); 
       CmbTestValue.DataSource = dt; 
       CmbTestValue.DisplayMember = "EmployeeID"; 
      } 
      catch (Exception E) 
      { 
       MessageBox.Show(ConfigurationManager.ConnectionStrings["con"].ToString() + ".This is invalid connection", "Incorrect server/Database"); 
      } 
     } 
     public void updateConfigFile(string con) 
     { 
      //updating config file 
      XmlDocument XmlDoc = new XmlDocument(); 
      //Loading the Config file 
      XmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); 
      foreach (XmlElement xElement in XmlDoc.DocumentElement) 
      { 
       if (xElement.Name == "connectionStrings") 
       { 
        //setting the coonection string 
        xElement.FirstChild.Attributes[2].Value = con; 
       } 
      } 
      //writing the connection string in config file 
      XmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); 
     } 
    } 
} 

使用Visual Studio 2010和SQL Server2008中,我得到了下一行2個錯誤:

  SqlDataAdapter da = new SqlDataAdapter("select * from employee"); 
  • 錯誤1「System.Data.SqlClient.SqlDataAdapter的最佳重載的方法匹配.SqlDataAdapter(System.Data.SqlClient.SqlCommand)'有一些無效參數

  • 錯誤2參數1:無法從'字符串'轉換爲'System.Data.SqlClient.SqlCommand'

有沒有解決這個問題的方法?謝謝。

回答

1

錯誤告訴你,你正在向你的SqlDataAdapter傳遞不正確的參數。我認爲正確的調用是:

SqlDataAdapter da = new SqlDataAdapter("select * from employee", Db); 

編輯

它看起來像你從你的程序中創建您的連接字符串,將其保存到您的配置文件,然後讀出來的我們配置文件,然後再創建SqlDataAdapter。所以,當你調試這條線:

Db.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ToString(); 

仔細檢查Db.ConnectionString實際上包含一個連接字符串。

要做的另一件事是打開你的SQL Server Management Studio並確認你可以從那裏連接到Northwind數據庫。包括/或者在Visual Studio中,打開你的「服務器資源管理器」窗口,並確認你可以創建一個數據連接到Northwind,方法是單擊添加連接,然後將連接屬性窗口設置到你的服務器,然後放下組合框,看它是否填充數據庫:

enter image description here

+0

SqlDataAdapter da = new SqlDataAdapter(「select * from employee」,Db)是正確的,但是當使用「。\ SQLEXPRESS」作爲服務器名和「Northwind」作爲數據庫名,我陷入catch(異常E)與消息:MessageBox.Show(ConfigurationManager.ConnectionStrings [「con」]。ToString()+「.This is invalid connection」 – 2012-04-18 23:53:59

+0

@AlphaBird,你提到你嘗試將連接設置更改爲:「Data Source = localhost; Initial Catalog = Northwind; Integrated Security = SSPI;」 – 2012-04-19 00:25:11

+0

連接也失敗,並且Data Source = localhost,我在「Data Source = 「並且也失敗了,我驗證了我的App.config的內容:<?xml version =」1.0「encoding =」utf-8「?> 2012-04-19 09:41:41

0

看看the available constructors of the SqlDataAdapter class

沒有隻接受SQL字符串的構造函數重載。
您需要使用其他重載之一。例如,there is one that needs an SQL String and a SqlConnection object。 要使用它,改變你的代碼是這樣的:

SqlDataAdapter da = new SqlDataAdapter("select * from employee", Db); 

編輯:在他的評​​論中已經提到

由於BradRem,嘗試不同的連接字符串。
如果他的例子不適合你,你可以在http://connectionstrings.com/sql-server-2008找到更多可能的例子。

你的服務器上真的有一個名爲Northwind的數據庫嗎?
當前計算機上的Windows用戶是否具有訪問數據庫的服務器權限? (這是什麼Integrated Security=SSPI意味着 - 您當前的Windows用戶用於訪問數據庫!)

+0

我用「SqlDataAdapter da = new SqlDataAdapter(」select * from employee「,Db);」它工作成功,但返回異常:「數據源=。\ SQLEXPRESS;初始目錄= Northwind;集成安全性= SSPI;。這是無效的連接」 – 2012-04-19 00:03:30