ADO.net:
数据库访问技术
一、查询显示
引用命名空间: using System.Data.SqlClient;
1、连接数据库(数据库连接类)
//连接数据库 ------ SqlConnection SqlConnection conn = new SqlConnection("server=.;database=Data0425;user=sa;pwd=123;"); //server=服务器;database=数据库名;user=账号;pwd=密码;
2、数据库操作(数据库操作类)
//操作 数据库 -------SqlCommand SqlCommand cmd = conn.CreateCommand(); //操作Conn所连接的数据库 cmd.CommandText = "select * from Student";
3、数据库数据读取(数据库读取类)
//数据库连接通道开启 conn.Open(); //数据读取类 -----SqlDataReader SqlDataReader dr = cmd.ExecuteReader(); //读取cmd所操作的数据库 //if (dr.HasRows) //是否读取到数据 (ture) //{ // while (dr.Read()) // { // Console.WriteLine("学号:" + dr[0] + ",姓名:" + dr[1] + ",性别:" + (((bool)dr[2]) ? "男" : "女") + ",生日:" + Convert.ToDateTime(dr[3]).ToString("yyyy年MM月dd日") + ",成绩:" + ((decimal)dr[4]).ToString("#.##")); // } //} //个人的 年龄 if (dr.HasRows) { while (dr.Read()) { DateTime dq = DateTime.Now; //当前时间 ////string a = dq.ToString("yyyy年MM月dd日"); ////int a1 = int.Parse(a.Substring(0, 4)); //当前年份 int a1 = dq.Year; //string shr = Convert.ToDateTime(dr[3]).ToString("yyyy年MM月dd日"); //int a2 = int.Parse(shr.Substring(0, 4)); //生日的年份 int a2 = Convert.ToDateTime(dr[3]).Year; int age = a1 - a2; int age1 = DateTime.Now.Year - Convert.ToDateTime(dr[3]).Year; Console.WriteLine("姓名:" + dr[1] + "年龄:" + age); } } //数据库连接通道关闭 ---------------- 一定要关闭!!!!!! conn.Close();
二、添加数据
cmd.ExecuteNonQuery();
//添加数据 //1、获取用户输入的信息 Console.Write("请输入学生编号:"); string Scode = Console.ReadLine(); Console.Write("请输入学生姓名:"); string Sname = Console.ReadLine(); Console.Write("请输入学生性别:"); bool Ssex = Convert.ToBoolean(Console.ReadLine()); Console.Write("请输入学生生日:"); DateTime Sbirthday = Convert.ToDateTime(Console.ReadLine()); Console.Write("请输入学生成绩:"); decimal Sscore = Convert.ToDecimal(Console.ReadLine()); //2、开启数据库连接,并且执行数据库操作 SqlConnection conn = new SqlConnection("server=.;database=Data0425;user=sa;pwd=123;"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "insert into Student values('" + Scode + "','" + Sname + "','" + Ssex + "','" + Sbirthday + "'," + Sscore + ")"; conn.Open(); cmd.ExecuteNonQuery(); Console.WriteLine("添加成功!"); conn.Close(); Console.ReadLine();
三、修改数据
cmd.ExecuteNonQuery();
//1、让用户输入要修改的学生编号 Console.Write("请输入要修改的学生编号:"); string Scode = Console.ReadLine(); Console.Write("请输入更改后的学生姓名:"); string Sname = Console.ReadLine(); Console.Write("请输入更改后的学生性别:"); bool Ssex = Convert.ToBoolean(Console.ReadLine()); Console.Write("请输入更改后的学生生日:"); DateTime Sbirthday = Convert.ToDateTime(Console.ReadLine()); Console.Write("请输入更改后的学生成绩:"); decimal Sscore = Convert.ToDecimal(Console.ReadLine()); //2、执行修改 SqlConnection conn = new SqlConnection("server=.;database=Data0425;user=sa;pwd=123;"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "update student set name='" + Sname + "',sex='" + Ssex + "',birthday='" + Sbirthday + "',score=" + Sscore + " where code = '" + Scode + "'"; conn.Open(); cmd.ExecuteNonQuery(); Console.WriteLine("修改成功!"); conn.Close(); Console.ReadLine(); }
四、删除数据
cmd.ExecuteNonQuery();
//1、要删除的学生学号 Console.Write("请输入要删除的学生编号:"); string Scode = Console.ReadLine(); //2、执行删除 SqlConnection conn = new SqlConnection("server=.;database=Data0425;user=sa;pwd=123;"); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "delete from student where code = '" + Scode + "'"; try { conn.Open(); cmd.ExecuteNonQuery(); Console.WriteLine("删除成功!"); } catch { Console.WriteLine("服务器连接失败!删除失败!"); } finally { conn.Close(); Console.ReadLine(); } }