Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. c# to call MySQL Update Stored Proc error

c# to call MySQL Update Stored Proc error

Scheduled Pinned Locked Moved Database
databasehelpannouncementcsharpsharepoint
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jonmy
    wrote on last edited by
    #1

    Hello, I'm trying to call the following MySQL SP:

    DROP PROCEDURE IF EXISTS tgc.UpdateCustomerBalance;
    CREATE PROCEDURE tgc.`UpdateCustomerBalance`(customerId varchar(50), cost Decimal(8,2))
    BEGIN
    UPDATE Customers SET Balance = Balance - cost WHERE CustomerId = customerId;
    END;

    with the following C# code:

    try
    {
    using (var context = new Db_Entities())
    {
    object[] parameters =
    {
    new MySql.Data.MySqlClient.MySqlParameter("@customerId", customerId),
    new MySql.Data.MySqlClient.MySqlParameter("@cost", cost)
    };

                await context.Database.ExecuteSqlCommandAsync("UpdateCustomerBalance @customerId,@cost", parameters);
                }
            }
            catch (Exception e)
            {
                throw;
            }
    

    But I receive the error:

    "Message": "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UpdateCustomerBalance 'xyz',0.03' at line 1",

    Can anyone help shed some light on this? I've tried all sort and am now seeing tunnel vision. Incidentally, this works:

    await context.Database.ExecuteSqlCommandAsync("UPDATE Customers SET Balance = Balance - " + cost + " WHERE CustomerId = '" + customerId + "'");

    All help appreciated.

    Richard DeemingR 1 Reply Last reply
    0
    • J jonmy

      Hello, I'm trying to call the following MySQL SP:

      DROP PROCEDURE IF EXISTS tgc.UpdateCustomerBalance;
      CREATE PROCEDURE tgc.`UpdateCustomerBalance`(customerId varchar(50), cost Decimal(8,2))
      BEGIN
      UPDATE Customers SET Balance = Balance - cost WHERE CustomerId = customerId;
      END;

      with the following C# code:

      try
      {
      using (var context = new Db_Entities())
      {
      object[] parameters =
      {
      new MySql.Data.MySqlClient.MySqlParameter("@customerId", customerId),
      new MySql.Data.MySqlClient.MySqlParameter("@cost", cost)
      };

                  await context.Database.ExecuteSqlCommandAsync("UpdateCustomerBalance @customerId,@cost", parameters);
                  }
              }
              catch (Exception e)
              {
                  throw;
              }
      

      But I receive the error:

      "Message": "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UpdateCustomerBalance 'xyz',0.03' at line 1",

      Can anyone help shed some light on this? I've tried all sort and am now seeing tunnel vision. Incidentally, this works:

      await context.Database.ExecuteSqlCommandAsync("UPDATE Customers SET Balance = Balance - " + cost + " WHERE CustomerId = '" + customerId + "'");

      All help appreciated.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      The MySQL syntax is subtly different to the MSSQL syntax. Try using CALL[^]:

      await context.Database.ExecuteSqlCommandAsync("CALL UpdateCustomerBalance(@customerId,@cost);", parameters);

      jonmy wrote:

      Incidentally, this works:

      await context.Database.ExecuteSqlCommandAsync("UPDATE Customers SET Balance = Balance - " + cost + " WHERE CustomerId = '" + customerId + "'");

      It might work, but it's vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query. In Entity Framework, you can simply use:

      await context.Database.ExecuteSqlCommandAsync("UPDATE Customers SET Balance = Balance - {0} WHERE CustomerId = {1}", cost, customerId);

      EF will replace the placeholders ({0} and {1}) with auto-generated parameter names, and add the parameter values (cost and customerId) as parameters.


      Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^] How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^] Query Parameterization Cheat Sheet | OWASP[^]

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      J 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        The MySQL syntax is subtly different to the MSSQL syntax. Try using CALL[^]:

        await context.Database.ExecuteSqlCommandAsync("CALL UpdateCustomerBalance(@customerId,@cost);", parameters);

        jonmy wrote:

        Incidentally, this works:

        await context.Database.ExecuteSqlCommandAsync("UPDATE Customers SET Balance = Balance - " + cost + " WHERE CustomerId = '" + customerId + "'");

        It might work, but it's vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query. In Entity Framework, you can simply use:

        await context.Database.ExecuteSqlCommandAsync("UPDATE Customers SET Balance = Balance - {0} WHERE CustomerId = {1}", cost, customerId);

        EF will replace the placeholders ({0} and {1}) with auto-generated parameter names, and add the parameter values (cost and customerId) as parameters.


        Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^] How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^] Query Parameterization Cheat Sheet | OWASP[^]

        J Offline
        J Offline
        jonmy
        wrote on last edited by
        #3

        Richard, Worked a treat, thank you. I've trawled the sites looking for a solution but didn't come across the CALL keyword - so very frustrating. On a side note, from a performance perspective, is there a better way of this being implemented? The solution could take a large throughput and I'm keen to understand any improvements. Again, thanks for the tip. JMY.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups