.NET SqlConnection and SqlCommand

.net dapper sql sqlcommand sqlconnection

Question

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();
        }

Popular Answer

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.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why