“DBClass”不包含“Close”的定义,并且找不到可接受类型为“DBClass”的第一个参数的扩展方法“Close”

新手求 问题解答。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public static bool Login(string strName, string StrPassword) //验证用户信息是否合法
{
string conn = System.Configuration.ConfigurationSettings.AppSettings["datac"].ToString();
bool l_Return;
DBClass db = new DBClass(conn);
try
{
SqlDataReader dr = db.GetSqlDataReader("selcet*from admin where adminname='" + strName + "'and adminPWD='" + StrPassword + "'");
if (dr.Read())
{
HttpContext.Current.Session["Userid"] = "admin";
HttpContext.Current.Response.Redirect(HttpContext.Current.Request.ApplicationPath.TrimEnd('/') + "/index.aspx");
}
dr.Close();
if (HttpContext.Current.Session["Userid"] != null)
{ l_Return = true; }
else
{ l_Return = false; }
}
finally
{ db.Close(); } //出问题处!!!!
return (l_Return);
}

这是语法错误,估计你也是新手。首先,先定位到DBClass上,右键转到定义,看下有没有Close这个公开方法,在这里,我觉得这个Close方法是关闭数据库的链接然后释放资源的。找你的说法肯定是找不到这个Close方法的。如果你能看的懂源代码,就自己在写上Close方法,就释放资源。要么,就把finally语句块去掉。。追问

确实是关闭数据库的链接然后释放资源的, 若把finally句块去掉 不会使内存越来越小吗
去掉后,
return (l_Return);处 错误 CS1524: 应输入 catch 或 finally

倘若自己写Close方法 怎么写

追答

无非就是Connection,DataAdapeter,DataSet,Command,DataReader这些对象需要Dispose掉,
找到这些对象
public Close()
{
if(Connetion!=null)
Connetion.Dispose();

if(.....!=null)
。。。.Dispose()
以此类推,找到上述的那些对象逐一释放资源
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-03-25
DBClass 这个类没有 Close()方法,或者该方法不是public共有类,或者该方法为static静态,等
相似回答