0

I use SqlConnection and SqlCommand classes in my project with Dapper ORM but I've got a strange problem. When I use SqlCommand for inserting a row in a db table it always work correct and when I select updated data form tables everything is fine but after close the application changes I made isn't commited like data have been saved in some cache. For example I created a simple table with only two columns (UserId (PK), and UserName (Unique)) and even in that case changes aren't saved. I'm using the following code for inserting a row:

using (SqlConnection c = new SqlConnection(Settings.Default.UsersConnectionString))
        {
            c.Open();


            using (SqlCommand d = new SqlCommand())
            {
                d.CommandText = "INSERT INTO Users (UserName) VALUES ('SomeName')";
                d.CommandType = System.Data.CommandType.Text;
                d.Connection = c;
                int t = d.ExecuteNonQuery();
            }


            c.Close();
        }
1
  • 3
    Aside: the c.Close(); row is redundant, this will be taken care of by the outer using clause. May 4, 2011 at 8:07

4 Answers 4

1

You are most likely inside of a transaction that is not committed. Perhaps the Dapper ORM has started a transaction and you must tell it to commit all changes.

1
  • To make it clear I've made a simple console app with only the only code above but there's still the same problem.
    – shadeglare
    May 4, 2011 at 8:14
1

At a guess, the default for your server has been set such that implicit transactions have been turned on. What this means is that, if you execute a command and no transaction is currently open, SQL Server will automatically start a transaction (as always). But when the command completes successfully, this transaction is left open, and needs an explicit commit to be issued.

To determine if this is the case, try querying @@OPTIONS:

IF @@OPTIONS & 2 > 0 
RAISERROR ('Implicit transactions are turned on', 1, 1)
0
1

This code in dapper should be:

var name = "SomeName";
int t  = c.Execute("INSERT INTO Users (UserName) VALUES (@name)", new {name});

Dapper does no internal transaction management, it must be passed in using the optional transaction param.

My gut is telling me you are connecting to a different db to insert the data. Either that of Damiens implicit transaction stuff, which is easy to validate.

7
  • Any way using Dapper to get the last inserted ID?
    – jpshook
    Jul 18, 2011 at 9:35
  • 1
    c.Query<Decimal>("insert ... select SCOPE_IDENTITY()").First() .. not sure if numeric(38) maps cleanly to int @Developr Jul 18, 2011 at 9:45
  • My Identity column in SQL is int. No matter what I set the Dapper query to (int, long, etc), it is giving me a "Specified cast is not valid." exception on Dapper line: 1104 return (T)val; Is there not a way to do it with execute where I get it back as a output parameter similar to how the stored proc example works? Not sure that would be any cleaner, but would at least be a alternative.
    – jpshook
    Jul 18, 2011 at 11:45
  • BTW, the invalid cast was because it was treating the SQL returned int as a decimal! For now, I changed the c.Query<decimal> and it is working, although it is a bit of a kludge.
    – jpshook
    Jul 18, 2011 at 11:53
  • yeah Decimal is it ... Dapper is a stickler for having the correct types set up ... it does no downcasting or upcasting by design Jul 18, 2011 at 11:58
0

It may be the database is automatically creating a transaction that's not being committed, try creating your own transaction in the code and committing it manually and see if that works

using (SqlConnection c = new SqlConnection(Settings.Default.UsersConnectionString))
{
   c.Open();
   using (SqlTransaction t = c.BeginTransaction("InsertIntoUsers"))
   {
        using (SqlCommand d = new SqlCommand())
        {
            d.CommandText = "INSERT INTO Users (UserName) VALUES ('SomeName')";
            d.CommandType = System.Data.CommandType.Text;
            d.Connection = c;
            int t = d.ExecuteNonQuery();
        }
        t.Commit();
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.