pallab mondal
pallab mondal

Reputation: 3

Data are saved temporary to an access database. When I terminate the application the data are automatically deleted from database

VB.NET data are saving temporary in access database, when i terminate the application, data are automatically deleted from database. Here is the code:-

con1.Open()
Dim da As New OleDbDataAdapter
Dim cmd As New OleDb.OleDbCommand
cmd.Connection = con1
cmd.CommandText = "INSERT INTO Journal(UsedAccount,Effect,Purpose,EffectedAccount,Amount,Datex) VALUES( '" & Me.ComboBox1.SelectedItem & "','" & effect & "','" & Me.TextBox1.Text & "','" & Me.ComboBox3.SelectedItem & "','" & Me.TextBox2.Text & "','" & Me.DateTimePicker1.Value & "')"
cmd.ExecuteNonQuery()
con1.Close()

What is my code doing wrong? How can I prevent the data from being automatically deleted?

Upvotes: 0

Views: 3949

Answers (1)

Steve
Steve

Reputation: 216313

Probably you have your MDB (ACCDB) file included in your project files.
If you check the properties associated with this project file you will find one named

Copy to Output Directory set to Copy Always.

Also your connection string contains the shortcut

Data Source = "|DataDirectory|\yourdb.mdb"

If this scenario is right, then

  • you start your application and the MDB file is copied from the original location to the BIN\DEBUG directory.
  • You insert your data there without error.
  • you stop the debug session to fix errors or other problems
  • you restart your application and the fresh copy (but empty) of your db is copied again in the output directory.

Fix it setting the property to Copy Never or change your connection string to point to a fixed location.

As a side note: NEVER use string concatenation to build your sql commands. This will be a huge security risk (Sql Injection Attacks) and the source of numerous parsing problems (Dates formatting, string with single quotes, decimal separator for non integer numbers). Use ALWAYS parametrized queries.

Upvotes: 3

Related Questions