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.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Collections;
using System.IO;/// <summary>
/// DBFun 的摘要说明
/// </summary>
public class DBFun
{
protected static SqlConnection conn = new SqlConnection();
protected static SqlCommand comm = new SqlCommand();
public static void IsAdmin()
{
//查看是否为超级管理员
if (HttpContext.Current.Session["usr_id"] != null)
{
string strsql = string.Format("select usr_name from usrinfo where rank_id =5 and usr_id='{0}'", HttpContext.Current.Session["usr_id"].ToString());
DataSet ds = dataSet(strsql);
string xm; if (ds != null & ds.Tables.Count > 0 & ds.Tables[0].Rows.Count > 0)
{
xm = ds.Tables[0].Rows[0]["usr_name"].ToString();
}
else
{
HttpContext.Current.Response.Write("<script>alert('你还没有登录或登录超时!');window.location.href='login.aspx';</script>");
}
}
else
{
HttpContext.Current.Response.Write("<script>alert('你还没有登录或登录超时!');window.location.href='login.aspx';</script>");
} } public DBFun()
{
//
// TODO: 在此处添加构造函数逻辑
//
} /// 打开数据库连接
private static void openConnection()
{
if (conn.State == ConnectionState.Closed)
{
conn.ConnectionString = "Data Source=.;Initial Catalog=OfficeOnLine;User ID=sa;Password=sa";
comm.Connection = conn;
try
{
conn.Open();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
} /// 关闭当前数据库连接
private static void closeConnection()
{
if (conn.State == ConnectionState.Open)
conn.Close();
comm.Dispose();
conn.Dispose(); }
/// 执行Sql查询语句
/// <param name="sqlstr">传入的Sql语句</param>
public static void ExecuteSql(string sqlstr)
{
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
comm.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
closeConnection();
}
} /// <summary>
/// 执行Sql更新语句
/// </summary>
/// <param name="sqlstr">传入的Sql语句</param>
/// <returns>布尔值</returns>
public static bool ExecuteUpdate(string sqlstr)
{
int isUpdateOk = 0;
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
isUpdateOk = Convert.ToInt32(comm.ExecuteNonQuery());
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
closeConnection();
}
if (isUpdateOk > 0)
{
return true;
}
else
{
return false;
}
} public static DataRow GetDataRow(string strSQL)
{
//查询数据,取得数据行
try
{
openConnection();
SqlDataAdapter sda = new SqlDataAdapter(strSQL, conn);
DataSet Rs = new DataSet();
sda.Fill(Rs);
if (Rs.Tables[0].Rows.Count != 0)
return Rs.Tables[0].Rows[0];
else
return null;
}
catch
{
return null;
} }
public static DataView GetDataView(string strSQL)
{
//查询数据,取得数据视图
try
{
openConnection();
SqlDataAdapter sda = new SqlDataAdapter(strSQL, conn);
DataSet Rs = new DataSet();
sda.Fill(Rs);
return Rs.Tables[0].DefaultView; }
catch (Exception e)
{
throw new Exception(e.Message);
} } public static bool SearchTable(string strSQL)
{
SqlDataReader sdr = null;
try
{
openConnection();
comm.CommandText = strSQL;
comm.CommandType = CommandType.Text;
//当 DataReader 处于使用中时,关联的 DbConnection 正忙于为 DataReader 服务。当处于此状态时,除了关闭 Connection 外,不能对其执行其他任何操作。除非调用 DataReader 的 Close 方法,否则会一直处于此状态。如果创建了 DataReader 并将 CommandBehavior 设置为 CloseConnection,则关闭 DataReader 会自动关闭此连接。
sdr = comm.ExecuteReader(CommandBehavior.CloseConnection);
if (sdr.Read())
{ return true;
}
else
{ return false;
}
}
catch
{
try
{
sdr.Close();
closeConnection();
}
catch
{
}
return false;
}
finally
{
if (sdr != null)
sdr.Close();
}
}
//获取Treeview的列表
//public static void GetDepList(TreeView trAccount,string strSQL)
//{
// try
// {
// DataView dvdep = GetDataView(strSQL); // int dvdepCount = dvdep.Table.Rows.Count - 1;
// int i, j, k;
// for (i = 0; i <= dvdepCount; i++)
// {
// TreeNode MainNode = new TreeNode();
// MainNode.Checked= true;
// MainNode.Text = dvdep.Table.Rows[i][0].ToString();
// MainNode.Value = dvdep.Table.Rows[i][0].ToString();
// trAccount.Nodes.Add(MainNode); // } // trAccount.DataSource = dvdep;
// trAccount.DataBind();
// }
// catch
// {
// }
// finally
// {
// if (dvdep!= null)
// {
// closeConnection();
// }
// } // }
//查找 public static string SearchValue(string strSQl)
{
SqlDataReader sdr = null;
try
{
openConnection();
comm.CommandText = strSQl;
comm.CommandType = CommandType.Text;
sdr = comm.ExecuteReader(CommandBehavior.CloseConnection);
if (sdr.Read())
{
return sdr[0].ToString();
}
else
return "";
}
catch
{
try
{
sdr.Close();
closeConnection();
}
catch
{ }
return "";
}
finally
{
if (sdr != null)
sdr.Close();
}
}
。以下是报错界面:“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------ExecuteReader: CommandText 属性尚未初始化
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Exception: ExecuteReader: CommandText 属性尚未初始化源错误:
行 171: catch (Exception e)
行 172: {
行 173: throw new Exception(e.Message);
行 174: }
行 175:
源文件: e:\WebSite\OfficeOnLine\App_Code\DBFUN.cs 行: 173 堆栈跟踪:
[Exception: ExecuteReader: CommandText 属性尚未初始化]
DBFun.GetDataView(String strSQL) in e:\WebSite\OfficeOnLine\App_Code\DBFUN.cs:173
filelist.Page_Load(Object sender, EventArgs e) in e:\WebSite\OfficeOnLine\filelist.aspx.cs:39
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061请问是哪里的错误啊? 应该怎样改呢? 小弟是菜鸟,请详细说一下 ,谢谢!
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.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Collections;
using System.IO;/// <summary>
/// DBFun 的摘要说明
/// </summary>
public class DBFun
{
protected static SqlConnection conn = new SqlConnection();
protected static SqlCommand comm = new SqlCommand();
public static void IsAdmin()
{
//查看是否为超级管理员
if (HttpContext.Current.Session["usr_id"] != null)
{
string strsql = string.Format("select usr_name from usrinfo where rank_id =5 and usr_id='{0}'", HttpContext.Current.Session["usr_id"].ToString());
DataSet ds = dataSet(strsql);
string xm; if (ds != null & ds.Tables.Count > 0 & ds.Tables[0].Rows.Count > 0)
{
xm = ds.Tables[0].Rows[0]["usr_name"].ToString();
}
else
{
HttpContext.Current.Response.Write("<script>alert('你还没有登录或登录超时!');window.location.href='login.aspx';</script>");
}
}
else
{
HttpContext.Current.Response.Write("<script>alert('你还没有登录或登录超时!');window.location.href='login.aspx';</script>");
} } public DBFun()
{
//
// TODO: 在此处添加构造函数逻辑
//
} /// 打开数据库连接
private static void openConnection()
{
if (conn.State == ConnectionState.Closed)
{
conn.ConnectionString = "Data Source=.;Initial Catalog=OfficeOnLine;User ID=sa;Password=sa";
comm.Connection = conn;
try
{
conn.Open();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
} /// 关闭当前数据库连接
private static void closeConnection()
{
if (conn.State == ConnectionState.Open)
conn.Close();
comm.Dispose();
conn.Dispose(); }
/// 执行Sql查询语句
/// <param name="sqlstr">传入的Sql语句</param>
public static void ExecuteSql(string sqlstr)
{
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
comm.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
closeConnection();
}
} /// <summary>
/// 执行Sql更新语句
/// </summary>
/// <param name="sqlstr">传入的Sql语句</param>
/// <returns>布尔值</returns>
public static bool ExecuteUpdate(string sqlstr)
{
int isUpdateOk = 0;
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
isUpdateOk = Convert.ToInt32(comm.ExecuteNonQuery());
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
closeConnection();
}
if (isUpdateOk > 0)
{
return true;
}
else
{
return false;
}
} public static DataRow GetDataRow(string strSQL)
{
//查询数据,取得数据行
try
{
openConnection();
SqlDataAdapter sda = new SqlDataAdapter(strSQL, conn);
DataSet Rs = new DataSet();
sda.Fill(Rs);
if (Rs.Tables[0].Rows.Count != 0)
return Rs.Tables[0].Rows[0];
else
return null;
}
catch
{
return null;
} }
public static DataView GetDataView(string strSQL)
{
//查询数据,取得数据视图
try
{
openConnection();
SqlDataAdapter sda = new SqlDataAdapter(strSQL, conn);
DataSet Rs = new DataSet();
sda.Fill(Rs);
return Rs.Tables[0].DefaultView; }
catch (Exception e)
{
throw new Exception(e.Message);
} } public static bool SearchTable(string strSQL)
{
SqlDataReader sdr = null;
try
{
openConnection();
comm.CommandText = strSQL;
comm.CommandType = CommandType.Text;
//当 DataReader 处于使用中时,关联的 DbConnection 正忙于为 DataReader 服务。当处于此状态时,除了关闭 Connection 外,不能对其执行其他任何操作。除非调用 DataReader 的 Close 方法,否则会一直处于此状态。如果创建了 DataReader 并将 CommandBehavior 设置为 CloseConnection,则关闭 DataReader 会自动关闭此连接。
sdr = comm.ExecuteReader(CommandBehavior.CloseConnection);
if (sdr.Read())
{ return true;
}
else
{ return false;
}
}
catch
{
try
{
sdr.Close();
closeConnection();
}
catch
{
}
return false;
}
finally
{
if (sdr != null)
sdr.Close();
}
}
//获取Treeview的列表
//public static void GetDepList(TreeView trAccount,string strSQL)
//{
// try
// {
// DataView dvdep = GetDataView(strSQL); // int dvdepCount = dvdep.Table.Rows.Count - 1;
// int i, j, k;
// for (i = 0; i <= dvdepCount; i++)
// {
// TreeNode MainNode = new TreeNode();
// MainNode.Checked= true;
// MainNode.Text = dvdep.Table.Rows[i][0].ToString();
// MainNode.Value = dvdep.Table.Rows[i][0].ToString();
// trAccount.Nodes.Add(MainNode); // } // trAccount.DataSource = dvdep;
// trAccount.DataBind();
// }
// catch
// {
// }
// finally
// {
// if (dvdep!= null)
// {
// closeConnection();
// }
// } // }
//查找 public static string SearchValue(string strSQl)
{
SqlDataReader sdr = null;
try
{
openConnection();
comm.CommandText = strSQl;
comm.CommandType = CommandType.Text;
sdr = comm.ExecuteReader(CommandBehavior.CloseConnection);
if (sdr.Read())
{
return sdr[0].ToString();
}
else
return "";
}
catch
{
try
{
sdr.Close();
closeConnection();
}
catch
{ }
return "";
}
finally
{
if (sdr != null)
sdr.Close();
}
}
。以下是报错界面:“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------ExecuteReader: CommandText 属性尚未初始化
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Exception: ExecuteReader: CommandText 属性尚未初始化源错误:
行 171: catch (Exception e)
行 172: {
行 173: throw new Exception(e.Message);
行 174: }
行 175:
源文件: e:\WebSite\OfficeOnLine\App_Code\DBFUN.cs 行: 173 堆栈跟踪:
[Exception: ExecuteReader: CommandText 属性尚未初始化]
DBFun.GetDataView(String strSQL) in e:\WebSite\OfficeOnLine\App_Code\DBFUN.cs:173
filelist.Page_Load(Object sender, EventArgs e) in e:\WebSite\OfficeOnLine\filelist.aspx.cs:39
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061请问是哪里的错误啊? 应该怎样改呢? 小弟是菜鸟,请详细说一下 ,谢谢!
检查下SqlConnection对象,用sqlhelper操作类
public static SqlConnection ReturnConn()
{
conn = new SqlConnection("");
if (conn.State.Equals(ConnectionState.Closed))
{
conn.Open();
}
return conn;
}