浏览:583 2007-12-11 16:09 来自 Bob.Xie      :

我这个是自己写的windows程序,只要一与数据库大交道就抱这错:



************** 异常文本 **************
System.TypeInitializationException: “ComputerManage.OleDbDAL.DbHelperOleDb”的类型初始值设定项引发异常。 ---> System.NullReferenceException: 未将对象引用设置到对象的实例。
   在 ComputerManage.OleDbDAL.DbHelperOleDb..cctor() 位置 E:\ComputerManage\ComputerManage\OleDbDAL\DbHelperOleDb.cs:行号 15
   --- 内部异常堆栈跟踪的结尾 ---
   在 ComputerManage.OleDbDAL.DbHelperOleDb.GetSingle(String SQLString, OleDbParameter[] cmdParms)
   在 ComputerManage.OleDbDAL.UserDAL.Exists(String uName) 位置 E:\ComputerManage\ComputerManage\OleDbDAL\UserDAL.cs:行号 52
   在 ComputerManage.BLL.UserBLL.Exists(String uName) 位置 E:\ComputerManage\ComputerManage\BLL\UserBLL.cs:行号 41
   在 FormUI.frmLogin.btnOK_Click(Object sender, EventArgs e) 位置 E:\ComputerManage\ComputerManage\FormUI\frmLogin.cs:行号 41
   在 System.Windows.Forms.Control.OnClick(EventArgs e)
   在 System.Windows.Forms.Button.OnClick(EventArgs e)
   在 System.Windows.Forms.Button.PerformClick()
   在 System.Windows.Forms.Form.ProcessDialogKey(Keys keyData)
   在 System.Windows.Forms.TextBoxBase.ProcessDialogKey(Keys keyData)
   在 System.Windows.Forms.Control.PreProcessMessage(Message& msg)
   在 System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg)
   在 System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)


操作数据方法:
public static int Exists(string uName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select uID from tbUser");
            strSql.Append(" where uName= @uName");
            OleDbParameter[] parameters = {
     new OleDbParameter("@uName", OleDbType.VarChar,50)
    };
            parameters[0].Value = uName;
            object obj = DbHelperOleDb.GetSingle(strSql.ToString(), parameters);   //经调试,发现错误出现在此
            if (obj != null)
            {
                return int.Parse(obj.ToString());
            }
            else
            {
                return 0;
            }
        }

 DbHelperOleDb.GetSingle方法:

 public static object GetSingle(string SQLString, params OleDbParameter[] cmdParms)
        {
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                using (OleDbCommand cmd = new OleDbCommand())
                {
                    try
                    {
                        PrepareCommand(cmd, connection, null, SQLString, cmdParms);
                        object obj = cmd.ExecuteScalar();
                        cmd.Parameters.Clear();
                        if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                        {
                            return null;
                        }
                        else
                        {
                            return obj;
                        }
                    }
                    catch (System.Data.OleDb.OleDbException e)
                    {
                        throw new Exception(e.Message);
                    }
                }
            }
        }

收藏 楼主
  10个月前  Anders Cui      :
把更详细的代码贴出来看看 :)
1楼 回到顶楼 
  10个月前  G yc {Son of VB.NET}      :
异常中不是说了吗?
空引用, 你那个助手,生成出来的东西,没有New出示里吧~
2楼 回到顶楼 
  10个月前  Bob.Xie      :
我已把生成的那个方法贴出来,麻烦高人帮忙解决下
3楼 回到顶楼 
  10个月前  Colin Han      :
你的DbHelperOleDb类型是不是有静态构造函数?或者其中包含静态字段?

请把DBHelperOldDb文件的第15行贴出来!
4楼 回到顶楼 
  10个月前  Bob.Xie      :
问题已解决,多谢楼上的关注
5楼 回到顶楼 
  10个月前  麒麟.NET      :
说说是怎么解决的?
6楼 回到顶楼 

注册用户登录后才能回复,登录注册
> 返回“ASP.NET”


其他话题

相关内容

相关链接

1 24251