2012-03-18 21 views
5

के माध्यम से MySQL के साथ मैं सी # (4.0 फ्रेमवर्क) में सीएमएस एप्लीकेशन विकसित कर रहा हूं जो MySQL कनेक्टर (6.5.4) द्वारा दूरस्थ सर्वर पर MySQL डेटाबेस (5.0.95) से कनेक्ट होता है।सी # कनेक्टर/नेट

मुझे क्वेरी निष्पादित करने में समस्या है।

उदा मेरे कनेक्शन स्ट्रिंग:

"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

स्टैकट्रैक इंगित करता है कि ExecuteNonQuery ExecuteReader को कॉल करता है। मैं कहूंगा कि आप अभी भी डेटाबेस में सफलतापूर्वक कनेक्ट नहीं हो रहे हैं। क्या आप कभी भी 'इनिट()' कॉल करते हैं? – dwerner

+0

उस बिंदु पर 'कमांड' क्या है जहां आपको अपवाद मिलता है? वहां एक नज़र डालें। अपवाद होने से पहले राज्य की जांच करने के लिए ब्रेकपॉइंट उपयोगी होते हैं ... – 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

@ मिखाइल प्रीस्कुरंटोव: आपके सुझाव के लिए धन्यवाद, लेकिन उदाहरण में आप मुझे निर्देश दे रहे हैं, लड़के के कुछ अपवादों में संदेश है: "टाइमआउट समाप्त हो गया। ऑपरेशन या सर्वर के पूरा होने से पहले टाइमआउट अवधि समाप्त हो गई जवाब नहीं दे रहा है। " मेरे पास मेरे स्टैक ट्रेस में वह संदेश नहीं है। अंत में, मैंने अपनी कनेक्शन स्ट्रिंग में कमांड टाइमआउट जोड़ा, लेकिन स्थिति अभी भी वही है। – 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