2016-06-09 52 views
-2

我一直堅持這樣的:錯誤CS1061 t「對象」不包含「的ConnectionStrings」的定義,並沒有擴展方法「的ConnectionStrings」接受

using System; 
using System.Data; 
using System.Windows.Forms; 
using System.Data.SqlClient; 

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

     public object ConfigurationManager { get; private set; } 

     private void button3_Click(object sender, EventArgs e) 
     { 
      SqlConnection con = new SqlConnection(); 
      con.ConnectionString = ConfigurationManager.ConnectionStrings["constring"].ConnectionString; 

      string command = "insert into Supplier (SupplierID, SupplierName, Address, City, Country, ContactName, Position, Product) values (@SupplierID, @SupplierName, @Address, @City, @Country, @ContactName, @Position, @Product)"; 

      SqlCommand com = new SqlCommand(command, con); 
      com.CommandType = CommandType.Text; 

我得到的錯誤:

'object' does not contain a definition for 'ConnectionStrings' and no extension method 'ConnectionStrings' accepting a first argument of type 'object' could be found...

上線

ConfigurationManager.ConnectionStrings["constring"].ConnectionString; 
+2

錯誤解釋了一切。你有一個'object'類型的字段,並試圖調用一個根本不存在的方法。我懷疑你*不*完全想要這個字段,並想使用[System.Configuration.ConfigurationManager](https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager(v = vs.110).aspx),但忘了添加命名空間。當編譯器抱怨時,你選擇了創建該字段的第一個修復程序 –

+1

爲什麼在這個類中有一個名爲'ConfigurationManager'的屬性? –

回答

3

您需要添加一個引用System.Configuration.dll也INC lude using System.Configuration;在您的「使用」部分

+4

並刪除ConfigurationManager字段 –

+0

@PanagiotisKanavos你說得對, – Oscar

0

謝謝大家! 我刪除了對象,添加了使用System.Configuration,並且還添加了一個對System.Configuration的引用,形成了References選項卡。 它的工作!

相關問題