写在前面:数据库编程采用的是SQL Server数据库
1、关系型数据库基本概念
1)数据库DataBase:存储有一定结构和组织数据。文件
Student(sno,sname,ssex,sbirth,sdept);
2)数据库管理系统DBMS:管理数据库,select ,insert,update,delete;create,
drop,alter; SQL语句编译、执行、返回结果都是由DBMS(部分)完成。
eg: select * from student;
3)数据库系统DBS
4)数据库管理员DBA
2、数据库基本对象
1) 表Table(二维表)
create table Student(
sno varchar(20),
sname varchar(20),
ssex varchar(4)
);
对于表的基本操作:SQL语句
1)Select语句
selectcustomerID,name,phone from customers;
select name from customers;
按照条件查询
select * from student where ssex='男';
select * from student where ssex='男' and sdept='数学系'
in关键字
select * from student where sdept='数学系'
or sdept='外语系'
select * from student where sdeptin('数学系', '外语系')and ssex='男'
between x1 and x2:介于两者之间
select * from student where sage>=18 and sage<=20;
select * from student where sage between 18 and 20;
模糊查询 like 通配符 _,%:
select * from student where sname=’张’;
select * from student where sname like ’张_’;
_:匹配任意一个字符
select * from student where sname like ’张_’;
%:匹配任意多个任意字符
select * from student where sname like '张_%';
列取别名:
2)Insert 语句:增加记录
insert into 表名(列1,列2,列3) values(值1,值2,值3)
eg:
insert intostudent(sno,sname,sage,ssex,sdept)
values('2013001','zhang',20,'男','math')
3)delete语句:删除记录
delete from studentwhere sno='2013001'
4)update语句:更新记录
update student set sname='liu' where sno='2013001'
3、ADO.NET数据库编程基本步骤
Application---sql--->DBMS---编译、执行---->DataBase
<-----------------SQL执行结果------------
1)创建一个连接,使应用程序连接上数据库服务器。
数据库基本要素:IP地址;SQLServer服务器名称;数据库名称;用户名、密码;(连接字符串)
因个人电脑而异,可在数据库连接中查看。我的没有设置用户名和密码,为 :
@"Data Source=.\SQLEXPRESS;Integrated Security=True";
通常为下面这个形式:
string connstr=”Data Source=.\SQLEXPRESS;InitialCatalog=mydb;User ID=sa;Password=123”;
以连接SQLServer为例,创建连接:
//创建一个连接
string connstr = @"Data Source=.\SQLEXPRESS;Integrated Security=True";
SqlConnection conn=new SqlConnection(connstr);
conn.Open();//打开连接
//执行SQL语句
string sql = "select * fromstudent";
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;//命令对象Connection属性
cmd.CommandText = sql;//命令文本SQL
//执行命令返回一个记录集
SqlDataReaderdr=cmd.ExecuteReader();//执行SQL语句
while (dr.Read())//使dr向下移动一行
{
Console.WriteLine(dr[0]+" "+dr[1]+""+dr[2]);//读取当前行的第1,2,3列
}
conn.Close();//关闭连接
2、执行增删改语句,不返回结果集
//创建一个连接
string connstr = @"Data Source=.\SQLEXPRESS;Integrated Security=True";
SqlConnection conn=new SqlConnection(connstr);
conn.Open();//打开连接
//执行SQL语句
string sql = "insert into customersvalues('1002','zhangsan','02222')";
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;//命令对象Connection属性
cmd.CommandText = sql;//命令文本SQL
//执行insert,delete,update命令
cmd.ExecuteNonQuery();
conn.Close();
删除语句:
//创建一个连接
string connstr = @"Data Source=.\SQLEXPRESS;Integrated Security=True";
SqlConnection conn=new SqlConnection(connstr);
conn.Open();//打开连接
//执行SQL语句
string sql = "delete from customers wherecustomerid='1002'";
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;//命令对象Connection属性
cmd.CommandText = sql;//命令文本SQL
//执行insert,delete,update命令
intx=cmd.ExecuteNonQuery();
Console.WriteLine(x);
conn.Close();
SQL语句带有变量:
stringcid = "";
cid = Console.ReadLine();
stringsql = "delete fromcustomers where customerid='"+cid+"'";
完整示例:
static void Main(string[] args)
{
//创建一个连接
string connstr = @"Data Source=.\SQLEXPRESS;Integrated Security=True";
SqlConnection conn=new SqlConnection(connstr);
conn.Open();//打开连接
//执行SQL语句
string cid = "",cname,cphone;
cid = Console.ReadLine();
cname = Console.ReadLine();
cphone = Console.ReadLine();
string sql = "insert into customersvalues('"+cid+"','"+cname+"','"+cphone+"')";
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;//命令对象Connection属性
cmd.CommandText = sql;//命令文本SQL
//执行insert,delete,update命令
intx=cmd.ExecuteNonQuery();
Console.WriteLine(x);
conn.Close();
Console.ReadLine();
}