2017-04-03 86 views
1

我想在MonoDevelop中創建一個簡單的登錄窗口,但只要點擊按鈕就會崩潰。用SQLite登錄屏幕崩潰

使用以下命令:SqliteConnection.CreateFile("*.sqlite");我能確定它運行到第26行,但不是28

這裏是我的代碼:

using System; 
    using Gtk; 
    using Mono.Data.Sqlite; 
    using System.Data; 

    namespace BB 
    { 
     public partial class BBLogin : Gtk.Window 
     { 
      public BBLogin() : 
       base (Gtk.WindowType.Toplevel) 
      { 
       this.Build(); 
      } 

      private void btnLoginOnClick (object sender, EventArgs e) 
      { 
       SqliteConnection conn = new SqliteConnection(); 
       conn.ConnectionString = "Data Source=BBUser.sqlite;Version=3;"; 
       SqliteCommand command = new SqliteCommand(); 
       command.CommandText = ("SELECT UserName FROM T_test WHERE [email protected] AND [email protected]"); 
       command.Parameters.AddWithValue ("@UserName", txtUserName.Text); 
       command.Parameters.AddWithValue ("@Password", txtPassword.Text); 

       conn.Open(); 
/*line 26*/  SqliteConnection.CreateFile("failsafe0.sqlite"); 
       object result = command.ExecuteScalar(); 
       SqliteConnection.CreateFile("failsafe1.sqlite"); 
       conn.Close(); 
       SqliteConnection.CreateFile("failsafe2.sqlite"); 
       string userNameLogin = Convert.ToString(result); 
       SqliteConnection.CreateFile("failsafe3.sqlite"); 

       if (userNameLogin != "") 
       { 
        SqliteConnection.CreateFile("success.sqlite"); 
        /*MessageDialog md = new MessageDialog ("Username was correct!"); 
        md.Run(); 
        md.Destroy();*/ 
       } 
       else 
       { 
        SqliteConnection.CreateFile("failed.sqlite"); 
        /*MessageDialog md = new MessageDialog ("Username or password is incorrect!"); 
        md.Run(); 
        md.Destroy();*/ 
       } 
      } 

     } 
    } 

,這裏是完整的異常詳細信息:

Gtk#回調委託中的異常 注意:應用程序可以使用GLib.ExceptionManager.UnhandledException來處理異常。 System.Reflection.TargetInvocationException:調用的目標引發了異常。 ---> System.InvalidOperationException:沒有與此命令關聯的連接 at Mono.Data.Sqlite.SqliteCommand.InitializeForReader()[0x00000] in:0 at Mono.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior) [0x00000] in:0 at Mono.Data.Sqlite.SqliteCommand.ExecuteScalar()[0x00000] in:0 at BB.BBLogin.btnLoginOnClick(System.Object sender,System.EventArgs e)[0x0006c] in/home/(包裝託管到本機)System.Reflection.MonoMethod:InternalInvoke(System.Reflection.MonoMethod,object,object [],System.Exception &) at System。回到頂端這篇文章中的信息適用於: Reflection.MonoMethod.Invoke(System.Object obj,BindingFlags invokeAttr,System.Reflection.Binder聯編程序,System.Object []參數在System.Reflection.MonoMethod.Invoke(System.Object obj,BindingFlags invokeAttr,System.Reflection。)中的內部異常堆棧跟蹤--- 結束。在System.Reflection.MethodBase.Invoke(System.Object obj,System.Object []參數)[0x00000]中的[]中的[0x00000] 0 at System.Delegate.DynamicInvokeImpl(System.Object [] args)[0x00000] in:0 at System.MulticastDelegate.DynamicInvokeImpl(System.Object [] args)[0x00000] in:0 at System.Delegate.DynamicInvoke (System.Object [] args)[0x00000] in:0 at GLib.Signal.ClosureInvokedCB(System.Object o,GLib.ClosureInvokedArgs args)[0x00000] in:0 at在GLib.SignalClosure.MarshalCallback(IntPtr raw_closure,IntPtr return_val,UInt32 n_param_vals,IntPtr param_values,IntPtr invocation_hint,IntPtr marshal_data)[0x00000]中的:0 在GLib.ExceptionManager.RaiseUnhandledException(System.Exception的E,布爾is_terminal) 在GLib.SignalClosure.MarshalCallback(IntPtr的raw_closure,IntPtr的RETURN_VAL,UInt32的n_param_vals,IntPtr的param_values,IntPtr的invocation_hint,IntPtr的marshal_data) 在Gtk.Application.gtk_main() 在Gtk.Application.Run() 在BB.MainClass.Main在/ home(System.String []參數)/基督教/ BB/BB /Program.cs:line 15

此致

Chris

回答

0

要執行的命令需要使用命令文本和連接。您提供的命令文本,但不連接的結合

執行命令

command.Connection = conn; 

而且之前剛剛加入這一行,我建議你用一次性物品打交道時使用此代碼

string userNameLogin = string.Empty; 
using(SqliteConnection conn = new SqliteConnection("Data Source=BBUser.sqlite;Version=3;")) 
using(SqliteCommand command = new SqliteCommand(@"SELECT UserName 
     FROM T_test WHERE [email protected] AND [email protected]", conn)) 
{ 
     command.Parameters.AddWithValue ("@UserName", txtUserName.Text); 
     command.Parameters.AddWithValue ("@Password", txtPassword.Text); 
     conn.Open(); 
     result = command.ExecuteScalar(); 
} 
if(string.IsNullOrEmpty(result)) 
{ 
    // Failure 
} 
else 
{ 
    // Success 
} 

這種方法更好,因爲可以保證當代碼從使用塊中退出時,連接將被關閉並處理,以防異常。還要注意,使用帶有兩個參數(命令文本和連接)的SqliteCommand重載,你不必記住在之後設置它們。

+0

非常感謝!這有助於:) – Chriswidell