연결이 닫힌 경우에도 mysql이 스크립트를 실행할 수 있습니까? .NET 커넥터 API를 통해 mysql 커뮤니티 서버를 사용하고 있습니다. API를 테스트하기 위해 C#을 사용하고있었습니다.연결이 닫힌 경우에도 MySqlScript를 실행하는 MySQL
나는 다음과 같은 정적 클래스를 가지고
using System;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;
public static class DataBase
{
static string connStr = "server=localhost;user=root;port=3306;password=*******;";
static MySqlConnection conn;
public static bool Connect()
{
conn = new MySqlConnection(connStr);
try
{
conn.Open();
}
catch (Exception Ex)
{
ErrorHandler(Ex);
return false;
}
return true;
}
public static int ExecuteScript(string scripttext) // returns the number of statements executed
{
MySqlCommand cmd = conn.CreateCommand();
cmd.CommandText = scripttext;
MySqlScript script;
int count= 0;
try
{
script = new MySqlScript(conn, cmd.CommandText);
script.Error += new MySqlScriptErrorEventHandler(script_Error);
script.ScriptCompleted += new EventHandler(script_ScriptCompleted);
script.StatementExecuted += new MySqlStatementExecutedEventHandler(script_StatementExecuted);
count = script.Execute();
}
catch (Exception Ex)
{
count = -1;
ErrorHandler(Ex);
}
return count;
}
# region EventHandlers
static void script_StatementExecuted(object sender, MySqlScriptEventArgs args)
{
string Message = "script_StatementExecuted";
}
static void script_ScriptCompleted(object sender, EventArgs e)
{
string Message = "script_ScriptCompleted!";
}
static void script_Error(Object sender, MySqlScriptErrorEventArgs args)
{
string Message = "script_Error: " + args.Exception.ToString();
}
# endregion
public static bool Disconnect()
{
try
{
conn.Close();
}
catch (Exception Ex)
{
ErrorHandler(Ex);
return false;
}
return true;
}
public static void ErrorHandler(Exception Ex)
{
Console.WriteLine(Ex.Source);
Console.WriteLine(Ex.Message);
Console.WriteLine(Ex.ToString());
}
}
와 나는 비록 내가 끊기를 (사용하여 내 MySQL의 연결을 종료 한 것으로 나타났습니다
using System;
using System.Data;
namespace Sample
{
public class Sample
{
public static void Main()
{
if (DataBase.Connect() == true)
Console.WriteLine("Connected");
if (DataBase.Disconnect() == true)
Console.WriteLine("Disconnected");
int count = DataBase.ExecuteScript("drop database sample");
if (count != -1)
{
Console.WriteLine(" Sample Script Executed");
Console.WriteLine(count);
}
Console.ReadKey();
}
}
}
이 클래스를 테스트하려면 다음 코드를 사용하고) - 내가 정의한, mysql은 다음에주는 명령을 실행하고 오류가 발생하지 않는다.
닫힌 연결에서 스크립트를 실행하려고 할 때 오류가 발생해야하므로 내가 잘못 생각한 것처럼 느껴집니다.
내 코드/논리 또는 mysql 커넥터의 결함에 문제가 있습니까?
mysql 워크 벤치를 통해 명령이 제대로 실행되었는지 여부를 확인했습니다. 이 MySqlScript.Execute 코드의 컴파일입니다
'novice' 코드를 리팩터링하고 연결 코드를'using() {}'블록 주위에 감싸고 연결이 여전히 있는지 확인하기 위해 조건부 검사를 추가 할 수도 있습니다 공개 여부 .. – MethodMan
나는 그것을 정확히 할 예정이었다. 이것은 커넥터 API가 제대로 작동하는지 확인하는 샘플 코드 일뿐입니다. 감사합니다. – novice