| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2076 人关注过本帖
标题:处理数据时,DataReader问题
只看楼主 加入收藏
小孔
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2016-2-25
结帖率:100%
收藏
 问题点数:0 回复次数:3 
处理数据时,DataReader问题
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace 查询
{
    public partial class Form1 : Form
    {
        OleDbConnection conn = new OleDbConnection();
        OleDbCommand comm = new OleDbCommand();
        OleDbDataReader dr;
        public Form1()
        {
            InitializeComponent();
            conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=studentinfo.mdb";
            conn.Open();
            comm.Connection = conn;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
               
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            string strcomm, strinfo;
            if (r1.Checked)
                strcomm = "select * from studentinfo where '姓名' like '%" + textBox1.Text + "%'";
            else
                strcomm = "select * from studentinfo where '学号' like '%" + textBox1.Text + "%'";
             = strcomm;
            dr = comm.ExecuteReader();
            strinfo = "";
            for (int i = 0; i <= dr.FieldCount - 1; i++)
                strinfo += dr.GetName(i) + " ";
            strinfo += "\n";
            while (dr.Read())
            {
                for (int i = 0; i <= dr.FieldCount - 1; i++)
                    strinfo += dr[i] + " ";
                strinfo += "\n";
                if (dr.HasRows)
                    MessageBox.Show(strinfo, "查询结果");
                else
                    MessageBox.Show("无匹配记录", "查询结果");
                dr.Close();
            }
        }
    }
}
搜索更多相关主题的帖子: public 
2016-02-25 16:05
小孔
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2016-2-25
收藏
得分:0 
是在    dr=comm.ExecuteReader();这出现问题。问题是:已有打开的与此命令相关联的 DataReader,必须首先将它关闭
2016-02-25 16:07
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:0 
程序代码:
namespace 查询
{
    public partial class Form1 : Form
    {
        OleDbConnection conn = new OleDbConnection();
        OleDbCommand comm = new OleDbCommand();
        OleDbDataReader dr;
        
        public Form1()
        {
            InitializeComponent();
            
            conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=studentinfo.mdb";
            //conn.Open();
            comm.Connection = conn;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            //   
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            string strcomm, strinfo;
            
            if (r1.Checked)
                strcomm = "select * from studentinfo where '姓名' like '%" + textBox1.Text + "%'";
            else
                strcomm = "select * from studentinfo where '学号' like '%" + textBox1.Text + "%'";
            
             = strcomm;
            coon.open();  //这样子看看行不行,赋值后再打开连接
            dr = comm.ExecuteReader();
            //
        }
    }
}

   唯实惟新 至诚致志
2016-02-25 16:46
小孔
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2016-2-25
收藏
得分:0 
回复 3楼 qq1023569223
不行
2016-02-25 17:27
快速回复:处理数据时,DataReader问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018237 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved