OleDbDataReader sdr = cmd.ExecuteReader();取值问题

kcalxy 2012-10-04 08:06:55
代码如下,while()时sdr只显示最后一条,if()时只显示第一条。怎么才能显示全部啊 ,着急,在线等。高手们帮帮忙!谢谢。

string ses = Session["name"].ToString();
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(ConfigurationManager.AppSettings["OleConnectionString"].ToString());
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();

string qk = "select xm from tb_Teacher where jgh ='" + ses + "'";
OleDbCommand cmdqk = new OleDbCommand(qk, conn);
cmdqk.CommandType = CommandType.Text;
OleDbDataReader drqk = cmdqk.ExecuteReader();
while (drqk.Read())
{
string jsxm = drqk["xm"].ToString();
string sqlxh = "select xh from tb_Student where zdjs='" + jsxm + "'";

OleDbCommand cmdxh = new OleDbCommand(sqlxh, conn);

OleDbDataReader sdrxh = cmdxh.ExecuteReader();

if (sdrxh != null)
{
while (sdrxh.Read())
{
string xsxh = sdrxh["xh"].ToString();

string sqljg = "select * from tb_design where xh='" + xsxh + "'";
if (!this.ddl_bysj.SelectedValue.Equals("0"))
sqljg += " and bysj='" + ddl_bysj.Text + "'";
if (!this.ddl_zy.SelectedValue.Equals("0"))
sqljg += " and zy='" + ddl_zy.Text + "'";
if (!this.ddl_bj.SelectedValue.Equals("0"))
sqljg += "and bj='" + ddl_bj.Text + "'";

OleDbCommand cmd = new OleDbCommand(sqljg, conn);

OleDbDataReader sdr = cmd.ExecuteReader();

Repeater1.DataSource = sdr;
Repeater1.DataBind();
sdr.Close();
}
}
sdrxh.Close();
}

drqk.Close();
conn.Close();
...全文
431 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
kcalxy 2012-10-04
  • 打赏
  • 举报
回复
大家看明白我贴的代码了吗?
kcalxy 2012-10-04
  • 打赏
  • 举报
回复
就是因为向前,所以到了最后一条不向前了,才只显示了最后一条,我想要while (sdrxh.Read())
显示全部条数绑定到repeter上,怎么做啊?
{
string xsxh = sdrxh["xh"].ToString();

string sqljg = "select * from tb_design where xh='" + xsxh + "'";
//if (!this.ddl_bysj.SelectedValue.Equals("0"))
//sqljg += " and bysj='" + ddl_bysj.Text + "'";
//if (!this.ddl_zy.SelectedValue.Equals("0"))
//sqljg += " and zy='" + ddl_zy.Text + "'";
//if (!this.ddl_bj.SelectedValue.Equals("0"))
//sqljg += "and bj='" + ddl_bj.Text + "'";

OleDbCommand cmd = new OleDbCommand(sqljg, conn);

OleDbDataReader sdr = cmd.ExecuteReader();

Repeater1.DataSource = sdr;
灿烂阳光168 2012-10-04
  • 打赏
  • 举报
回复
while (sdrxh.Read())

表示向前移动一行,如果有记录,那么执行循环。
kcalxy 2012-10-04
  • 打赏
  • 举报
回复
我的问题大家明白吗?
kcalxy 2012-10-04
  • 打赏
  • 举报
回复
对的,用到的表比较多,环境有点复杂。就想知道OleDbDataReader这种方法怎么解决问题。
暖枫无敌 2012-10-04
  • 打赏
  • 举报
回复
你双层循环啊?

就是将数据绑定到Repeater上,搞这么麻烦!
using System; using System.Data; using System.Configuration; using System.Collections; 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; public partial class userShow : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { this.Label1.Text = System.DateTime.Now.ToString(); loadData(); } protected void Button2_Click(object sender, EventArgs e) { OleDbConnection conn; OleDbCommand cmd; string olecmd, oleString,value; value = this.TextBox1.Text; Session.Add("key", value); oleString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("news.mdb"); olecmd = "select * from userLoad where userName='"; olecmd += TextBox1 .Text.Trim(); olecmd += "'and userPassword='"; olecmd += TextBox2 .Text.Trim() + "'"; conn = new OleDbConnection();//实例化对象 conn.ConnectionString = oleString; cmd = new OleDbCommand(); cmd.Connection = conn; cmd.CommandText = olecmd; conn.Open(); OleDbDataReader dr = cmd.ExecuteReader(); if (this.TextBox1.Text.Trim() == "" || this.TextBox2.Text.Trim() == "") { Response.Redirect("usersub.aspx"); } if (dr.Read()) { Session["username"] = this.TextBox1.Text; Response.Redirect("todayNews.aspx"); } else { this.TextBox1 .Focus(); this.TextBox1 .Text = ""; this.TextBox2 .Text = ""; } conn.Close(); dr.Close(); } protected void Button3_Click(object sender, EventArgs e) { this.TextBox1.Focus(); this.TextBox1.Text = ""; this.TextBox2.Text = ""; } DataSet GetDataSet() { OleDbConnection conn; OleDbCommand cmd; conn = new OleDbConnection(); conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("news.mdb"); conn.Open(); cmd = new OleDbCommand(); cmd.CommandText = "select * from newsData where newsclassid=0"; cmd.Connection = conn; OleDbDataAdapter oleDA = new OleDbDataAdapter(); oleDA.SelectCommand = cmd; DataSet ds = new DataSet(); oleDA.Fill(ds, "newsData"); conn.Close(); return ds; } void loadData() { DataSet ds = GetDataSet(); GridView1.DataSource = ds; GridView1.DataBind(); } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { Response.Redirect("userLogin.aspx"); } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; loadData(); } }
用ASP.NET做的 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; /// /// Class1 的摘要说明 /// public class Class1 { private OleDbConnection m_Connection; private String m_brChar = "\n"; public Class1() { // // TODO: 在此处添加构造函数逻辑 // } private void ExecuteCommand(OleDbCommand cmd) { cmd.Connection=m_Connection; try { m_Connection.Open(); cmd.ExecuteNonQuery(); } finally { m_Connection.Close(); } } public DataSet GetNotes(DateTime fromDate, DateTime toDate) { DataSet ds=new DataSet(); OleDbDataAdapter da=new OleDbDataAdapter("select * from Notes where DateTime BETWEEN ? and ? ORDER BY DateTime DESC",m_Connection); da.SelectCommand.Parameters.Add("FromDate", OleDbType.Date).Value = fromDate.AddDays(1); m_Connection.Open(); da.Fill(ds,"MyNotes"); m_Connection.Close(); return ds; } public void InsertNote(string title,string contents) { OleDbCommand cmd =new OleDbCommand ("insert into Notes(Title,Contents) values(?,?)"); cmd.Parameters .Add ("Title",OleDbType .VarChar ).Value =title ; cmd.Parameters .Add ("Contents",OleDbType .LongVarChar ).Value =contents.Replace (m_brChar ,"
"); ExecuteCommand (cmd); } public void InsertComment(int noteid,string author,string email,string comment) { OleDbCommand cmd=new OleDbCommand ("insert into Comments(NoteID,Author,Email,Comment) values(?,?,?,?)"); cmd.Parameters .Add ("NoteID",OleDbType .VarChar ).Value =noteid; cmd.Parameters .Add ("Author",OleDbType .VarChar ).Value =author ; cmd.Parameters .Add ("Email",OleDbType .VarChar ).Value =email ; cmd.Parameters .Add ("Comment",OleDbType .LongVarChar ).Value =comment.Replace (m_brChar ,"
"); ExecuteCommand (cmd); } public void UpdaeNote(int noteid, string title, string contents) { OleDbCommand cmd = new OleDbCommand("UPDATE [Notes] SET [Title]=?,[Contents]=? WHERE [NoteID]=?"); cmd.Parameters.Add("Title", OleDbType.VarChar).Value = title; cmd.Parameters.Add("Contents", OleDbType.LongVarChar).Value = contents.Replace(m_brChar, "
"); cmd.Parameters.Add("NoteID", OleDbType.Integer).Value = noteid; ExecuteCommand(cmd); } public void UpdateComment(int commentid,string author,string email,string comment) { OleDbCommand cmd =new OleDbCommand ("UPDATE Comments SET Author =?,Email=?,Comment=? WHERE CommentID=?"); cmd.Parameters .Add ("Author",OleDbType .VarChar ).Value =author; cmd.Parameters .Add ("Comment",OleDbType .LongVarChar).Value =comment.Replace (m_brChar ,"
"); cmd.Parameters.Add("Email", OleDbType.VarChar).Value = email; cmd.Parameters.Add("CommentID", OleDbType.VarChar).Value = commentid; ExecuteCommand(cmd); } public void DeleteNote(int noteid) { OleDbCommand cmd =new OleDbCommand ("delete from Notes where NoteID="+noteid); ExecuteCommand(cmd); } public void DeleteComment(int commentid) { OleDbCommand cmd =new OleDbCommand ("delete from Comments where CommentID="+commentid); ExecuteCommand (cmd); } public void GetNoteData(int noteid,ref string title,ref string contents,ref string post_time) { OleDbCommand cmd=new OleDbCommand ("select * from Notes where NoteID="+ noteid ,m_Connection ); try { m_Connection .Open(); OleDbDataReader reader=cmd.ExecuteReader (CommandBehavior .CloseConnection ); if(reader .Read ()) { title =reader ["Title"].ToString (); contents =reader ["Contents"].ToString ().Replace ("
",m_brChar ); post_time =reader ["DataTime"].ToString (); reader .Close (); } } finally { m_Connection .Close (); } } public void GetCommentData(int commentid,ref string author,ref string email,ref string comment) { OleDbCommand cmd=new OleDbCommand ("select * from Comments where CommentID="+commentid ,m_Connection ); try { m_Connection .Open (); OleDbDataReader reader=cmd.ExecuteReader (CommandBehavior .CloseConnection ); if(reader.Read ()) { author =reader ["Author"].ToString (); email =reader ["Comment"].ToString ().Replace ("
",m_brChar ); reader .Close (); } } finally { m_Connection .Close (); } } } /// /// data 的摘要说明 ///

62,244

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧