0

I have got an UPDATE query, what works well when I execute it in MS Managment Studio. But if I try execute this query from my c# app, it executes without any exceptions, but does not update the table. Connection string is right. This is the way I do it:

int contractId = 2
con.ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\tst.mdf;Integrated Security=True;Connect Timeout=30";
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "update аренды set datetime_возврата=GETDATE() where id_договора=@contractId";
cmd.Parameters.Add("@contract_id", SqlDbType.Int, 4).Value = contractId;         
cmd.ExecuteNonQuery();

What can be wrong?

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • 4
    You add a named parameter `@contract_id` while the command text has `@contractId`. Might that be the case? – Jaanus Varus Jun 07 '15 at 19:02
  • no, that is not the case. it does not works even if it is "update аренды set datetime_возврата=GETDATE() where id_договора=2" – Siberian Breaks Jun 07 '15 at 19:04
  • 2
    Are you sure the problem isn't that you don't commit it and rollback happens? Maybe profiler helps you to see what actually happens. – James Z Jun 07 '15 at 19:07
  • I would also recommend checking the query that arrives to the server using SQL Server Profiler and comparing that to the one you executed in Management Studio. – Jaanus Varus Jun 07 '15 at 19:09
  • Probably your answer is here http://stackoverflow.com/questions/17147249/why-saving-changes-to-a-database-fails/17147460#17147460 of course you need to fix the parameter name as explained in comments above – Steve Jun 07 '15 at 19:23

1 Answers1

1

If your c# code executes without any exceptions, it updates the database too, but please note you are used AttachDbFilename=|DataDirectory|\tst.mdf in your ConnectionString means the database that is updated is located in the sub-folder BIN\DEBUG folder of your project. If you want to see the updated data just attach the database located in the bin/debug folder in SSMS. Also as Steve mentioned in comments, for more details read this post.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109