1、什么是ADO.NET? (是一种数据库访问技术)
ADO.NET的名称起源于ADO(ActiveX Data Objects),是一个COM组件库,用于在以往的Microsoft技术中访问数据。之所以使用ADO.NET名称,是因为Microsoft希望表明,这是在NET编程环境中优先使用的数据访问接口。
2、ADO.NET 的作用?
通过程序来操作数据库
3、连接,基础增删改:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //连接字符串 string sql = "server=.;database=Data0216;user=sa;pwd=123;"; //数据库连接类 SqlConnection conn = new SqlConnection(sql); //数据库操作类 SqlCommand cmd = conn.CreateCommand(); //cmd.CommandText = "insert into Users values('zhaoliu','1234','赵六',1,'2004-4-4','N001');"; //cmd.CommandText = "update Users set NickName = '小六子' where username = 'zhaoliu'"; //编写TSQL语句 cmd.CommandText = "delete from Users where username='zhaoliu'"; //打开数据库连接 conn.Open(); //执行操作 cmd.ExecuteNonQuery(); //关闭数据库连接 conn.Close(); Console.ReadLine(); } } }
4、查询:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { string sql = "server=.;database=Data0216;user=sa;pwd=123"; SqlConnection conn = new SqlConnection(sql); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select *from Users"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { string ids = dr[0].ToString(); string username = dr[1].ToString(); string birthday = dr["Birthday"].ToString(); string password = dr["PassWord"].ToString(); string nickname = dr["NickName"].ToString(); string sex = dr["Sex"].ToString(); string nation = dr["Nation"].ToString(); Console.WriteLine(ids + " | " + username + " | " + password + " | " + nickname + " | " + sex + " | " + birthday + " | " + nation); } } conn.Close(); Console.ReadKey(); } } }
5、完整的增删改:
删除:
using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string sql = "server=.;database=Data0216;user=sa;pwd=123"; SqlConnection conn = new SqlConnection(sql); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "delete from users where username='zhangsan'"; conn.Open(); int a = cmd.ExecuteNonQuery(); if (a > 0) Console.WriteLine("删除成功,本次共删除" + a + "行"); else Console.WriteLine("删除失败,本次未删除任何数据"); conn.Close(); Console.ReadLine(); } } }
增和改:
using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { bool has = false; string sql = "server=.;database=Data0216;user=sa;pwd=123"; SqlConnection conn = new SqlConnection(sql); SqlCommand cmd = conn.CreateCommand(); Console.Write("请输入要修改的用户的用户名:"); string name = Console.ReadLine(); cmd.CommandText = "select *from Users where UserName = '" + name + "'"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { has = true; } conn.Close(); if (!has) Console.WriteLine("用户名输入错误!查无此用户"); else { Console.WriteLine("已查询到此用户,请输入更改的信息"); Console.Write("请输入修改后的密码:"); string password = Console.ReadLine(); Console.Write("请输入修改后的昵称:"); string nickname = Console.ReadLine(); Console.Write("请输入修改后的性别:"); string sex = Console.ReadLine(); Console.Write("请输入修改后的生日:"); string birthday = Console.ReadLine(); Console.Write("请输入修改后的民族:"); string nation = Console.ReadLine(); cmd.CommandText = "update Users set PassWord='" + password + "',NickName='" + nickname + "',Sex='" + sex + "',Birthday='" + birthday + "',Nation='" + nation + "' where UserName = '" + name + "'"; conn.Open(); int aaa = cmd.ExecuteNonQuery(); conn.Close(); if (aaa > 0) Console.WriteLine("修改成功"); else Console.WriteLine("修改失败"); } Console.ReadLine(); } } }