Reputation: 3
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
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
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