处理数据时,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();
}
}
}
}