C#学习笔记09:数据库编程(一)
2013-08-01 16:15
441 查看
写在前面:数据库编程采用的是SQL Server数据库
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
create table Student(
sno varchar(20),
sname varchar(20),
ssex varchar(4)
);
对于表的基本操作:SQL语句
1)Select语句
selectcustomerID,name,phone from customers;
select
namefrom 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'
<-----------------SQL执行结果------------
1)创建一个连接,使应用程序连接上数据库服务器。
数据库基本要素:IP地址;SQLServer服务器名称;数据库名称;用户名、密码;(连接字符串)
因个人电脑而异,可在数据库连接中查看。我的没有设置用户名和密码,为 :
@"Data Source=.\SQLEXPRESS;Integrated Security=True";
通常为下面这个形式:
string connstr=”Data Source=.\[b]SQLEXPRESS;InitialCatalog=mydb;[/b]User
ID=sa;Password=123”;
以连接SQLServer为例,创建连接:
//创建一个连接
string connstr = @"Data Source=.\SQLEXPRESS;Integrated Security=True";
SqlConnection conn=newSqlConnection(connstr);
conn.Open();//打开连接
//执行SQL语句
string sql = "select * fromstudent";
SqlCommand cmd = newSqlCommand();
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();//关闭连接
string connstr = @"Data Source=.\SQLEXPRESS;Integrated Security=True";
SqlConnection conn=newSqlConnection(connstr);
conn.Open();//打开连接
//执行SQL语句
string sql = "insert into customersvalues('1002','zhangsan','02222')";
SqlCommand cmd = newSqlCommand();
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=newSqlConnection(connstr);
conn.Open();//打开连接
//执行SQL语句
string sql = "delete from customers wherecustomerid='1002'";
SqlCommand cmd = newSqlCommand();
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+"'";
完整示例:
staticvoid Main(string[] args)
{
//创建一个连接
string connstr = @"Data Source=.\SQLEXPRESS;Integrated Security=True";
SqlConnection conn=newSqlConnection(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 = newSqlCommand();
cmd.Connection = conn;//命令对象Connection属性
cmd.CommandText = sql;//命令文本SQL
//执行insert,delete,update命令
intx=cmd.ExecuteNonQuery();
Console.WriteLine(x);
conn.Close();
Console.ReadLine();
}
本章练习题下载地址:点此下载
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
namefrom 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=.\[b]SQLEXPRESS;InitialCatalog=mydb;[/b]User
ID=sa;Password=123”;
以连接SQLServer为例,创建连接:
//创建一个连接
string connstr = @"Data Source=.\SQLEXPRESS;Integrated Security=True";
SqlConnection conn=newSqlConnection(connstr);
conn.Open();//打开连接
//执行SQL语句
string sql = "select * fromstudent";
SqlCommand cmd = newSqlCommand();
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=newSqlConnection(connstr);
conn.Open();//打开连接
//执行SQL语句
string sql = "insert into customersvalues('1002','zhangsan','02222')";
SqlCommand cmd = newSqlCommand();
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=newSqlConnection(connstr);
conn.Open();//打开连接
//执行SQL语句
string sql = "delete from customers wherecustomerid='1002'";
SqlCommand cmd = newSqlCommand();
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+"'";
完整示例:
staticvoid Main(string[] args)
{
//创建一个连接
string connstr = @"Data Source=.\SQLEXPRESS;Integrated Security=True";
SqlConnection conn=newSqlConnection(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 = newSqlCommand();
cmd.Connection = conn;//命令对象Connection属性
cmd.CommandText = sql;//命令文本SQL
//执行insert,delete,update命令
intx=cmd.ExecuteNonQuery();
Console.WriteLine(x);
conn.Close();
Console.ReadLine();
}
本章练习题下载地址:点此下载
相关文章推荐
- C#学习笔记24——数据库编程
- C#学习笔记10:数据库编程(二)
- 《Web Service 编程 --用C#.NET 开发网络服务》北京希望出版社 我的学习笔记(第一章)(也就是书上抄了一写东西而已)
- Mysql 数据库学习笔记02 编程
- 学习笔记之在 LINQ 外部使用 Lambda 表达式(C# 编程指南)
- .黑马程序员之C#编程基础学习笔记:不断要求用户输入一个数字,当用户输入end的时候显示刚才输入的数字中的最大值。
- C# 学习笔记 数据库 断开连接层 (书:C#与.NET4 第22章)
- Oracle 9i & 10g编程艺术-深入数据库体系结构-学习笔记(持续更新中)
- 启动MySQL 监视器(Monitor) 【数据库高效编程 - 学习笔记 第三章】
- Java学习笔记11——数据库编程
- C#2005高级编程Chapter11内存管理和指针学习笔记
- 【卷土重来之C#学习笔记】(二)c#编程概述
- (原创)c#学习笔记09--定义类03--构造函数和析构函数
- (原创)c#学习笔记09--定义类05--类库项目
- C#GUI编程学习之ComboBox控件篇1--连接数据库并传递数据到文本框
- 《Web Service 编程 --用C#.NET 开发网络服务》北京希望出版社 我的学习笔记(第二章)(也就是书上抄了一写东西而已)
- 表的维护和改造 【数据库高效编程 - 学习笔记 第五章】
- C# 学习笔记(一) ListView控件显示数据库表信息
- 深入.NET平台和C#编程 第四章 学习笔记
- 黑马程序员之C#编程基础学习笔记:用while continue实现计算1到100之间的除了能被7整除之外所有整数的和。