您的位置:首页 > 数据库

SqlDataReader读取数据

2008-11-21 15:22 405 查看
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Data;

using System.Data.SqlClient;

namespace ConsoleApplication11

{

class Program

{

static void Main(string[] args)

{

SqlConnection conn = new SqlConnection("Server=zhuobin;uid=sa;pwd=zhuobin;database=northwind");

string sql = @"select contactname from customers";

try

{

conn.Open();

//create the command

SqlCommand cmd = new SqlCommand(sql,conn);

SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())

{

Console.WriteLine("{0}",reader[0]);

} reader.Close();

}

catch (SqlException ex)

{

Console.WriteLine("The error:" + ex.Message);

}

finally

{

conn.Close();

Console.WriteLine("The end!");

}

Console.ReadLine();

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐