locked
Execute command on insert query doesn`t work - using Dapper RRS feed

  • Question

  • User-914804179 posted

    Hey all,

    I had a problem with using the Execute command, when I start debugging and check the query out - it does work since I see the data displayed on the program. But, at the DataBase it doesn`t show anything.

    I tried running the query on the Database itself and it did work.
    The query is an insert type, this is my code:

            public void InsertCar(string manufacturer, string carName, int carYear)
            {
                using (OleDbConnection connection = new OleDbConnection(Helper.CnnVal("CarDB")))
                {
                    connection.Execute("AddCar", new { Manufacturer = manufacturer, CarName = carName, CarYear = carYear }, commandType: CommandType.StoredProcedure);
                }
            }

    Why doesn`t the DB inserting the data into itself? it seems like it puts it there as temp data when I debug, and when I stop debugging it`s gone.
    Thanks!

    Thursday, January 7, 2021 3:14 PM

Answers

  • User-939850651 posted

    Hi Adir987,

    According to the code you provided, it seems to be no problem. 

    And I created a simple demo and got it to run correctly. The corresponding data will be updated in the database. So I am not sure of the cause of your issue.

    My test:

    protected void Insert_Click(object sender, EventArgs e)
            {
                string sql = "Owner_Insert";
    
                string conStr = ConfigurationManager.ConnectionStrings["conStr"].ToString();
                int result;
                using (SqlConnection connection = new SqlConnection(conStr))
                {
                    connection.Open();
                    result = connection.Execute(sql,
                        new {UserID = 2, Name = "Name2"},
                        commandType: CommandType.StoredProcedure
                    );
                }
                if (result > 0) {
                    Response.Write("<script>alert('insert successfully!')</script>");
                }
            }
    create or alter proc Owner_Insert
    @UserID int,
    @Name varchar(50)
    as
    begin
    insert into Owners (UserID,Name)values (@UserID,@Name)
    end

    Why doesn`t the DB inserting the data into itself? it seems like it puts it there as temp data when I debug, and when I stop debugging it`s gone.

    Based on the temporary data you mentioned, have you read the answer in this case?

    Why saving changes to a database fails?

    Best regards,

    Xudong Peng 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 8, 2021 7:21 AM

All replies

  • User-939850651 posted

    Hi Adir987,

    According to the code you provided, it seems to be no problem. 

    And I created a simple demo and got it to run correctly. The corresponding data will be updated in the database. So I am not sure of the cause of your issue.

    My test:

    protected void Insert_Click(object sender, EventArgs e)
            {
                string sql = "Owner_Insert";
    
                string conStr = ConfigurationManager.ConnectionStrings["conStr"].ToString();
                int result;
                using (SqlConnection connection = new SqlConnection(conStr))
                {
                    connection.Open();
                    result = connection.Execute(sql,
                        new {UserID = 2, Name = "Name2"},
                        commandType: CommandType.StoredProcedure
                    );
                }
                if (result > 0) {
                    Response.Write("<script>alert('insert successfully!')</script>");
                }
            }
    create or alter proc Owner_Insert
    @UserID int,
    @Name varchar(50)
    as
    begin
    insert into Owners (UserID,Name)values (@UserID,@Name)
    end

    Why doesn`t the DB inserting the data into itself? it seems like it puts it there as temp data when I debug, and when I stop debugging it`s gone.

    Based on the temporary data you mentioned, have you read the answer in this case?

    Why saving changes to a database fails?

    Best regards,

    Xudong Peng 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 8, 2021 7:21 AM
  • User-914804179 posted

    Hey XuDong Peng,

    Thank you for your help!
    The case you sent in the link was my answer, now it is working! thank you!

    Friday, January 8, 2021 6:05 PM