数据访问:——ADO.NET
1.创建链接2.创建与执行命令——SQL3.读取或准备相关数据一、命名空间:using System.Data;using System.Data.SqlClient;二、链接类 - SqlConnection——创建与数据库的链接(一)链接字符串: 连哪台电脑,连电脑上的哪个数据库,用户名是什么,密码server=目标服务器的IP地址;database=数据库名;uid=数据库的登录名;pwd=数据库的登录密码server=.;(代表本机)database=mydb;uid=sa;pwd=123(二)实例化——构造SqlConnection conn = new SqlConnection();SqlConnection conn = new SqlConnection(链接字符串);(三)属性ConnectionString:(string)指定与获取链接字符串。State:用来描述链接当前的状态。Closed-链接处于关闭状态。Open-链接处于打开状态。(四)方法Open():打开链接Close():关闭链接。(五)案例1.定义链接字符串。string connectionString = "server=.;database=mydb;uid=sa;pwd=123";2.构造链接对象SqlConnection conn = new SqlConnection(connectionString);或SqlConnection conn = new SqlConnection();conn.ConnectionString = connectionString;3.打开链接conn.Open();4.关闭链接conn.Close();三、命令类——SqlCommand——向数据传递SQL语句或存储过程,并执行。(一)构造:SqlCommand cmd = new SqlCommand();SqlCommand cmd = conn.CreateCommand();(二)属性:Connection:(SqlConnection)指定通过哪个链接对象来操作数据库CommandText:(string)要执行的SQL语句或存储过程名.(三)方法:ExecuteNonQuery()——执行命令,返回影响行数。一般用来执行 增删改 的语句。ExecuteReader() ——执行命令,返回一个读取器对象。一般用来执行 查询 语句。(四)案例: string connectionString = "server=.;database=mydb;uid=sa;pwd=123"; SqlConnection conn = new SqlConnection(connectionString); conn.Open(); //操作数据库 SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "insert into student values('s001','张三','1','清华大学','1990-6-1')"; cmd.ExecuteNonQuery(); conn.Close();四、读取器类:——SqlDataReader——从查询的结果集中把数据逐个读出来。(一)构造:只有唯一的一种构造方式。再没有别的构造方式了。SqlDataReader dr = cmd.ExecuteReader();说明:只读,只向前,读取器工具。内存中只占一条记录的空间。(二)属性:HasRows : bool,判断是否有数据可读。是否查出数据来了。(三)方法:Read():bool。把结果集中当前行读取内存的datareader中来。读来了,返回true。没有数据可读,返回false。在使用SqlDataReader读取某列数据时,必须先使用Read()方法把数据先取到内存中的SqlDataReader中。最常用的读取句式: while (dr.Read()) { Console.WriteLine(dr[0].ToString() + dr[1].ToString() + dr[2].ToString() + dr[3].ToString() + dr[4].ToString()); }从SqlDataReader中读取某列值的时候,使用方法:1.dr[列的下标/列的索引号]2.dr["列名"]3.dr.GetInt(索引号) dr.GetString(索引号) ...(四)案例:1.登录。//SQL代码create table Login(Username varchar(50) primary key,Password varchar(50))goinsert into Login values('aaa','aaa')insert into Login values('bbb','aaa')insert into Login values('ccc','aaa')insert into Login values('ddd','aaa')insert into Login values('eee','aaa')//vs代码 static void Main(string[] args) { Console.Write("登陆:"); string uid=Console.ReadLine(); Console.Write("密码:"); string pwd = Console.ReadLine(); //操作数据库 SqlConnection conn = new SqlConnection("server=.;database=mydb;uid=sa;pwd=5587725"); conn.Open(); SqlCommand comm = conn.CreateCommand(); comm.CommandText = "select * from Login where Username='"+uid+"'and Password='"+pwd+"'"; //查询 SqlDataReader dr = comm.ExecuteReader(); //生成 读取器对象 if (dr.HasRows) { Console.WriteLine("正确"); } else { Console.WriteLine("错误"); } conn.Close(); Main(args); }2.读取显示表中的所有的数据。3.完善插入功能,在插入之前,判断一下学号是否已存在。如果已存在就提示报名,不存在就插入进去。查询练习:class Program { //将登陆的信息设定成常量,常量一般用大写。 public const string CONNECTIONSTRING = "server=.;database=mydb;uid=sa;pwd=5587725"; //民族 static string Getnationname(string code) { string str = ""; SqlConnection conn = new SqlConnection(CONNECTIONSTRING); conn.Open(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select * from Nation where code='"+code+"'"; SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { dr.Read(); str = dr["Name"].ToString(); } else { str = "未知"; } conn.Close(); return str; } //工作简历 static string GetWorks(string code) { string str=""; SqlConnection conn = new SqlConnection(CONNECTIONSTRING); conn.Open(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select * from work where InfoCode='"+code+"'"; SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { str += ((DateTime)dr["StartDate"]).ToString("yyyy年MM月dd日")+"\t"; str += ((DateTime)dr["EndDate"]).ToString("yyyy年MM月dd日") + "\t"; str += dr["Firm"].ToString() + "\t"; str += dr["Depart"].ToString() + "\n"; } conn.Close(); return str; } // 家庭称谓 static string Getfamily(string title) { string str = ""; SqlConnection conn = new SqlConnection(CONNECTIONSTRING); conn.Open(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select * from title where code='"+title+"'"; SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { dr.Read(); str = dr["name"].ToString(); } else { str = ""; } conn.Close(); return str; } //家庭关系 static string GetFamily(string code) { string str=""; SqlConnection conn = new SqlConnection(CONNECTIONSTRING); conn.Open(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select * from family where infocode='"+code+"'"; SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { str += dr["Name"].ToString()+"\t"; str += Getfamily(dr["title"].ToString())+"\t"; str += dr["firm"].ToString()+"\n"; } conn.Close(); return str; } static void Main(string[] args) { string str = ""; //显示 SqlConnection conn = new SqlConnection(CONNECTIONSTRING); conn.Open(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select * from info"; SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { string code = dr["Code"].ToString(); string name = dr["Name"].ToString(); string sex = ((bool)dr["Sex"])?"男":"女"; string nation = Getnationname(dr["Nation"].ToString()); string birthday = ((DateTime)dr["Birthday"]).ToString("yyyy年MM月dd日"); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(code+"\t"+name+"\t"+sex+"\t"+nation+"\t"+birthday); Console.ResetColor(); //显示工作简历 Console.WriteLine("******************工作简历**************************"); Console.WriteLine(GetWorks(code)); //显示家庭情况 Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("******************工作简历**************************"); Console.WriteLine(GetFamily(code)); Console.ResetColor(); } Console.ReadLine(); conn.Close(); } }