views:

117

answers:

5

when I run this sql:

insert into table1(ID,Name) values ('10','Saeed');

it seems that the record has been inserted, and if I read the table using (select * from table1) it shows me the inserted record, but after closing the program, it disappears.

it's the code:

string constr="Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|" +   
    "\\Database1.mdf;Integrated Security=True;User Instance=True";

SqlConnection con = new SqlConnection(constr);
con.Open();

SqlCommand cmd = new SqlCommand(
    "insert into st (ID,Name) values ('10','saeed');", con);

cmd.ExecuteNonQuery();
cmd.Close();

I inserted some records in it manually, and when I read the database, the manually inserted records exist.

It is not a transaction problem wont be solved with a transaction!

+1  A: 

Guess: You are working with SQL Server Express and "Database1.mdf" within your project is configured (in properties window) for "Copy to Output Directory" value "Always"

Greets Flo

Florian Reischl
I have the same feeling. Let us know if this was the solution
Nealv
and what shuold i do?
saeed
Change the property to "If Newer" or "Don't copy". Whenever the file becomes copied, the data are lost.
Florian Reischl
+1  A: 

Try to specify: "Initial Catalog=InstanceDB;" as well to make sure it does not create a new db name when you restart the application.

Mharlin
A: 

error in your code itself its

con.Close();

not

cmd.Close();

there is no close method for SqlCommand

anishmarokey
it was a blunder.con.Cloce(); is correct.
saeed
A: 

I agree with pascal. Sounds like the transaction isn't being committed.

(Edited to provide clearer code block from my Comment below)

con.Open(); 
trans = con.BeginTransaction(); 

SqlCommand cmd = new SqlCommand( "insert into st (ID,Name) values ('10','saeed');", con);

cmd.ExecuteNonQuery(); 
tran.Commit(); 
Henry
How to committee the insertion?
saeed
Well, basically it's something like: con.Open(); trans = con.BeginTransaction(); SqlCommand cmd = new SqlCommand( "insert into st (ID,Name) values ('10','saeed');", con); cmd.ExecuteNonQuery(); tran.Commit();This is off the top of my head. You'll want to do some Googleing to confirm the details.
Henry
A: 

The issue sounds like you started a transaction and forgot to commit it. However, if you are using the exact code you posted this is not a transaction problem because you are not using one.

That makes me think there is something funky going on with your connection string.

For kicks and giggles trying changing your connection string to something like this

Server=myServerName\theInstanceName;Database=myDataBase;Trusted_Connection=True;
dretzlaff17
would you please write me the exact connection string, you can see mine in the question.
saeed
Initial Catalog=Database1;Data Source=<server name>;Integrated Security=TrueAlso try changing the User Instance=False in your current connection string. The records are probably inserting per that user instance only. That is probably why you do not see the new inserted records after you close the application.
dretzlaff17
Voted up for a good question.
dretzlaff17