我正在尝试在MonoDevelop中创建一个简单的登录窗口,但是一旦点击按钮就会崩溃。
使用以下命令: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 UserName=@UserName AND Password=@Password");
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 #callback委托中的异常 注意:应用程序可以使用GLib.ExceptionManager.UnhandledException来处理异常。 System.Reflection.TargetInvocationException:调用目标抛出了异常。 ---> System.InvalidOperationException:没有与此命令关联的连接 在Mono.Data.Sqlite.SqliteCommand.InitializeForReader()[0x00000] in:0 在Mono.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)[0x00000] in:0 在Mono.Data.Sqlite.SqliteCommand.ExecuteScalar()[0x00000] in:0 在BB.BBLogin.btnLoginOnClick(System.Object sender,System.EventArgs e)[0x0006c] in /home/christian/BB/BB/BBLogin.cs:27 at(wrapper managed-to-native)System.Reflection.MonoMethod:InternalInvoke(System.Reflection.MonoMethod,object,object [],System.Exception&) 在System.Reflection.MonoMethod.Invoke(System.Object obj,BindingFlags invokeAttr,System.Reflection.Binder binder,System.Object [] parameters,System.Globalization.CultureInfo culture)[0x00000] in:0 ---内部异常堆栈跟踪结束--- 在System.Reflection.MonoMethod.Invoke(System.Object obj,BindingFlags invokeAttr,System.Reflection.Binder binder,System.Object [] parameters,System.Globalization.CultureInfo culture)[0x00000] in:0 在System.Reflection.MethodBase.Invoke(System.Object obj,System.Object [] parameters)[0x00000] in:0 在System.Delegate.DynamicInvokeImpl(System.Object [] args)[0x00000] in:0 在System.MulticastDelegate.DynamicInvokeImpl(System.Object [] args)[0x00000] in:0 在System.Delegate.DynamicInvoke(System.Object [] args)[0x00000] in:0 在GLib.Signal.ClosureInvokedCB(System.Object o,GLib.ClosureInvokedArgs args)[0x00000] in:0 at GLib.SignalClosure.Invoke(GLib.ClosureInvokedArgs args)[0x00000] in:0 在GLib.SignalClosure.MarshalCallback(IntPtr raw_closure,IntPtr return_val,UInt32 n_param_vals,IntPtr param_values,IntPtr invocation_hint,IntPtr marshal_data)[0x00000] in:0 at GLib.ExceptionManager.RaiseUnhandledException(System.Exception e,Boolean is_terminal) at 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(System.String [] args)/ home / christian / BB / BB /Program.cs:line 15
致以最诚挚的问候,
克里斯
答案 0 :(得分: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 UserName=@UserName AND Password=@Password", 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
}
这种方法更好,因为在异常的情况下,当代码退出using块时,可以保证关闭和处理连接。另请注意,使用带有两个参数(命令文本和连接)的SqliteCommand重载,您不必记住以后设置它们。