1

So I'm trying to execute the following SQL Code from a C# app

SOURCE Install/Create_Initial_Index
SOURCE Install/Create_Table_Pk

Using the following C# code

public static void installScripts(MySqlConnection conn, String installScriptPath)
{
    MySqlScript script = new MySqlScript(conn, File.ReadAllText(installScriptPath));
    script.Execute();
}

However it is raising a syntax error for the SOURCE keyword

Error Code: 1064. 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 'SOURCE Install/Create_Initial_Index.sql # 20170109 NOT REQUIRED ==>>> SOURCE Inst' at line 1

The SOURCE keyword seems to not work when calling a .sql script from MySQL workbench, but does work when calling it from the mysql> command line. However, it seems that the C# code I'm using is the equivalent to calling it from MySQL workbench. Does anyone know how I can go about changing my C# code in order to equate the call to that of a mysql> command line call. I think this would fix the issue.

Edit 1: I tried doing it by running a shell script from C# but it doesn't work because there's a password required to connect

Edit 2: This question is completely different than what someone identified as a "duplicate". Please read the question fully.

4
  • Possible duplicate of How to execute *.sql mysql file in my c# application Commented Oct 26, 2018 at 19:57
  • No, it's not... Commented Oct 29, 2018 at 14:03
  • You cannot use the SOURCE command in the scripts that are run through the MySqlScript object. It only runs sql commands but the source command is a mysql client command. It is interpreted by the mysql client before sending it to the mysql server. dev.mysql.com/doc/refman/8.0/en/mysql-commands.html Commented Oct 29, 2018 at 14:49
  • @mewkie you misunderstood my point. You're trying to execute "SOURCE somesql.sql" in c# as if it were an SQL statement the server supports, it isn't. SOURCE is a command understood by mysql command line client as "load this script and execute the contents". Hence what you're actually trying/wanting to do is "load the contents of <some sql file> into c# and execute it" -> I linked that other question because it solves the problem you're actually trying to solve (have-a-sql-script-in-a-file-and-execute-it-in-c#), not the problem with your broken solution. (The name for this is "XY problem") Commented Oct 29, 2018 at 16:02

1 Answer 1

0

I think to change your c# code to work the same way as the mysql> command line client you will need to

  • parse the file you read in
  • identify the source statements
  • load and execute each file in the source statement
Sign up to request clarification or add additional context in comments.

1 Comment

i.e. he should read the answers to stackoverflow.com/questions/8855224/… and implement them so that the files with the SQL in (Create_Initial_Index, Create_Table_pk) are read by the c# app (like the mysql command line would), formed into a bunch of sql by the c# (like the command line would) and sent to the server for execution :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.