Database only allow me to view but cannot write, what is the problem?

jeff lee khong wai

New member
Joined
Jan 18, 2007
Messages
2
Location
Malaysia
Programming Experience
3-5
I create a new database by Visual Studio. Add table and want to use VB.NET to add data to the table.

Here is my code:

Dim sqlConnection1 As New System.Data.SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MAAMedicareDB.mdf;Integrated Security=True;User Instance=True")
Dim cmd As New System.Data.SqlClient.SqlCommand
cmd.CommandType = System.Data.CommandType.Text
cmd.CommandText =
"INSERT Centers (centerName, centerADD) VALUES ('USA center', 'Your house')"
cmd.Connection = sqlConnection1
sqlConnection1.Open()
cmd.ExecuteNonQuery()
sqlConnection1.Close()
MsgBox(
"Run", MsgBoxStyle.Information, "run")

My program just run the code without error. But when i open my database, nothing change. That's mean even the code run but no data add in my database.

I sure the connection is ok because i able to retrieve data.
Is it anything i need to set? or I am not permit to write to the database?
Any setting need to do?

Please help me. Urgent...

Thank you
 
I have noticed when following web examples to create a database that added items where lost when you reopened your program in debug mode.

I have change the Database property "Copy into output folder" (second property in the advanced section) to "Copy if more recent" instead of "Always copy" and it worked.

I am using a French Version of Visual Studio, therefore my translation of terms might not be exactly what you have in English.
 
youre using a file based database. every time you start your app, the original "clean room" development version of the database is copied out, over the top of the database you changed. if youre using a query tool to look at the original database, then that wont have changed because it never does

an analogy could be:

you have a text document on a floppy disk
you copy it to c:temp and then edit the copy
you look at the text file on floppy disk and say "oh.. it hasnt changed!"
you then copy the original over the edited copy again, and then look at either and say "oh, neither have changed"


they did.. you just copied over the changes

it doesnt happen in a production system


Bob's solution works fine - click the db file in solution explorer, and change the Copy property to "copy if newer"
 
Back
Top