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. General Programming
  3. Windows Forms
  4. How to create mysql stored function from c#.net

How to create mysql stored function from c#.net

Scheduled Pinned Locked Moved Windows Forms
csharphelpquestionmysqltutorial
8 Posts 3 Posters 2 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.
  • N Offline
    N Offline
    nitish_07
    wrote on last edited by
    #1

    I have to create mysql stored function from c#.net. User will create the function at run time and i have to pass this string in odbc command like this...

    rt.Text = "CREATE FUNCTION" +" "+ "`" + node + "`" +" "+ "." +" "+"'"+textBox1.Text +"'"+" "+" ()"+" "+ "@RETURNS INT"+" @BEGIN@END;";
    rt.Text = rt.Text.Replace("@", System.Environment.NewLine);
    OdbcCommand cmd = new OdbcCommand(rt.text, cn);
    cmd.CommandText = rt1.text;
    cmd.ExecuteNonQuery();

    But it shows that You have syntax error.Check the manual that correspond to your mysql document...so plz tell me what is the problem.....??

    L D 2 Replies Last reply
    0
    • N nitish_07

      I have to create mysql stored function from c#.net. User will create the function at run time and i have to pass this string in odbc command like this...

      rt.Text = "CREATE FUNCTION" +" "+ "`" + node + "`" +" "+ "." +" "+"'"+textBox1.Text +"'"+" "+" ()"+" "+ "@RETURNS INT"+" @BEGIN@END;";
      rt.Text = rt.Text.Replace("@", System.Environment.NewLine);
      OdbcCommand cmd = new OdbcCommand(rt.text, cn);
      cmd.CommandText = rt1.text;
      cmd.ExecuteNonQuery();

      But it shows that You have syntax error.Check the manual that correspond to your mysql document...so plz tell me what is the problem.....??

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      nitish_07 wrote:

      But it shows that You have syntax error

      If you encounter an error or an exception, than paste the entire thing into your post. I'm guessing that it's an exception that's thrown on ExecuteNonQuery as your Sql-statement is malformed. It's also damn hard to read. Concatenating strings is fun, but you don't want to overdo it. The code below is allowed too;

      rt.Text = "CREATE FUNCTION `" + node + "` . '"+textBox1.Text +"' () "+
      "@RETURNS INT @BEGIN@END;";

              rt.Text = rt.Text.Replace("@", System.Environment.NewLine);
      

      OdbcCommand cmd = new OdbcCommand(rt.text, cn);
      cmd.CommandText = rt1.text;
      cmd.ExecuteNonQuery();

      I'm going to guess again that it's the `-character that's not being recognized, and that it should be replace with a '. Further, you'd want a Debug.Print(rt.Text) so that you can easily validate the syntax of your query. Hope this helps :)

      Bastard Programmer from Hell :suss:

      N 1 Reply Last reply
      0
      • L Lost User

        nitish_07 wrote:

        But it shows that You have syntax error

        If you encounter an error or an exception, than paste the entire thing into your post. I'm guessing that it's an exception that's thrown on ExecuteNonQuery as your Sql-statement is malformed. It's also damn hard to read. Concatenating strings is fun, but you don't want to overdo it. The code below is allowed too;

        rt.Text = "CREATE FUNCTION `" + node + "` . '"+textBox1.Text +"' () "+
        "@RETURNS INT @BEGIN@END;";

                rt.Text = rt.Text.Replace("@", System.Environment.NewLine);
        

        OdbcCommand cmd = new OdbcCommand(rt.text, cn);
        cmd.CommandText = rt1.text;
        cmd.ExecuteNonQuery();

        I'm going to guess again that it's the `-character that's not being recognized, and that it should be replace with a '. Further, you'd want a Debug.Print(rt.Text) so that you can easily validate the syntax of your query. Hope this helps :)

        Bastard Programmer from Hell :suss:

        N Offline
        N Offline
        nitish_07
        wrote on last edited by
        #3

        Error is this System.Data.Odbc.OdbcException: ERROR [42000] [MySQL][ODBC 5.1 Driver][mysqld-5.0.77-community-nt]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 ''df' () RETURNS INT BEGIN return 1; END' at line 1 and string is i.e rt.text=

        "CREATE FUNCTION `userdb` . 'df' () \nRETURNS INT \nBEGIN\nreturn 1;\nEND;"

        L 1 Reply Last reply
        0
        • N nitish_07

          Error is this System.Data.Odbc.OdbcException: ERROR [42000] [MySQL][ODBC 5.1 Driver][mysqld-5.0.77-community-nt]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 ''df' () RETURNS INT BEGIN return 1; END' at line 1 and string is i.e rt.text=

          "CREATE FUNCTION `userdb` . 'df' () \nRETURNS INT \nBEGIN\nreturn 1;\nEND;"

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I think the error goes away if you change the type of quote in there;

          "CREATE FUNCTION 'userdb'.'df'() \nRETURNS INT \nBEGIN\nreturn 1;\nEND;"

          Again, the character ` cannot be used instead of the character '.

          Bastard Programmer from Hell :suss:

          N 2 Replies Last reply
          0
          • L Lost User

            I think the error goes away if you change the type of quote in there;

            "CREATE FUNCTION 'userdb'.'df'() \nRETURNS INT \nBEGIN\nreturn 1;\nEND;"

            Again, the character ` cannot be used instead of the character '.

            Bastard Programmer from Hell :suss:

            N Offline
            N Offline
            nitish_07
            wrote on last edited by
            #5

            ok I am trying...actually this is written in the orginal mysqlbrowser when i create the fucntion

            CREATE FUNCTION `userdb`.`gh` () RETURNS INT
            BEGIN
            return 1;
            END$$

            so i think this is also ` not ' ???

            1 Reply Last reply
            0
            • L Lost User

              I think the error goes away if you change the type of quote in there;

              "CREATE FUNCTION 'userdb'.'df'() \nRETURNS INT \nBEGIN\nreturn 1;\nEND;"

              Again, the character ` cannot be used instead of the character '.

              Bastard Programmer from Hell :suss:

              N Offline
              N Offline
              nitish_07
              wrote on last edited by
              #6

              Thanks man done....:)

              1 Reply Last reply
              0
              • N nitish_07

                I have to create mysql stored function from c#.net. User will create the function at run time and i have to pass this string in odbc command like this...

                rt.Text = "CREATE FUNCTION" +" "+ "`" + node + "`" +" "+ "." +" "+"'"+textBox1.Text +"'"+" "+" ()"+" "+ "@RETURNS INT"+" @BEGIN@END;";
                rt.Text = rt.Text.Replace("@", System.Environment.NewLine);
                OdbcCommand cmd = new OdbcCommand(rt.text, cn);
                cmd.CommandText = rt1.text;
                cmd.ExecuteNonQuery();

                But it shows that You have syntax error.Check the manual that correspond to your mysql document...so plz tell me what is the problem.....??

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                First, using string concatentation to build a query is just plain bad practice. Especially when you are doing with with consecutive, individual characters. It makes your code impossible to read and debug accurately. Oh, and your call to Replace is not needed at all. Using the @ character in a string to denote new lines in an SQL statement is a bad idea since SQL parameters usually start with that character. A better method is to use String.Format:

                string queryString;
                queryString = String.Format("CREATE FUNCTION '{0}'.'{1}'()\\nRETURNS INT\\nBEGIN\\nEND;", node, functionName);
                

                Now is your code easier to read, or is mine?

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak

                N 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  First, using string concatentation to build a query is just plain bad practice. Especially when you are doing with with consecutive, individual characters. It makes your code impossible to read and debug accurately. Oh, and your call to Replace is not needed at all. Using the @ character in a string to denote new lines in an SQL statement is a bad idea since SQL parameters usually start with that character. A better method is to use String.Format:

                  string queryString;
                  queryString = String.Format("CREATE FUNCTION '{0}'.'{1}'()\\nRETURNS INT\\nBEGIN\\nEND;", node, functionName);
                  

                  Now is your code easier to read, or is mine?

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak

                  N Offline
                  N Offline
                  nitish_07
                  wrote on last edited by
                  #8

                  Yes you are right...Thanks man....If you could provide the solution for the problem which i posted some min ago....

                  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