代码之家  ›  专栏  ›  技术社区  ›  Timothy

使用Int32强制转换枚举项0时,查询参数值为Null

  •  0
  • Timothy  · 技术社区  · 16 年前

    Enum LogEventType : int
    {
        SignIn,
        SignInFailure,
        SignOut,
        ...
    }
    
    private static DataTable QueryEventLogSession(DateTime start, DateTime stop)
    {
        DataTable entries = new DataTable();
    
        using (FbConnection conn = new FbConnection(DSN))
        {
            using (FbDataAdapter adapter = new FbDataAdapter(
                "SELECT event_type, event_timestamp, event_details FROM event_log " +
                "WHERE event_timestamp BETWEEN @start AND @stop " +
                "AND event_type IN (@signIn, @signInFailure, @signOut) " +
                "ORDER BY event_timestamp ASC", conn))
            {
                adapter.SelectCommand.Parameters.AddRange(new Object[] {
                    new FbParameter("@start", start),
                    new FbParameter("@stop", stop),
                    new FbParameter("@signIn", (Int32)LogEventType.SignIn),
                    new FbParameter("@signInFailure", (Int32)LogEventType.SignInFailure),
                    new FbParameter("@signOut", (Int32)LogEventType.SignOut)});
    
                Trace.WriteLine(adapter.SelectCommand.CommandText);
                foreach (FbParameter p in adapter.SelectCommand.Parameters)
                {
                    Trace.WriteLine(p.Value.ToString());
                }
    
                adapter.Fill(entries);
            }
        }
        return entries;
    }
    
    1 回复  |  直到 16 年前
        1
  •  1
  •   Raj Kaimal    16 年前

    我不知道这个api,但它可能是一个错误。定义参数时,请尝试传入FbDbType枚举。

    http://www.firebirdsql.org/dotnetfirebird/documentation/api/1.7/FirebirdSql.Data.Firebird.FbDbType.html

    http://www.firebirdsql.org/dotnetfirebird/documentation/api/1.7/FirebirdSql.Data.Firebird.FbParameter.html

    另一个选择是尝试逐个添加参数,而不是使用对象数组和AddRange方法。