0

the application i made can read data from .mdf file but can't write/update/delete data from .mdf file, i'm using c# + desktop, can anyone help me? I will give an example code:

SqlConnection cn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\HagiosWebFifo.mdf;Integrated Security=True");

private void SimpanAttribute()
        {
            string query = "INSERT INTO attribute" +
                "(head, detail, sub, ket) " +
                "VALUES (@head, @detail, @sub, @ket)";
            cn.Open();
            SqlCommand cmd = new SqlCommand(query, cn);
            cmd.Parameters.AddWithValue("@head", txtHead.Text );
            cmd.Parameters.AddWithValue("@detail", txtDet.Text );
            cmd.Parameters.AddWithValue("@sub", txtSub.Text );
            cmd.Parameters.AddWithValue("@ket", txtKet.Text );
            cmd.ExecuteNonQuery();
            cn.Close();

        }

thank you very much

  • 1
    Probably duplicate of this one https://stackoverflow.com/questions/17147249/why-dont-changes-to-database-save/17147460?duplicate=0#17147460 – Steve Nov 03 '21 at 14:17
  • Does this answer your question? [what's the issue with AttachDbFilename](https://stackoverflow.com/questions/11178720/whats-the-issue-with-attachdbfilename) You should just attach your DB properly – Charlieface Nov 03 '21 at 14:45
  • 3
    Side point: `cn` should *not* be cached in a field, you need to dispose it with `using`. Also [`AddWithValue` is Evil](https://www.dbdelta.com/addwithvalue-is-evil/), specify parameter types and lengths/scale explicitly. And you may find multiline strings useful for writing SQL queries – Charlieface Nov 03 '21 at 14:46

0 Answers0