如何创建一段C#.NET脚本让MySql服务器执行MySql脚本?
假设我有一个 myTest.sql 脚本,其中包含数千条“create table blahblah”语句。
myTest.sql :
CREATE TABLE A;
CREATE TABLE A1;
....
CREATE TABLE A1000;
我想要实现的是制作一个 C# 脚本来强制 MySql 服务器执行 myTest.sql 文件,而不是做
using (MySqlConnection cn = new MySqlConnection(ConectionString))
{
MySqlCommand newCmd = new MySqlCommand("create statement here 1", cn);
cn.Open();
newCmd.ExecuteNonQuery();
cn.Close();
}
我不想重复 1000 次或 for 循环之类的事情。感谢您的所有帮助,请原谅我的语法问题。
Suppose I have a myTest.sql scripts, which contains thousands of "create table blahblah" statements.
myTest.sql :
CREATE TABLE A;
CREATE TABLE A1;
....
CREATE TABLE A1000;
What Im trying to achieve is that make an C# script to force MySql server EXECUTE the myTest.sql file, instead of doing
using (MySqlConnection cn = new MySqlConnection(ConectionString))
{
MySqlCommand newCmd = new MySqlCommand("create statement here 1", cn);
cn.Open();
newCmd.ExecuteNonQuery();
cn.Close();
}
I dont want to repeat 1000 times or a for loop something like that. Thanks for all helps and please forgive my grammar problems.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您能否将 myTest.sql 文件加载到字符串中并将其传递给 MySqlCommand。
只要 MySQL 接受以分号分隔的命令,就应该可以工作。
Could you load the myTest.sql file into a string and pass it to the MySqlCommand.
Should work as long as MySQL accepts commands separated by semicolons.
您当然不必每次都打开和关闭连接,但如果您一次运行其中的每一个并查看结果以确保语句成功完成,这将是最干净的。不幸的是,如果您运行包含 1000 条语句的巨型语句并且失败,则您没有一种简单的方法来确定哪些步骤成功以及哪些步骤必须重复。
You certainly don't have to open and close the connection every single time, but it would be the cleanest if you ran each of these one at a time and look at the result to ensure that the statement completed successfully. Unfortunately if you run a giant statement with 1000 statements and it fails, you don't have an easy way of determining which step(s) were successful and which have to be repeated.
使用这个
如果您想使用 .net / c#,您可以使用 mysql 命令行和带有
System.Process
静态方法的 shell 来完成此操作。Use this
You can do it using mysql command-line and shell with
System.Process
static methods if you want to use .net / c#.