ADO_NET_基础

来源:互联网 发布:网络教育和函授含金量 编辑:程序博客网 时间:2024/06/15 23:04
ADO.NET
-> ADO.NET是什么? 一套用于使用C#操作数据库的类库
-> 能做什么?你会写什么SQL语句,ADO.NET就能做什么
-> 四个对象、四个模型
连接对象 Connection系对象
执行对象 Command系对象
读取器对象 DataReader系对象
适配器对象 DataAdapter系对象

执行非查询语句的模型 增、删、改 ExecuteNonQuery()
执行查询单个值的模型 select count(*) ExecuteScalar()
执行查询读取所有数据 ExecuteReader()
包工头的模型 告诉你在哪里要怎样
-> 其他知识点
-> 注入漏洞攻击,使用参数化查询
-> 配置文件处理
-> SQLHelper(重要)




----------------------


3、 连接对象
-> 命名空间
System.Data; // 存储描述数据库数据信息的资源
System.Data.SqlClient;// 存储操作SQL Server的资源
-> 连接对象
SqlConnection
-> 构造方法:参数,数据库的钥匙
数据库实例
数据库名
用户名
密码
string connStr = @"server=IP\实例名;database=数据库名;uid=sa;pwd=123;";
-> Open()   打开


4、 执行对象 SqlCommand
-> 构造方法: 干嘛? sql语句
去哪儿?连接对象
-> 使用
using(SqlConnection conn = new SqlConnection(connStr))
{
using(SqlCommand cmd = new SqlCommand(sql, conn))
{
}
}
-> 方法(多)
-> int cmd.ExecuteNonQuery();执行非查询语句:增、删、改、其他
执行增删改返回受影响行数
执行其他返回-1
-> object cmd.ExecuteScalar();执行查询语句
返回结果集中第一行第一列的数据
如果没有任何数据,返回null
-> SqlDataReader cmd.ExecuteReader();执行查询语句,返回一个读取器


5、 第二个模型 ExecuteScalar()


-> 基本的SQL语句总结
-> 联合结果集与插入多条数据
-> insert into 表名(列1, 列2, ...) values(值1, 值2, ...);
-> insert into 表名(列1, 列2, ...) 符合表结构的结果集
insert into 表名(列1, 列2, ...) select 值1, 值2, ...;
-> 表值构造函数(SQL Server 2008)
select * from
(values(...),(...),...) as tbl(列名)
-> select * into 新表明 from 源表名;
where 1 > 2;
-> ADO.NET
-> 类库
-> 四个类、四个模型
-> 命名空间: System.Data.SqlClient;
SqlConnection
SqlCommand
SqlDataReader
SqlDataAdapter
-> ExecuteNonQuery
-> ExecuteScalar
-> 连接字符串(必须写出来)


2、 SQL注入漏洞


"where uid='" + uid + "' and pwd='" + pwd + "';"

uid = "12333"
pwd = "1' or '1'='1";


"where uid='12333' and pwd='1' or '1'='1';"

select count(*) from Exe2.LoginTbl

原本用来作为值的SQL语句,变成了两个条件
-> 将所有的'变成''
SQL Server中默认'是字符串,在字符串中要表示单引号''表示一个单引号
string str = @"你""好";
-> 参数化查询
-> 将所有的SQL语句需要拼接字符串的地方用别名 @名字 标记需要用的变量
string sql = "select count(*) from Exe2.loginTbl where uid='" + uid + "' and pwd=' + pwd + ';";

string sql = "select count(*) from Exe2.loginTbl where uid=@uid and pwd=@pwd;";
-> 创建一个SqlParameter对象,将参数值与别名绑定到一起
SqlParameter pUid = new SqlParameter("@uid", "赵晓虎");
-> 在执行SQL语句以前,将值交给执行对象cmd
cmd.Parameters.Add(pUid);  // AddRange()


exec
sp_executesql -- 存储过程,表示执行动态SQL
N'select count(*) from Exe2.loginTbl where uid=@uid and pwd=@pwd;'
,N'@uid nvarchar(3),@pwd nvarchar(9)'
,@uid=N'赵晓虎'
,@pwd=N'tigerZhao'


exec 
sp_executesql 
N'select count(*) from Exe2.loginTbl where uid=@uid and pwd=@pwd;'
,N'@uid nvarchar(3),@pwd nvarchar(12)'
,@uid=N'赵晓虎'
,@pwd=N'1'' or ''1''=''1'


3、 第三个模型 ExecuteReader
-> 这个方法返回一个读取器
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
object o = reader[0];
}


4、 补充
-> reader索引
object reader[0]   第0列 (推荐)
object reader["列名"]
-> Get系方法
指定类型数据 reader.Get类型(列编号);
-> Get系方法中的特例
GetValue和GetName
-> GetValue与索引用法一样
-> GetName获得列的名字
-> FieldCount属性
方便的将数据导出成字符串
while(reader.Read())
{
// 此时遍历每一行数据
List<string> list = new List<string>();
for(int i = 0; i < reader.FieldCount; i++)
{
// 遍历当前行中的每一列,并将其作为字符串存到list中
list.Add(reader[i].ToString());
}
string.Join("|", list.ToArray());
}
-> 空数据 NULL
-> 数据库中 NULL 不知道
-> C#中 null 不指向
-> 数据库中的数据在C#中有一个专门的类型叫 DBNull
-> 值类型的空值 可空类型
System.Nullable<T>
int num = 10;
Nullable<int> num = 10;
num = null;




5、 SqlParameter
SqlParameter p = new SqlParameter("@别名", SqlDbType, 长度){ value = 值 };
p.Value = ...


6、 属性与构造方法的作用
Person p = new Person();
p.Name = "张三";
Console.WriteLine(p.Name);

new Person("张建宇", '男', 19);

class LoginForm// 连接字符串就在其中生成了
serverInfo = 连接字符串等  (Regex.Match(..., @"server=(.+?);").Groups[1].Value)
ServerInfo
class Form1

1 创建登录窗体 LoginForm
2 显式登录窗体 ShowDialog()
3 判断在登录窗体中是否登录成功
4 创建主窗体,并告诉主窗体连接字符串是什么
就是在Main方法中得到LoginForm窗体的链接字符串(通过属性获得)
将连接字符处交给主窗体,创建窗体对象(构造方法)