日期:2014-05-18  浏览次数:20803 次

TypeInitializationException,类的初始值设定发生异常
我定义了一个类
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Data.SqlClient;
/// <summary>
/// DB 的摘要说明
/// </summary>
namespace DAL
{
  public class OleDbDB
  {
  #region Fields
  private static readonly string connectionstring = GetConnectionString();
  private static SqlConnection conn = new SqlConnection(connectionstring);
  #endregion

  #region Properties

  #endregion

  #region Constructed Function
  public OleDbDB()
  {
  }
  #endregion

  #region Methods
  private static string GetConnectionString()
  {
  return ConfigurationManager.AppSettings["ConnetionString"].ToString();
  }
  private static bool OpenConnection()
  {
  if (conn.State != ConnectionState.Open)
  {
  try
  {
  conn.Open();
  }
  catch
  {
  conn.Close();
  return false;
  }
  }
  return true;
  }

  public static SqlDataReader ReturnOleDbDataReader(string sql)
  {
  if (OpenConnection())
  {
  //conn已经打开
  SqlCommand cmd = new SqlCommand(sql, conn);
  SqlDataReader dr;
  try
  {
  dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  }
  catch
  {
  conn.Close();
  return null;
  }
  //DataReader的数据读完之前不能关闭conn
  return dr;
  }
  else
  {
  conn.Close();
  return null;
  }
  }
  public static DataSet ReturnDataSet(string sql)
  {
  if (OpenConnection())
  {
  SqlDataAdapter da = new SqlDataAdapter(sql, conn);
  DataSet ds = new DataSet();
  try
  {
  da.Fill(ds);
  }
  catch
  {
  conn.Close();
  return null;
  }
  conn.Close();
  return ds;
  }
  else
  {
  conn.Close();
  return null;
  }
  }
  public static DataTable ReturnDataTable(string sql, int TableIndex)
  {
  DataSet ds = ReturnDataSet(sql);
  if (ds != null)
  {
  return ds.Tables[TableIn

相关资料更多>