注册 登录
编程论坛 C# 论坛

处理数据时,DataReader问题

小孔 发布于 2016-02-25 16:05, 2190 次点击
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();
            }
        }
    }
}
3 回复
#2
小孔2016-02-25 16:07
是在    dr=comm.ExecuteReader();这出现问题。问题是:已有打开的与此命令相关联的 DataReader,必须首先将它关闭
#3
qq10235692232016-02-25 16:46
程序代码:
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();
            //
        }
    }
}
#4
小孔2016-02-25 17:27
回复 3楼 qq1023569223
不行
1