类调用及数据库显示问题!------在线等!!
我写有一个类文件名为:userDatabase.cs 其中的代码如下:
using System;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Xml;
using System.IO;
using System.Data.Common;
namespace lzcomputer.UserOperate
{
/// <summary>
/// userDatabase 的摘要说明。
/// </summary>
public class userDatabase
{
public userDatabase()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
}
/// <summary>
/// 基类
/// </summary>
public class Sql_Base
{
string strConn;
public string StrConn
{
get{return strConn;}
}
///<summary>
///构造函数没有字符串
///</summary>
public Sql_Base()
{
strConn=ConfigurationSettings.AppSettings["Connection"];
}
///<summary>
///构造函数
/// </summary>
/// <param name="strconn">连接串</param>
public Sql_Base(string strconn)
{
strConn=strconn;
}
/// <summary>
/// 用于执行目录操作(例如查询数据库的结构或创建诸如表等的数据库对象)
/// 或通过执行 UPDATE、INSERT 或 DELETE 语句
/// 虽然 ExecuteNonQuery 不返回任何行,但映射到参数的任何输出参数或返回值都会用数据进行填充。
/// </summary>
/// <param name="strSql">SQL语句</param>
/// <returns>对于 UPDATE、INSERT 和 DELETE 语句,返回值为该命令所影响的行数。对于其他所有类型的语句,返回值为 -1。</returns>
public int ExecuteNonQuery(string strSql)
{
int ret=0;
SqlConnection objConn=new SqlConnection(strConn);
SqlCommand objComm=new SqlCommand(strSql,objConn);
try
{
objConn.Open ();
ret=objComm.ExecuteNonQuery ();
}
catch(Exception)
{
ret=0;
}
finally
{
objConn.Close ();
}
return ret;
}
/// <summary>
/// 用于判断某条查询语句是否有记录
/// </summary>
/// <param name="strSql"></param>
/// <returns>如果有则返回true,否则返回false。</returns>
public bool ExecuteReader(string strSql)
{
SqlDataReader objDReader=null;
bool ret=false;
SqlConnection objConn=new SqlConnection (StrConn);
SqlCommand objCmd=new SqlCommand (strSql,objConn);
try
{
objConn.Open ();
objDReader=objCmd.ExecuteReader ();
if(objDReader.Read ())ret=true;
}
catch(Exception)
{
}
finally
{
objConn.Close ();
}
return ret;
}
}
/// <summary>
/// 附件信息类
/// </summary>
public class accessobj:Sql_Base
{
private string strSql;
public bool Setnewtext()
{
strSql="select * form newtext";
return ExecuteReader(strSql);
}
}
}
在其中另一窗体的.cs个文件是下是using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO ;
using lzcomputer.UserOperate;
namespace lzcomputer
{
/// <summary>
/// indexbottom 的摘要说明。
/// </summary>
public class indexbottom : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.ImageButton ImageButton1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button3;
protected System.Web.UI.WebControls.Button Button4;
protected System.Web.UI.WebControls.TextBox TextBox4;
protected System.Web.UI.WebControls.TextBox TextBox5;
protected System.Web.UI.WebControls.ImageButton ImageButton2;
protected System.Web.UI.WebControls.ImageButton ImageButton3;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Button Button6;
protected System.Web.UI.WebControls.Button Button5;
protected System.Web.UI.WebControls.Label Label1;
private void Page_Load(object sender, System.EventArgs e)
{
this.Label1.Text=System.DateTime.Now.Year.ToString() +"年" + System.DateTime.Now.Month.ToString() + "月" + System.DateTime.Now.Day.ToString() +"日" + System.DateTime.Now.DayOfWeek.ToString();
// 在此处放置用户代码以初始化页面
accessobj objconn = new accessobj();
bool ret=objconn.Setnewtext();
Response.Write (ret);
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
现在问题是我想把SQL语句查询出来的一些字段的值放到比如说Label1.text中,不过如何去实现,再一上面的类调用是否正确和所有的代码应该怎么写呢??
问题点数:60、回复次数:12Top
1 楼trainli(男人要有霸气得有钱)回复于 2006-03-04 16:04:40 得分 0
谁给我指点一下,刚学,菜菜的,多多指点呀!Top
2 楼trainli(男人要有霸气得有钱)回复于 2006-03-04 16:43:28 得分 0
为什么呀??要下班了,怎么都没有人回答呀??Top
3 楼trainli(男人要有霸气得有钱)回复于 2006-03-05 16:18:27 得分 0
郁闷
Top
4 楼Lcindep110(Descovering YourSelf)回复于 2006-03-05 16:29:03 得分 50
///这个方法应该返回SqlDataReader吧
public SqlDataReader ExecuteReader(string strSql)
{
SqlDataReader objDReader=null;
SqlConnection objConn=new SqlConnection (StrConn);
SqlCommand objCmd=new SqlCommand (strSql,objConn);
objConn.Open ();
objDReader=objCmd.ExecuteReader ();
objConn.Close ();
return objDReader;
}
public class accessobj:Sql_Base
{
private string strSql;
public SqlDataReader Setnewtext()
{
strSql="select * form newtext";
return ExecuteReader(strSql);
}
}
}
在页面就可以用
accessobj objconn = new accessobj();
SqlDataReader reader = objconn.Setnewtext();
while(reader.Read())
{
Label1.Text = Convert.ToString(reader[columnName]);
}Top
5 楼nameone(过客)回复于 2006-03-05 16:31:40 得分 0
UPTop
6 楼trainli(男人要有霸气得有钱)回复于 2006-03-05 17:26:11 得分 0
“/lzcomputer”应用程序中的服务器错误。
--------------------------------------------------------------------------------
阅读器关闭时 Read 的尝试无效。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.InvalidOperationException: 阅读器关闭时 Read 的尝试无效。
源错误:
行 50: accessobj objconn = new accessobj();
行 51: SqlDataReader reader = objconn.Setnewtext();
行 52: while(reader.Read())
行 53: {
行 54: Label2.Text = Convert.ToString(reader["wherenew"]);
源文件: c:\inetpub\wwwroot\lzcomputer\indexbottom.aspx.cs 行: 52
堆栈跟踪:
[InvalidOperationException: 阅读器关闭时 Read 的尝试无效。]
System.Data.SqlClient.SqlDataReader.Read()
lzcomputer.indexbottom.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\lzcomputer\indexbottom.aspx.cs:52
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()
Top
7 楼levin9(生活就是強姦,習慣啦就是享受)回复于 2006-03-05 20:44:27 得分 5
这是返回datareader的实例
public SqlDataReader GetDataReaderSql(string CommandText)
{
//sql="select * from goods";
SqlDataReader dr=null;
dr=CreateCommand(CommandText).ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}Top
8 楼Lcindep110(Descovering YourSelf)回复于 2006-03-05 21:13:21 得分 0
while(reader.Read())
{
Label2.Text = Convert.ToString(reader["wherenew"]);
}
//少加了这句reader.Close();Top
9 楼ljhkim6()回复于 2006-03-05 21:15:45 得分 5
public SqlDataReader GetDataReaderSql(string CommandText)
{
//sql="select * from goods";
SqlDataReader dr=null;
return CreateCommand(CommandText).ExecuteReader(CommandBehavior.CloseConnection);;
}
Top
10 楼trainli(男人要有霸气得有钱)回复于 2006-03-06 08:25:18 得分 0
public SqlDataReader ExecuteReader(string strSql)
{
SqlDataReader objDReader=null;
SqlConnection objConn=new SqlConnection (StrConn);
SqlCommand objCmd=new SqlCommand (strSql,objConn);
objConn.Open ();
objDReader=objCmd.ExecuteReader ();
objConn.Close ();
return objDReader;
}
在这个方法里不是已经有objConn.Close();关闭了吗??/
还要在那里关闭吗??Top
11 楼trainli(男人要有霸气得有钱)回复于 2006-03-06 09:14:35 得分 0
while(reader.Read())
{
Label2.Text = Convert.ToString(reader["wherenew"]);
}
//少加了这句reader.Close();
,这个我已经加上了还是一样的提示不能关闭数据库!Top
12 楼trainli(男人要有霸气得有钱)回复于 2006-03-06 10:16:53 得分 0
why??Top




