运行程序时出现 DBHelper 的类型初始值设定项引发异常。
publicstaticclassDBHelper{publicstaticstringconnStr=WebConfigurationManager.Connectio...
public static class DBHelper
{
public static string connStr = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
public static bool ExcuteCommand(string sql)
{
OleDbConnection conn = new OleDbConnection(connStr);
OleDbCommand cmd = new OleDbCommand(sql, conn);
conn.Open();
int i = cmd.ExecuteNonQuery();
conn.Close();
if (i != 0)
return true;
else
return false;
}
public static DataSet GetDataSet(string sql)
{
OleDbConnection conn = new OleDbConnection(connStr);
OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
public static OleDbDataReader GetReader(string sql)
{
OleDbConnection conn = new OleDbConnection(connStr);
OleDbCommand cmd = new OleDbCommand(sql, conn);
conn.Open();
OleDbDataReader dr = cmd.ExecuteReader();
conn.Close();
return dr;
}
public static int GetScalar(string sql)
{
return 0;
}
//将excel中的数据导入dataset
public static DataSet ImportExcel(string filePath, string fileName, string sheetName)
{
try
{
string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filePath + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = string.Format("select * from [{0}$]", sheetName);
OleDbDataAdapter da = new OleDbDataAdapter(strExcel, conn);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
return ds;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
改成.Tostring()了,但还是不正确。但是我用的是SQL数据库。这些代码是从别的程序中拷到自己的程序中的,运行后就出现了引发异常的错误。到底是怎么回事啊? 展开
{
public static string connStr = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
public static bool ExcuteCommand(string sql)
{
OleDbConnection conn = new OleDbConnection(connStr);
OleDbCommand cmd = new OleDbCommand(sql, conn);
conn.Open();
int i = cmd.ExecuteNonQuery();
conn.Close();
if (i != 0)
return true;
else
return false;
}
public static DataSet GetDataSet(string sql)
{
OleDbConnection conn = new OleDbConnection(connStr);
OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
public static OleDbDataReader GetReader(string sql)
{
OleDbConnection conn = new OleDbConnection(connStr);
OleDbCommand cmd = new OleDbCommand(sql, conn);
conn.Open();
OleDbDataReader dr = cmd.ExecuteReader();
conn.Close();
return dr;
}
public static int GetScalar(string sql)
{
return 0;
}
//将excel中的数据导入dataset
public static DataSet ImportExcel(string filePath, string fileName, string sheetName)
{
try
{
string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filePath + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = string.Format("select * from [{0}$]", sheetName);
OleDbDataAdapter da = new OleDbDataAdapter(strExcel, conn);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
return ds;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
改成.Tostring()了,但还是不正确。但是我用的是SQL数据库。这些代码是从别的程序中拷到自己的程序中的,运行后就出现了引发异常的错误。到底是怎么回事啊? 展开
1个回答
展开全部
public static string connStr = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
//第一句就错啦。
改为:
public static string connStr=WebConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
//第一句就错啦。
改为:
public static string connStr=WebConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询