c#中读取远程数据
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication82
{
class Program
{
static void Main(string[] args)
{
[color=#DC143C]SqlConnection thisconnetion = new SqlConnection(@"Server=0.0.0.0;User=sa;PWD=sa;" + "Database=Test");[/color]
thisconnetion.Open();
SqlCommand thisCommand = thisconnetion.CreateCommand();
thisCommand.CommandText = "SELECT Name,Age From tb_Test";
SqlDataReader thisReader = thisCommand.ExecuteReader();
while (thisReader.Read())
{
Console.WriteLine("\t{0}\t{1}\a\v",thisReader["Name"],thisReader["Age"]);
[color=#0000FF] // [/color] }
[color=#FF0000] thisReader.Close();
thisconnetion.Close();
Console.ReadKey();[/color]
}
}
}
如果红色部分放在蓝色位置则会提示[color=#008000]阅读器关闭时 read无效[/color]
原因在于红色部分在蓝色时会在阅读出第一个字段后就将数据库关闭 从而使后边的阅读无法进行!


