2012-03-18 20 views
5

MySQL Connector(6.5.4)でリモートサーバ のMySQLデータベース(5.0.95)に接続するC#(4.0 Framework)でCMSアプリケーションを開発しています。C#with Connector/NET

質問の実行に問題があります。

私の接続文字列:

"Server=" + Options.DbServer + ";Database="+ Options.Database +";Uid=" + Options.DbUser + ";Pwd=" + Options.DbPassword + ";CharSet=utf8; Connect Timeout=30;"; 

私はデータベース関連のものを管理し、静的なクラスがあり、そこに私はプライベートメンバ_connectionを持っています。

private static MySqlConnection _connection; 
public static MySqlConnection Connection 
{ 
    get 
    { 
     if (_connection.State != ConnectionState.Open) 
      _connection.Open(); 

     return _connection; 
    } 
    set { _connection = value; } 
} 

これは、接続を初期化する方法であって、

public static bool InsertRecord(MySqlCommand command) 
{ 
    command.Connection = Connection; 
    if(command.ExecuteNonQuery() > 0) 
     return true; 

    return false; 
} 

command.ExecuteNonQuery()が例外をスローします:コマンドの実行中に発生した致命的なエラーを

public static bool Init(string cs) 
{ 
    _connection = new MySqlConnection(cs); 
    MySqlCommand command = new MySqlCommand("SET NAMES utf8", Connection); 
    command.ExecuteNonQuery(); 
    return true; 
} 

これは私が例外を取得する方法です。

これは、トレース...

MySql.Data.MySqlClient.MySqlException was unhandled 
    Message=Fatal error encountered during command execution. 
    Source=MySql.Data 
    ErrorCode=-2147467259 
    Number=0 
    StackTrace: 
     at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) 
     at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() 
     at CMS.Database.InsertRecord(MySqlCommand command) in C:\_myStuff\VS2010\CMS\CMS\Database.cs:line 95 
     at CMS.frmAddItem.btnDo_Click(Object sender, EventArgs e) in C:\_myStuff\VS2010\CMS\CMS\frmAddItem.cs:line 138 
     at System.Windows.Forms.Control.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
     at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
     at System.Windows.Forms.Control.WndProc(Message& m) 
     at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
     at System.Windows.Forms.Button.WndProc(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
     at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
     at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
     at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.RunDialog(Form form) 
     at System.Windows.Forms.Form.ShowDialog(IWin32Window owner) 
     at System.Windows.Forms.Form.ShowDialog() 
     at CMS.frmMain.btnNovi_Click(Object sender, EventArgs e) in C:\_myStuff\VS2010\CMS\CMS\frmMain.cs:line 381 
     at System.Windows.Forms.Control.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
     at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
     at System.Windows.Forms.Control.WndProc(Message& m) 
     at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
     at System.Windows.Forms.Button.WndProc(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
     at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
     at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
     at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.RunDialog(Form form) 
     at System.Windows.Forms.Form.ShowDialog(IWin32Window owner) 
     at System.Windows.Forms.Form.ShowDialog() 
     at CMS.frmLogin.DoLogin() in C:\_myStuff\VS2010\CMS\CMS\frmLogin.cs:line 55 
     at CMS.frmLogin.button2_Click(Object sender, EventArgs e) in C:\_myStuff\VS2010\CMS\CMS\frmLogin.cs:line 31 
     at System.Windows.Forms.Control.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
     at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
     at System.Windows.Forms.Control.WndProc(Message& m) 
     at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
     at System.Windows.Forms.Button.WndProc(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
     at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
     at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
     at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.Run(Form mainForm) 
     at CMS.Program.Main() in C:\_myStuff\VS2010\CMS\CMS\Program.cs:line 18 
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: MySql.Data.MySqlClient.MySqlException 
     Message=Fatal error encountered attempting to read the resultset. 
     Source=MySql.Data 
     ErrorCode=-2147467259 
     Number=0 
     StackTrace: 
      at MySql.Data.MySqlClient.MySqlDataReader.NextResult() 
      at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) 
     InnerException: MySql.Data.MySqlClient.MySqlException 
      Message=Reading from the stream has failed. 
      Source=MySql.Data 
      ErrorCode=-2147467259 
      Number=0 
      StackTrace: 
       at MySql.Data.MySqlClient.MySqlStream.LoadPacket() 
       at MySql.Data.MySqlClient.MySqlStream.ReadPacket() 
       at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int32& insertedId) 
       at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int32& insertedId) 
       at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force) 
       at MySql.Data.MySqlClient.MySqlDataReader.NextResult() 
      InnerException: System.IO.EndOfStreamException 
       Message=Attempted to read past the end of the stream. 
       Source=MySql.Data 
       StackTrace: 
         at MySql.Data.MySqlClient.MySqlStream.ReadFully(Stream stream, Byte[] buffer, Int32 offset, Int32 count) 
         at MySql.Data.MySqlClient.MySqlStream.LoadPacket() 
       InnerException: 

任意の提案スタックされますか?

+2

'メッセージ=ストリームの終わりを過ぎて読み込もうとしました.' - どこかで_reader_を実行しています。 – Oded

+1

stacktraceは、ExecuteNonQueryがExecuteReaderを呼び出すことを示します。私はあなたがまだ正常にデータベースに接続していないと思います。あなたは 'Init()'を呼び出すことができますか? – dwerner

+0

例外を取得した時点で 'command'とは何ですか?そこを見てください。ブレークポイントは、例外が発生する前の状態を調べるのに便利です。 – dwerner

答えて

6

私はあなたが接続を開いていることに気付きましたが、接続が終了しても接続を閉じることはありません。私は、必要に応じて接続を開く方が好きです。開かれていない場合は開くことはできません。彼らは古いかもしれません。

接続文字列をキャッシュしますが、接続自体はキャッシュしないでください。

public static string ConnectionString {get;set;} 

public static bool InsertRecord(sql) 
{ 
    bool success = false; 
    using (var con = new Connection(ConnectionString)){ 
     var command = new SqlCommand(sql,con); 
     success = (command.ExecuteNonQuery() > 0); 
    } 
    return success; 
} 

リソースは不要になったときに解放する必要があります。

+0

はい、これは明らかに優れたアプローチです。私はあなたの提案に従ってメソッドを変更し、例外はなくなりました。今のところ。 :) どうもありがとうございました! – magic

+0

あなたは大歓迎です! – dwerner

2

この問題に関しては、thisのバグレポートをチェックすることをお勧めします。

+0

デフォルトのコマンドタイムアウト= 3600; – dwerner

+1

@Mikhail Preyskurantov: あなたの提案をありがとうございますが、例では私に指示しています、男は内部例外のいくつかのメッセージを持っています: '"タイムアウトが切れました操作の完了前に経過したタイムアウト時間が 応答しません。 " スタックトレースにそのメッセージがありません。 最後に、接続文字列にコマンドタイムアウトを追加しましたが、状況は同じです。 – magic

1

MySQLは、さまざまなレベルでさまざまなタイムアウト変数を使用します。 >CONNECT_TIMEOUT

サーバが別のクエリのアイドル待機送信する - -

は、接続が確立されている場合>

をWAIT_TIMEOUTクエリが読み出されている場合、または返送される結果セット - >でnet_read_timeout

net_write_timeout net_write_timeoutとでnet_read_timeout両方があるSEあなたは、クエリが面倒であり、それゆえサーバの残りの部分に影響を与えないことがわかっているときに、接続ごとにそれらを単に変更することができます。まず、あなたが何かを実行して、サーバー上でこれらのタイムアウト値のそれぞれについて、デフォルト値をチェックする必要があり

(回避策として)しかし:

show variables like '%timeout%' 

をまた、あなたが挿入しているコマンドを確認する必要がありますそれが簡略化できるかどうかを確認したり、より小さな更新を導入したりすることができます。

+0

これらは私の値です net_read_timeout net_write_timeout これは、読み取りタイムアウトが30秒であることを意味しますか? 私のクエリは実際には非常に簡単です、私はレコードを4列のテーブルに挿入しています。 – magic

+0

はい、そうです^^ クラッシュするのに合っていますか? 'SET @@ session.net_read_timeout = 360;'を試してくださいその場合は、挿入クエリの前に。 – Kharaone

+0

いいえ、すぐにクラッシュするので、このタイムアウトを増やすことは役に立ちません。 – magic