0
        cmd = new SQLiteCommand();
        ...
        if (Convert.ToInt32(cmd.ExecuteScalar()) == 0)
            goto doClose;
        else
            cmd.CommandText = sql;
        reader = cmd.ExecuteReader(); // this is line 182, check the exception details below

もっと:私は寝なければなりません。以下はコード ソース (GitHub からの私のレポ) です。git@github.com:tomxuetoy/WPF_startPrograms.git

上記は私のコードで、正常に動作します。私の場合、テーブルが存在しないcmd.ExecuteScalar()ため null が返されます。SQLiteそして、以下のように変更しようとしましたが失敗しました:

if (cmd.ExecuteScalar() == null)

だから私は式(null返された)をnull直接比較できない理由を知りたいですか?ありがとう!

詳細: 以下のものを試してみましたが、同じ結果になりました: 動作しません

if (cmd.ExecuteScalar() == DBNull.Value)
or
if (cmd.ExecuteScalar() is DBNull)

詳細な例外は以下にコピーされていますが、一部の漢字が含まれています...

System.Windows.Markup.XamlParseException occurred
  HResult=-2146233087
  Message=对类型“MultiStart.MainWindow”的构造函数执行符合指定的绑定约束的调用时引发了异常。
  Source=PresentationFramework
  LineNumber=0
  LinePosition=0
  StackTrace:
       在 System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
  InnerException: System.Data.SQLite.SQLiteException
       HResult=-2147467259
       Message=SQLite error
no such table: testTable
       Source=System.Data.SQLite
       ErrorCode=-2147467259
       StackTrace:
            在 System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn, String strSql, SQLiteStatement previous, UInt32 timeoutMS, String& strRemain)
            在 System.Data.SQLite.SQLiteCommand.BuildNextCommand()
            在 System.Data.SQLite.SQLiteCommand.GetStatement(Int32 index)
            在 System.Data.SQLite.SQLiteDataReader.NextResult()
            在 System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave)
            在 System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
            在 System.Data.SQLite.SQLiteCommand.ExecuteReader()
            在 MultiStart.MainWindow.dbOp(dowhat dw) 位置 c:\Users\Administrator\Desktop\WPF_startPrograms\WpfApplication2\WpfApplication2\MainWindow.xaml.cs:行号 182
            在 MultiStart.MainWindow.DataBinding() 位置 c:\Users\Administrator\Desktop\WPF_startPrograms\WpfApplication2\WpfApplication2\MainWindow.xaml.cs:行号 43
            在 MultiStart.MainWindow..ctor() 位置 c:\Users\Administrator\Desktop\WPF_startPrograms\WpfApplication2\WpfApplication2\MainWindow.xaml.cs:行号 36
       InnerException: 
4

4 に答える 4

1

null 値が返されることはあまりありません。Reader(および/または) が実行される前にExecuteScalar、例外がスローされます。それをキャッチし、それに応じて処理します...

try 
{
       if (Convert.ToInt32(cmd.ExecuteScalar()) == 0)
            goto doClose; // Really? That must be complex then...
        else
            cmd.CommandText = sql;
        reader = cmd.ExecuteReader(); 
}
catch(SQLiteException exp)
{
       Trace.WriteLine( exp.Message);
}
于 2013-09-21T16:17:41.027 に答える