How can I create a MySql function in C#? -
i'm trying create mysql function in c#. reason i'm doing because want loop through multiple mysql databases , create function in each one.
the problem mysqlcommand not delimiter syntax required create function. script below executes fine in mysql clients, not work in context:
delimiter // drop function if exists cleandate // create function cleandate(mydate date) returns date reads sql data begin return (case when mydate <= '1900-01-01' '1900-01-01' when mydate = '0000-00-00' '1900-01-01' when date_format(mydate, '%y') = '0000' '1900-01-01' when date_format(mydate, '%m') = '00' '1900-01-01' when date_format(mydate, '%d') = '00' '1900-01-01' else mydate end); end//
the c# code trying implement this(assuming connstrings list of strings):
foreach(var connstring in connstrings) { // create connection database mysql.data.mysqlclient.mysqlconnection dbconn = new mysql.data.mysqlclient.mysqlconnection(connstring); mysqlcommand cmd = dbconn.createcommand(); cmd.commandtext = @"[same script here]"; try { dbconn.open(); cmd.executenonquery(); } catch (exception erro) { console.writeline(erro.tostring()); } }
i "syntax error near delimiter //" when run though. ideas how can achieve this?
the mysql connector offers mysqlscript class can used in place of mysqlcommand when dealing executing multiple statements , setting delimiters.
adapted example:
foreach(var connstring in connstrings) { mysqlconnection dbconn = new mysqlconnection(connstring); try { dbconn.open(); mysqlscript script = new mysqlscript(dbconn); script.query = @"[same script here]"; script.delimiter = @"//"; script.execute(); } catch (exception error) { console.writeline(error.tostring()); } }
https://dev.mysql.com/doc/connector-net/en/connector-net-tutorials-mysqlscript.html
Comments
Post a Comment