using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace TestSql
{
class Program
{
static void Main(string[] args)
{
//数据库字符串
string CSql="Data Source=aabbccddeeff-PC\\aabbccddeeff;Initial Catalog=QQ;User ID=qq;Password=qq";
SqlConnection sqlc=new SqlConnection(CSql);//创建数据库对象
//打开数据库
sqlc.Open();
Console.WriteLine("打开数据库成功,正在读取数据……");
SqlCommand sqlcoma = new SqlCommand();
sqlcoma.CommandText = "select * from qquser";
sqlcoma.Connection = sqlc;
SqlDataReader reader = sqlcoma.ExecuteReader();//执行SQL语句并且返回一个读取数据的对象
while(reader.Read())//判断是否读取了一行数据,结果为真
{
Console.Write(reader["qqid"]+" "+reader["password"]+" "+reader["lastlogtime"]+" "+reader["online"]+" "+reader["level"]);
Console.WriteLine();
}
reader.Close();//关闭读取源
//关闭数据库
sqlc.Close();
Console.WriteLine("关闭数据库成功");
Console.ReadKey();
}
}
}