求~~~通过SQLDataReader读取数据的过程

来源:百度知道 编辑:UC知道 时间:2024/05/16 13:31:50
有么有人知道啊
要求 写出类名 函数名 大致框架就好了~~~
..大家帮帮我吧~~~~~
俺给一个老师的程序...是参考程序
public static void ExecuteSql(string source)
{
string select = "SELECT ContactName,CompanyName FROM Customers";

try
{
using (SqlConnection conn = new SqlConnection(source))
{
conn.Open();
SqlCommand cmd = new SqlCommand(select, conn);
using (SqlDataReader reader = cmd.ExecuteReader())
{
Console.WriteLine("*** SqlProvider ***");
Console.WriteLine("Output from direct SQL statement...");
Console.WriteLine();
Console.WriteLine("CONTACT COMPANY");
Console.WriteLine("--------------------------------------");

while (reader.Read())
{
Console.WriteLine("{0,-30} {1}", reader[0], reader[1]);<

//...前面省略
using System.Data.SqlClient; //1:引入命名空间

namespace Test
{
public partial class Form1 : Form
{
static string conStr = "server=.; database = myqq; uid=sa; pwd=0000;"; //2: 定义连接字符串
SqlConnection con = new SqlConnection(conStr); //3: 定义数据库连接对象

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
string sql = "select * from Users"; //要查询的sql语句
SqlCommand cmd = new SqlCommand(sql, con); //4:创建sqlcommand对象, 执行sql语句
con.Open(); //5: 打开数据库连接
SqlDataReader dr = cmd.ExecuteReader(); //6: 执行sql语句并返回 sqldatareader对象 <相当赋值>
while (dr.Read()) //7:利用sqldatareader读取数据
{
MessageBox.Show(&quo