program.cs
来自「c#开发宝典 光盘内容。本光盘主要为书中的源程序」· CS 代码 · 共 47 行
CS
47 行
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace Example20
{
class Program
{
static void Main(string[] args)
{
//定义连接字符串
string connectionString = @"Data Source=WWW-C4074C4DF06\SQLEXPRESS;Initial Catalog=Chap20;Integrated Security=True;Pooling=False";
//使用连接字符串建立连接
SqlConnection myConnection = new SqlConnection(connectionString);
//打开连接
myConnection.Open();
//定义SqlCommand变量
SqlCommand myCommand = myConnection.CreateCommand();
//设置SQL语句
myCommand.CommandText = "select * from student";
//从数据库获取数据
SqlDataReader myDataReader = myCommand.ExecuteReader();
//读取DataReader中的数据并输出
while (myDataReader.Read())
{
Console.WriteLine("\t{0}\t{1}", myDataReader["ID"], myDataReader["Name"]);
}
//关闭SqlDataReader
myDataReader.Close();
//关闭连接
myConnection.Close();
Console.ReadLine();
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?