2017-04-03 157 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