0

SOや他の場所にも同様の質問がいくつかありますが、ほとんどがphpであり、私はそれを理解していません。次のような62個のテーブルを持つデータベースを復元しようとしています:

string query = @"SET SQL_MODE= 'NO_AUTO_VALUE_ON_ZERO'; CREATE DATABASE " + dbName + " DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; USE " + dbName + ";" + Environment.NewLine;

using (StreamReader reader = File.OpenText("C:\b.sql"))
{
    string line = reader.ReadToEnd();
    query += line; //almost 1700 lines.
}
// upto this i get the query correctly which works fine in phpMyAdmin.

MySqlCommand c = new MySqlCommand(query, conn);
c.ExecuteReader();
//but when I execute, throws: "Fatal error encountered during command execution."

これはなぜですか?それがクエリの長​​さの 'cos である場合、アプリケーションからこのような大きなクエリを実行するにはどうすればよいでしょうか?

4

2 に答える 2

1

チェックエラーのためにこれを試してください:

List<string> query = new List<string>(){
        "SET SQL_MODE= 'NO_AUTO_VALUE_ON_ZERO';",
        string.Format("CREATE DATABASE `{0}` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;", dbName),
        string.Format("USE `{0}`;", dbName)}; // error string.Format("USE `{0}`;{1}", dbName)
/*    
using (StreamReader reader = File.OpenText("C:\b.sql"))
{
    while (reader.Peek() >= 0)
        query.Add(reader.ReadLine());
}
*/

using (StreamReader reader = File.OpenText("C:\b.sql"))
{
    string lines = reader.ReadToEnd();
    string[] alines = lines.Split(';');
    foreach(string q in alines) 
            query.Add(q);
}

foreach (string command in query)
{
    try
    {
        using (MySqlCommand c = new MySqlCommand(command, conn))
        {
            c.ExecuteReader();
            Console.WriteLine(string.Format("OK Command: {0}", command));
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(string.Format("Error: {0}. Command: {1}", ex.Message, command));
        break;
    }
}

編集

パフォーマンスを向上させるために、このクラスを使用できます。私は試していませんが、うまくいくことを願っています:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.IO;

namespace MySQLHelperTest
{
    public class MySQLTestingQuery
    {
        public MySqlConnection MyConnection { get; set; }
        public string FileSql { get; set; }
        public List<string> PreviousQuerys { get; set; }
        public List<string> CorrectQuerys { get; private set; }
        public string ErrorQuery { get; private set; }

        public MySQLTestingQuery()
        {
            this.CorrectQuerys = new List<string>();
            this.ErrorQuery = string.Empty;
        }

    public void Start()
    {
        FileInfo file = new FileInfo(this.FileSql);
        if (!file.Exists)
            throw new ApplicationException(string.Format("nonexistent file: '{0}'", this.FileSql));

        if (this.PreviousQuerys != null)
            foreach (string command in this.PreviousQuerys)
                this.RunMySQLCommand(command);

        try
        {
            foreach (string command in this.ReadQuerys(this.FileSql))
                Console.WriteLine(command);
        }
        catch (ApplicationException ex)
        {
            throw ex;
        }
        catch (Exception ex)
        {
            throw new ApplicationException(string.Format("an unexpected error happened: {0}. ", ex.Message));
        }

    }

        private void RunMySQLCommand(string command)
        {
            try
            {
                using (MySqlCommand c = new MySqlCommand(command, this.MyConnection))
                {
                    c.ExecuteReader();
                    this.CorrectQuerys.Add(command);
                }
            }
            catch (Exception ex)
            {
                this.ErrorQuery = command;
                throw new ApplicationException(string.Format("error: {0}. command: {1}", ex.Message, command));
            }
        }

        private IEnumerable<string> ReadQuerys(string file)
        {
            using (StreamReader sr = new StreamReader(file)) 
            {
                string query = string.Empty;
                while (sr.Peek() >= 0)
                {
                    query += (char)sr.Read();
                    if (query.EndsWith(";"))
                    {
                        yield return query;
                        query = string.Empty;
                    }
                }
            }
        }

    }
}
于 2011-08-12T13:34:28.030 に答える
1

MySql のクエリ文字列の長さに制限があるかどうかはわかりません。私が最初に考えたのは、巨大な 1700 行のクエリ セットを個々のクエリに分割することです。

string query = @"SET SQL_MODE= 'NO_AUTO_VALUE_ON_ZERO'; CREATE DATABASE " + dbName + " DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci" + Environment.NewLine;
MySqlCommand c;
c = new MySqlCommand(query, conn);
c.ExecuteNonQuery();

using (StreamReader reader = File.OpenText("C:\b.sql"))
{
    string line = reader.ReadToEnd();
    query = "USE " + dbName + "; " + line;
    c = new MySqlCommand(query, conn);
    c.ExecuteNonQuery();
}

このようにして、データベースを作成し、各クエリを個別に実行できます。データを知らなくても、問題を特定するのに役立ちます。"ExecuteNonQuery" を try catch ブロックで囲むと、失敗したクエリをキャッチしてログ ファイルに保存し、バッチが完了したときに確認できます。

于 2011-08-12T13:17:30.193 に答える