数据访问---ADO.NET

数据访问:——ADO.NET

1.创建链接
2.创建与执行命令——SQL
3.读取或准备相关数据

一、命名空间:
using System.Data;
using System.Data.SqlClient;

二、链接类 - SqlConnection——创建与数据库的链接
(一)链接字符串: 连哪台电脑,连电脑上的哪个数据库,用户名是什么,密码
server=目标服务器的IP地址;database=数据库名;uid=数据库的登录名;pwd=数据库的登录密码
server=.;database=mydb;uid=sa;pwd=123

public const string CONNSTRING="server=.,database=mydb,uid=sa,pwd=123";
(二)实例化——构造
SqlConnection conn = new SqlConnection();
SqlConnection conn = new SqlConnection(链接字符串);

sqlconnction  conn=new sqlconnction(CONNSTRIN);
(三)属性
ConnectionString:(string)指定与获取链接字符串。
State:用来描述链接当前的状态。Closed-链接处于关闭状态。Open-链接处于打开状态。
(四)方法
Open():打开链接
Close():关闭链接。
(五)案例
1.定义链接字符串。
string connectionString = "server=.;database=mydb;uid=sa;pwd=123";
2.构造链接对象
SqlConnection conn = new SqlConnection(connectionString);

SqlConnection conn = new SqlConnection();
conn.ConnectionString = connectionString;
3.打开链接
conn.Open();
4.关闭链接
conn.Close();

三、命令类——SqlCommand——向数据传递SQL语句或存储过程,并执行。
(一)构造:
SqlCommand cmd = new SqlCommand();
SqlCommand cmd = conn.CreateCommand();
(二)属性:
Connection:(SqlConnection)指定通过哪个链接对象来操作数据库
CommandText:(string)要执行的SQL语句或存储过程名.
(三)方法:
ExecuteNonQuery()——执行命令,返回影响行数。一般用来执行增删改的语句。
ExecuteReader() ——执行命令,返回一个读取器对象。一般用来执行查询语句。
(四)案例:
string connectionString = "server=.;database=mydb;uid=sa;pwd=123";
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();

//操作数据库
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "insert into student values('s001','张三','1','清华大学','1990-6-1')";
cmd.ExecuteNonQuery();

conn.Close();


四、读取器类:——SqlDataReader——从查询的结果集中把数据逐个读出来。
(一)构造:
只有唯一的一种构造方式。再没有别的构造方式了。
SqlDataReader dr = cmd.ExecuteReader();
说明:只读,只向前,读取器工具。内存中只占一条记录的空间。
(二)属性:
HasRows : bool,判断是否有数据可读。是否查出数据来了。

(三)方法:
Read():bool。把结果集中当前行读取内存的datareader中来。读来了,返回true。没有数据可读,返回false。
在使用SqlDataReader读取某列数据时,必须先使用Read()方法把数据先取到内存中的SqlDataReader中。

最常用的读取句式:
while (dr.Read())
{
Console.WriteLine(dr[0].ToString() + dr[1].ToString() + dr[2].ToString() + dr[3].ToString() + dr[4].ToString());
}
从SqlDataReader中读取某列值的时候,使用方法:
1.dr[列的下标/列的索引号]
2.dr["列名"]
3.dr.GetInt(索引号) dr.GetString(索引号) ...
(四)案例:
1.登录。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Data;
 7 using System.Data.SqlClient;
 8 
 9 namespace ConsoleApplication19
10 {
11     class Program
12     {
13         public const string CONNSTRING = "sever=.,database=mydb,uid=sa,pwd=123";
14         static void Main(string[] args)
15         {
16             Console.Write("请输入用户名:");
17             string uid = Console.ReadLine();
18             Console.Write("请输入密码:");
19             string pwd = Console.ReadLine();
20 
21             SqlConnection conn = new SqlConnection(CONNSTRING);
22             conn.Open();
23             SqlCommand cmd = conn.CreateCommand();
24             cmd.CommandText = "select * from Login where UserName=XX and password=XX";
25             SqlDataReader dr = cmd.ExecuteReader();
26             if (dr.HasRows == true)
27             {
28                 Console.WriteLine("OK");
29             }
30             else
31             {
32                 Console.WriteLine("Error");
33             }
34             conn.Close();
35         }
36     }
37 }
登录

 


2.读取显示表中的所有的数据。


3.完善插入功能,在插入之前,判断一下学号是否已存在。如果已存在就提示报名,不存在就插入进去。

posted @ 2015-05-08 09:55  小天哥哥  阅读(133)  评论(0编辑  收藏  举报