本文内容主要记录本人学习过程中遇到的错误,其中不乏有一些低级错误,为了牢记以免再次犯错特意记录,不要笑话哈;
1.“System.TypeInitializationException”类型的未经处理的异常在 LoginDAL.dll 中发生其他信息: “sqlHelper.sqlHelper”的类型初始值设定项引发异常;
问题原因:App.config文件中配置的connectionString.name="conn"与代码中读取配置文件ConfigurationManager.ConnectionStrings["connStr"]提取的索引名称不一致;
解决办法:保持一致;
2.阅读器关闭时尝试调用Read无效;
问题原因:①在获取SqlDataReader时创建连接使用了using();②在Reader SqlDataReader前关闭了链接或Reader
解决办法:去掉using,在Reader之前不要关闭Reader;
3.Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data. For WPF bind to DbSet.Local. For WinForms bind to DbSet.Local.ToBindingList(). For ASP.NET WebForms you can bind to the result of calling ToList() on the query or use Model Binding......
private void barBtnQuery_ItemClick(object sender, HuanSi.XtraBars.ItemClickEventArgs e) { using(var db=new DBModel()) { var result = from s in db.Students select new { s.sName,s.iAge}; gridControl1.DataSource = result.ToList();//不加ToList()会报错 gridView1.PopulateColumns(); } }