ado.net
数据库访问技术
将数据库中的数据,提取到内存中,展示给用户看
还可以将内存中的数据写入数据库中去
并不是唯一的数据库访问技术,但是它是最底层的数据库访问技术
数据库:
create database Data1128 use Data1128 create table Users ( code int primary key identity(0,1), usename nvarchar(100), [password] nvarchar(100), nick nvarchar(100), sex nvarchar(100), nation nvarchar(100), class nvarchar(100), birthday datetime, ) create table Nation ( nationcode nvarchar(100) primary key, nationname nvarchar(100), ) create table Class ( classcode nvarchar(100) primary key, classname nvarchar(100), ) insert into Nation values('N01','汉族') insert into Nation values('N02','藏族') insert into Nation values('N03','回族') insert into Nation values('N04','维吾尔族') insert into Class values('C01','美术班') insert into Class values('C02','音乐班') insert into Class values('C03','表演班') insert into Class values('C04','体育班') insert into Users values('张三','1234','同花顺',1,'N01','C04','1999-05-24') insert into Users values('李四','1234','沃达丰',1,'N02','C03','1998-09-03') insert into Users values('王美丽','1234','妞妞',0,'N03','C02','1999-02-25') select *from Users; select *from Nation; select *from Class;
//删除:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient;//需要添加 namespace abonet { class Program { static void Main(string[] args) { Console.Write("请输入要删除的用户名:"); string name = Console.ReadLine(); //创建数据库连接类 string sql = "server=.;database=Data1128;user=sa;pwd=123;";//编写连接字符串 SqlConnection conn = new SqlConnection(sql); //SqlConnection conn = new SqlConnection(server=.;database=Data1128;user=sa;psd=123;); //创建数据库操作类,创建过程是与刚创建的连接对象匹配起来 SqlCommand comd = conn.CreateCommand(); //编写操作语句 TSQL语句 comd.CommandText = "delete from Users where usename like '"+name+"%'"; //数据库连接打开,准备执行操作 conn.Open(); //执行操作,并记录受影响的行数 int count = comd.ExecuteNonQuery(); //关闭数据库连接 conn.Close(); //提示操作是否成功 if (count > 0) Console.WriteLine("删除成功!"); else Console.WriteLine("删除失败!"); Console.ReadKey(); } } }
//添加
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) { Console.Write("请输入要添加的用户名:"); string name = Console.ReadLine(); Console.Write("请输入要添加的密码:"); string passw = Console.ReadLine(); Console.Write("请输入要添加的昵称:"); string nick = Console.ReadLine(); Console.Write("请输入要添加的性别:"); string sex = Console.ReadLine(); Console.Write("请输入要添加的民族:"); string nation = Console.ReadLine(); Console.Write("请输入要添加的班级:"); string clas = Console.ReadLine(); Console.Write("请输入要添加的生日:"); string bir = Console.ReadLine(); //创建数据库连接类 SqlConnection con = new SqlConnection("server=.;database=Data1128;user=sa;pwd=123;"); //创建数据库操作类 SqlCommand com = con.CreateCommand(); com.CommandText = "insert into Users values('"+name+"','"+passw+"','"+nick+"','"+sex+"','"+nation+"','"+clas+"','"+bir+"')"; con.Open(); int count = com.ExecuteNonQuery(); con.Close(); if (count > 0) Console.WriteLine("添加成功!"); else Console.WriteLine("添加失败!"); Console.ReadKey(); } } }