Web页面的数据库访问技术
SqlCommand通过ExecuteNonQuery()方法来执行 增、删、改类型的SQL语句。
Web页面数据库访问技术
4.1.4 DataSet和DataTable
DataSet类位于System.Data命名空间,DataSet对 象可以看作是一个数据库,存放的是一些表的集合。 DataSet对象有一个重要属性Tables,Tables是一个集 合属性,集合的元素为表DataTable,要访问DataSet 对象的第一个表可以用如下代码。
Web页面数据库访问技术
3.更新语句 update 表名 set 字段=值, …[where 条件语句]
如修改姓名为“王三”学生的学号为22222的SQL语句如下:
update 学生 set 学号='22222' where 姓名='王三'
Web页面数据库访问技术
4.查询语句 select 字段列表 from 表名 [where 条件语句] 如查询所有的男学生的SQL语句如下: select * from 学生 where 性别='男'
4.2 操作数据库
主要有四类操作: 1 .从数据库中查询数据 2 .修改数据库表中的数据 3 .往数据库表中添加一行数据 4 .删除数据库表中的数据
Web页面数据库访问技术
4.2.1 从数据库中查询数据
1.单条记录查询与显示 protected void btnQuery_Click(object sender, EventArgs e) { string connectionString = @"server=.\MARK;database=BookShop;uid=sa;pwd=123456"; SqlConnection connection = new SqlConnection(connectionString); connection.Open(); string sql = "select UserId,RealName from [User] where UserName='"+this.txtUserName.Text+"'"; SqlCommand cmm = new SqlCommand(sql, connection); SqlDataAdapter da = new SqlDataAdapter(cmm); DataSet ds = new DataSet(); da.Fill(ds); if(ds.Tables[0].Rows.Count>0) { this.txtUserId.Text = ds.Tables[0].Rows[0]["UserId"].ToString(); this.txtRealName.Text = ds.Tables[0].Rows[0]["RealName"].ToString(); } connection.Close(); }
数据库访问模型
学习目标
1 理解工作原理 2 掌握DataBase类的使用 3 能够编写增删改查操作代码
Web页面数据库访问技术
4.1 数据库访问模型
Connection 连接对象
增、删、改
SQL查询 查询结果
Command 命令对象
@"server=.\MARK;database=BookShop;uid=sa;pwd=123456"; SqlConnection connection = new SqlConnection(connectionString); connection.Open(); string sql = "insert into
Web页面数据库访问技术
4.1.2 SqlConnection对象
属性 server
database uid pwd
说明 需要连接的数据库服务器,本地机器可以用.或 者IP地址或者(local),如果是远程主机则使用IP 地址,如果主机上装有多个Sql Server服务器则还 要加标识,标识的名字可以在Microsoft SQL Server Management Studio 中看到,如上面代码中 标识为MARK,因此Server要用server=.\MARK。 数据库服务器中具体数据库的名字 用户名,这是采用SQL Server验证方式才需要的, 如果是Windows身份验证则不需要。 登录密码,这也是只有SQL Server验证方式才需 要的
string connectionString=@"server=.\MARK;database=BookShop;uid=sa;pwd=123456"; SqlConnection connection = new SqlConnection(connectionString); connection.Open();
UserName='"+this.txtUserName.Text+"',RealName='"+this.txtRealName.Text+"' where UserId=" + this.txtUserId.Text;
SqlCommand cmm = new SqlCommand(sql, connection); cmm.ExecuteNonQuery(); connection.Close(); }
DataSet 数据集对象
查询结果
查询结果
DataAdapter 数据适配器
Web页面数据库访问技术
4.1.1 SQL语句
增、删、改操作分别使用insert、delete、update 语句,查询操作使用select语句。 1.插入语句 insert into 表名(字段列表) values(对应字段的列表) 如给学生表插入一条记录的SQL语句如下
b页面数据库访问技术
4.2.1 从数据库中查询数据
1.多条记录查询与显示 (GridView控件)
protected void Page_Load(object sender, EventArgs e) { string connectionString =
@"server=.\MARK;database=BookShop;uid=sa;pwd=123456"; SqlConnection connection = new SqlConnection(connectionString); connection.Open(); string sql = "select * from [User]"; SqlCommand cmm = new SqlCommand(sql, connection); SqlDataAdapter da = new SqlDataAdapter(cmm); DataSet ds = new DataSet(); da.Fill(ds); this.gvUser.DataSource = ds.Tables[0]; this.gvUser.DataBind(); connection.Close();
DataSet ds = new DataSet(); DataTable dt = ds.Tables[0];
Web页面数据库访问技术
4.1.4 DataSet和DataTable
代码第一行新建了DataSet对象,然后获取了 DataSet对象的第一个表,如果知道表的名字(如 student)也可以通过代码ds.Tables["student"]获取表。
DataTable类也属于System.Data命名空间,它有一 个重要属性Rows,Rows也是一个集合属性,集合的元 素是表中的记录(DataRow),类似的要访问表中的第 一行可以用 ds.Tables[0].Rows[0]
其中ds是DataSet对象,如果要访问第一行的第一列, 可以用代码 ds.Tables[0].Rows[0][0]
Web页面数据库访问技术
4.2.3 往数据库表中添加一行数据
往数据库表中添加一行数据和修改数据库表中的数据在
代码实现时十分类似,主要的变化就是SQL语句有update语
句改成了insert 语句。
protected void btnAdd_Click(object sender, EventArgs e) { string connectionString =
Web页面数据库访问技术
4.1.2 SqlConnection对象
SqlConnection对象用来建立和某个数据库的连接,通 过SqlConnection对象的连接字符串来设定SqlConnection对象 是和哪个服务器的哪个数据库连接,登录数据库的用户名和 密码分别是什么。连接字符串确定后,通过SqlConnection对 象的Open()方法来启动与数据库的连接。
@"server=.\MARK;database=BookShop;uid=sa;pwd=123456"; SqlConnection connection = new SqlConnection(connectionString); connection.Open(); string sql = "update [User] set
[User](UserName,Password,Sex,RealName,Telephone,Address) Values(@UserName,@Password,@Sex,@RealName,@Telephone,@Addres s)";
SqlCommand cmm = new SqlCommand(sql, connection); cmm.Parameters.Add(new SqlParameter("@UserName",this.txtUserName.Text)); cmm.Parameters.Add(new SqlParameter("@Password", this.txtPassword.Text)); cmm.Parameters.Add(new SqlParameter("@Sex",this.rdoMan.Checked?"男":"女")); cmm.Parameters.Add(new SqlParameter("@RealName", this.txtRealName.Text)); cmm.Parameters.Add(new SqlParameter("@Telephone", this.txtPhone.Text)); cmm.Parameters.Add(new SqlParameter("@Address", this.txtAddress.Text)); cmm.ExecuteNonQuery(); connection.Close(); }