如何创建一段C#.NET脚本让MySql服务器执行MySql脚本?

发布于 2024-11-20 00:07:24 字数 528 浏览 4 评论 0原文

假设我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

反话 2024-11-27 00:07:24

您能否将 myTest.sql 文件加载到字符串中并将其传递给 MySqlCommand。

string myTestSql = IO.File.ReadAllText("myTest.sql");
...
MySqlCommand newCmd = new MySqlCommand(myTestSql, cn);

只要 MySQL 接受以分号分隔的命令,就应该可以工作。

Could you load the myTest.sql file into a string and pass it to the MySqlCommand.

string myTestSql = IO.File.ReadAllText("myTest.sql");
...
MySqlCommand newCmd = new MySqlCommand(myTestSql, cn);

Should work as long as MySQL accepts commands separated by semicolons.

烟酒忠诚 2024-11-27 00:07:24

您当然不必每次都打开和关闭连接,但如果您一次运行其中的每一个并查看结果以确保语句成功完成,这将是最干净的。不幸的是,如果您运行包含 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.

弃爱 2024-11-27 00:07:24

使用这个
如果您想使用 .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#.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文