一、 ADO.NET access database object 连接数据库对象
a) 连接对象:SqlConnection
//导包两个:using System.Data; using System.Data.SqlClient;
连接字符串:string conStr = “server=.;uid=accp;pwd=bdqn;database=QQMaster”;
创建连接对象:SqlConnection con = new SqlConnection(conStr);
b) 操作对象:SqlCommand
创建操作对象:SqlCommand cmd = new SqlCommand();
把管子和泵装在一起:cmd.Connection = con;
为泵提供sql指令:cmd.CommandText = 一条sql语句;(例如:select * from QQUser)
c) 打开连接
con.Open();
d) 数据对象:SqlDataReader
增删改:int rows = cmd.ExecuteNonQuery(); //rows 影响数据库的记录行数
查询单个值:object obj = cmd.ExecuteScalar(); //select count(*) from QQUser;
查询多个值:SqlDataReader read = cmd.ExecuteReader(); //select * from QQUser;
e) 用完连接要关闭
con.Close();