Search code examples
c#sqlinsertsql-server-2008-express

SQL Server Express inserts doesn't work anymore


In my C# application, I do the following:

using (SqlConnection connection = new SqlConnection(connectionstr))
{
    connection.Open();
    SqlCommand com1 = new SqlCommand("insert into Anfangstierbestand(bestand, jahr, loginid) VALUES (" + bestand + ", " + jahr + ", " + loginid + ");", connection);
    SqlCommand com2 = new SqlCommand("select * from Anfangstierbestand;", connection);

    com1.Connection = connection;
    int ferksum = 0;

    com1.ExecuteNonQuery();
    connection.Close();

    connection.Open();
    SqlDataReader read = com2.ExecuteReader();

    while (read.Read())
    {
        ferksum += Convert.ToInt32(read[2]);
    }

    // MessageBox.Show("Fehler beim Erstellen des Tierbestandes", "Fehler"); }
    MessageBox.Show(ferksum.ToString());
}

It's a simple insert to a database. I added com2 to check, if the insert works.

Unfortunately, the the value of com2 tells me, that it works, but when I go to the Server Explorer and press Execute SQL, there are no new values.

I don´t know the reason. The code above is just an example, since a few days, no insert works anymore(before, the same code works).

Does anybody know what the reason can be?

EDIT:

C#:

string connectionstr = "Data Source=.\\SQLEXPRESS;" + "AttachDbFilename=|DataDirectory|\\Datenbank\\FarmersCalc.mdf;" + "Integrated Security=True;" + "User Instance=true;";

EDIT2:

Server Explorer:

Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Users\user\Desktop\Farmer´s Calc\Programmierung\WPF\Finanz_WPF\Finanz_WPF\Datenbank\FarmersCalc.mdf";Integrated Security=True;User Instance=True

EDIT3:

Here are the columns:

id int,
jahr int,
anzahl int,
loginid int

EDIT4:

Is it possible that it´s a problem that I opened my project with expression blend? Normally, I work with VS 2010...

EDIT5:

Even because I can not answer my question(<100 reputations) I write it here:

Shame on me. Some of you were right, it was the wrong database-file. But I´m still wondering, why this happened, because I did not change anything since a few days and before this, it worked!

Thanks to all for helping!


Solution

  • You're using user instances, why? Isn't it possible that the instance you're connecting to in Server Explorer is not the same as the instance where your app is inserting data? What happens when you select data from within the app, does it reflect your insert(s)? It seems to me based on your connection strings that these are two completely different MDF files - they have the same name but they are in different locations.