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 format(@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:You can then use the following code to access the connection string:
..instead of what you have: