0

I want to create a program for my school, handling marks and certificates. To save the data I have to use a "local" network shared database-file, because we are using Citrix and there is no possibility to setup an seperate SQL-Server.

I tried it with localdb v11 with the following code:

        string connectionString = @"Data Source=(LocalDB)\v11.0; AttachDbFilename=D:\_prog\TestNoten\TestNoten\bin\Debug\Database1.mdf; Integrated Security=True;Connect Timeout=3;";

        SqlConnection connection = new SqlConnection(connectionString);

        string sql = "INSERT INTO test(name) VALUES('lal')";
            SqlCommand cmd = new SqlCommand(sql, connection);
            connection.Open();
            cmd.ExecuteNonQuery();
            connection.Close();

        DataTable table = new DataTable();
        SqlDataAdapter adp = new SqlDataAdapter("Select * from test", connection);
            connection.Open();
            adp.Fill(table);
            MessageBox.Show(table.Rows[0][1].ToString());

The MessageBox says "lal", but when I restart the program with "lala" instead of "lal", it will show "lala" and not the expected "lal". So when closing the program, the database won't be saved correctly. I also opened the file over VSs Data Connections, and the testing table is empty.

Is there something I missed?

gu471
  • 173
  • 12
  • Did you empty the table in your database? If not: you are always displaying the first row in your table. On the first execute you inserted `lal`, on the second `lala`, but in both cases the first row returned contains `lal`. – Mark Rotteveel Jun 17 '14 at 18:32
  • After some runs, there should be more than one entry, but running `MessageBox.Show(table.Rows[1][1].ToString());` triggers an `IndexOutOfRangeException` – gu471 Jun 17 '14 at 18:39

0 Answers0