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