2

I am trying to write an application in Visual Studio C# to write to a service based database. The issue I am having is that I am not getting any errors, however nothing is saving to the database after relaunching the application...

I know it is writing because it will not allow duplicate primary keys. It's almost as if it's a temporary database... If this is the case how do I make it a persistent service based database?

Here is my code:

private void button1_Click(object sender, EventArgs e)
{
    try {
        // Connection to DB
        SqlConnection con = new SqlConnection();
        con.ConnectionString = (@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MTR_Database.mdf;Integrated Security=True");

        // Insert Query
        string insertquery = "INSERT INTO dbo.Job01 ([Item #], [Manufacturer], [Mill Location], [Product Description], [Weld Seam Type], [Outer Dimension], [Wall Thickness], [Coating], [Grade], [Heat], [ANSI/ASME], [Purchase Order], [Standard]) VALUES(@ItemNum, @Manufacturer, @MillLocation, @ProductDescription, @WeldSeamType, @OuterDimension, @WallThickness, @Coating, @Grade, @Heat, @ANSIASME, @PurchaseOrder, @Standard)";

        SqlCommand cmd = new SqlCommand(insertquery, con);

        // open connection
        con.Open();

        // Parameters
        cmd.Parameters.AddWithValue("@ItemNum", item__TextBox.Text);
        cmd.Parameters.AddWithValue("@Manufacturer", manufacturerTextBox.Text);
        cmd.Parameters.AddWithValue("@MillLocation", mill_LocationTextBox.Text);
        cmd.Parameters.AddWithValue("@ProductDescription", product_DescriptionTextBox.Text);
        cmd.Parameters.AddWithValue("@WeldSeamType", weld_Seam_TypeTextBox.Text);
        cmd.Parameters.AddWithValue("@OuterDimension", outer_DimensionTextBox.Text);
        cmd.Parameters.AddWithValue("@WallThickness", wall_ThicknessTextBox.Text);
        cmd.Parameters.AddWithValue("@Coating", coatingTextBox.Text);
        cmd.Parameters.AddWithValue("@Grade", gradeTextBox.Text);
        cmd.Parameters.AddWithValue("@Heat", heatTextBox.Text);
        cmd.Parameters.AddWithValue("@ANSIASME", aNSI_ASMETextBox.Text);
        cmd.Parameters.AddWithValue("@PurchaseOrder", purchase_OrderTextBox.Text);
        cmd.Parameters.AddWithValue("@Standard", standardTextBox.Text);

        // Execute
        cmd.ExecuteNonQuery();

        // close connection
        con.Close();
    }
    catch (Exception ex)
    {
        // catch error
        MessageBox.Show(ex.Message);
    }
}

I really can't put my finger on what is going on.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mokey
  • 215
  • 1
  • 15
  • I guess more so, I want it to act as SQLite... Without an active server and within the application. Is this possible with SQL Express embedded in Visual Studio? – Mokey Jan 16 '17 at 17:55
  • Read this: http://stackoverflow.com/questions/6221951/sqlexception-catch-and-handling on exception handling with SQL-Server. Also use a direct file path to the MDF and see what happens – Ibrahim Malluf Jan 16 '17 at 18:05

2 Answers2

1

The whole AttachDbFileName= approach is flawed - at best! When running your app in Visual Studio, it will be copying around the .mdf file (from your App_Data directory to the output directory - typically .\bin\debug - where you app runs) and most likely, your INSERT works just fine - but you're just looking at the wrong .mdf file in the end!

If you want to stick with this approach, then try putting a breakpoint on the myConnection.Close() call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there.

The real solution in my opinion would be to

  1. install SQL Server Express (and you've already done that anyway)

  2. install SQL Server Management Studio Express

  3. create your database in SSMS Express, give it a logical name (e.g. MTR_Database)

  4. connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. In that case, your connection string would be something like:

    Data Source=.\\SQLEXPRESS;Database=MTR_Database;Integrated Security=True
    

    and everything else is exactly the same as before...

Also see Aaron Bertrand's excellent blog post Bad habits to kick: using AttachDbFileName for more background info.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Will do @marc_s Thanks for the info... I will try this out. I've been working in MySql quite a bit, but trying to switch to SQL for work... it's been a horrible experience so far. Besides the fact, Again I will work on this. If it works I'll Upvote the answer :) I may finish it tomorrow since It's almost time to gett off work :( – Mokey Jan 16 '17 at 19:54
  • 1
    You were exactly right... I was looking at the wrong DB file. Everything was inserting properly. I guess I have to learn how to make a new server and give it a database name. I can then just pretty much treat it like mySQL. I'll probably just run it off a server rather than local. – Mokey Jan 16 '17 at 20:46
0

I've had that problem before and just solved it.

Don't use |DataDirectory|\MTR_Database.mdf, use the full path.

anothernode
  • 5,100
  • 13
  • 43
  • 62