An object reference is required for the non-static field, method, or property 'extractDB.Properties.Settings.MasterConnectionString.get'
namespace extractDB
{
class ConsoleApp2
{
static void Main(string[] args)
{
string dog ="fancy";
string shep = "shepherd";
Add _add = new Add();
_add.Ex(45, dog, shep);
}
}
class Add
{
public void Ex(int weight, string name, string breed)
{
using (SqlConnection con = new SqlConnection(extractDB.Properties.Settings.MasterConnectionString))
{
con.Open();
try
{
using (SqlCommand command = new SqlCommand("INSERT INTO fo开发者_Python百科rmat(@Weight,@Name,@breed)", con))
{
command.Parameters.Add(new SqlParameter("Weight", weight));
command.Parameters.Add(new SqlParameter("Name", name));
command.Parameters.Add(new SqlParameter("Breed", breed));
command.ExecuteNonQuery();
}
}
catch
{
Console.WriteLine("count not inserted");
}
}
}
}
}
I'm getting an error message because of "extractDB.Properties.Settings.MasterConnectionString."
I checked in the app.config and settings.settings files and that's the string app.config gives me. I even tried adding in
static void Ex(int weight, string name, string breed)
but it doesn't get rid of the error message.
So yeah kind of lost on this one.
The conventional way of using configuration files to store Database Connection Strings would be to use the <connectionStrings>
element at the root of your web or app.config file:
<configuration>
<connectionStrings>
<add
name="NorthwindConnectionString"
connectionString="Data Source=serverName;Initial
Catalog=Northwind;Persist Security Info=True;User
ID=userName;Password=password"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
<configuration>
You can then use the following code to access the connection string:
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
..instead of what you have:
extractDB.Properties.Settings.MasterConnectionString
You should be storing this in the app.config, but looking at your code it appears that you might be missing:
Try this:
string conn = extractDB.Properties.Settings.Default.MasterConnectionString;
You are getting that error exactly because of what the compiler says: Either your extracDB
object is not instantiated, or your extractDB.Properties.Settings.MasterConnectionString
is not static. Double check your code for extractDB.Properties.Settings.MasterConnectionString
精彩评论