-5

I am trying to use a button that will add to my two columns in my database. I have placeholder values in there currently but will eventually be using 2 pop ups to read in those values.

My question is: how do I get the connection string? I don't know what to put there or where to get that data.

    private void button_AddPartNumber_Click(object sender, EventArgs e)
    {
        string cmdString = "INSERT INTO Part_Numbers (Part_Number, Barcode_Number) VALUES (@val1, @val2)";
        string connectionString = "I DONT KNOW WHAT TO PUT HERE";

        using (SqlCommand connection = new SqlCommand(connectionString))
        {
            using (SqlCommand comm = new SqlCommand())
            {
                comm.Connection = connectionString;
                comm.CommandText = cmdString;
                comm.Parameters.AddWithValue("@val1", "L-0G004-0830-xx"); //placeholder value
                comm.Parameters.AddWithValue("@val2", "asdf1234"); // placehold value
            } 
        } 
    } // end button_AddPartNumber_Click()
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 5
    What connection string you use depends on the database type and other parameters. But a good resource for this is [connectionstrings.com](http://www.connectionstrings.com/). By the way, you should add the parameters before you open the connection (above the inner using statement) and you need to add the logic to actually execute the command. And you can use the overloaded constructor for `SqlCommand` that accepts the `cmdString`. – mason May 05 '15 at 15:44
  • I guess that would have been a good thing to put in there. Just a Local SQL Database. – Richard Sanford May 05 '15 at 15:45
  • 1
    a local sql database doesnt make much sense. Are you talking about MS's localdb? or just an instance of sql server, oracle, mysql? – crthompson May 05 '15 at 15:46
  • http://stackoverflow.com/questions/10479763/how-to-get-the-connection-string-from-a-database – Steve Hall May 05 '15 at 15:49
  • If you're using MSSQL the connection string is available in Management Studio, or in Server Explorer in the Visual Studio environment. – Drew Kennedy May 05 '15 at 15:49
  • I would not use AddWithValue. http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/ Also, your connection string be in your config file instead of hard coded in your app. – Sean Lange May 05 '15 at 15:54

1 Answers1

-1

Well, what database are you using? For example, SQLServer? Oracle? MySQL?

In SQLServer, at least, the connection string is defined in the web.config, or app.config, and has syntax similar to the following:

<appSettings> <add key="ConnectionStringName" value="AppName"/> </appSettings> <connectionStrings> <add name="AppName" connectionString="Data Source=DataSourceName; Initial Catalog=DataBaseName; user id=UserID; password=Password" providerName="System.Data.SqlClient"/> </connectionStrings>

Something like that. There's probably something I'm missing, but without testing it out in your code, I'm blanking on what that might be.

This how you extract the connection string from your code if the connection string is properly set in the web.config:

 var connectionString = ConfigurationManager.ConnectionStrings["AppName"].ConnectionString;
Dalorzo
  • 19,834
  • 7
  • 55
  • 102