Command对象-对用户信息表进行操作

上传人:jiups****uk12 文档编号:93376287 上传时间:2019-07-21 格式:DOC 页数:9 大小:91KB
返回 下载 相关 举报
Command对象-对用户信息表进行操作_第1页
第1页 / 共9页
Command对象-对用户信息表进行操作_第2页
第2页 / 共9页
Command对象-对用户信息表进行操作_第3页
第3页 / 共9页
Command对象-对用户信息表进行操作_第4页
第4页 / 共9页
Command对象-对用户信息表进行操作_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《Command对象-对用户信息表进行操作》由会员分享,可在线阅读,更多相关《Command对象-对用户信息表进行操作(9页珍藏版)》请在金锄头文库上搜索。

1、数据库应用技术 第五讲Command对象第五讲 Command对象四、案例(在小型超市管理系统中的实现)l SqlCommand .CommandType= CommandType.Text(SQL命令)的例子功能描述:实现对表tbYHXXB(用户信息表【由用户ID、用户名、密码、用户类型4个字段构成】,如下图)的添加、修改和删除,没有返回结果,操作结果看表的内容变化代码参看附录1l SqlCommand .CommandType= CommandType.StoredProcedure(存储过程)的例子用到SqlParameter对象功能描述:实现对表tbYHXXB(用户信息表)的添加、修改

2、和删除,没有返回结果,操作结果看表的内容变化代码参看附录2l SqlCommand .CommandType= CommandType. TableDirect(表)的例子功能描述:打开表tbYHXXB(用户信息表),用ExcuteReader方法实现。代码参看附录3五、上机实践仿照【案例】上机练习,验证。运行界面如下图【附录1】提示:MySQL为前面创建的动态链接库ClassLibOfSuperMarket.dllprivate void button1_Click(object sender, EventArgs e) /用SQL命令添加一个用户:“10001,张三,收银员” MySQL

3、getConn=new MySQL(); SqlConnection theConn = null; try theConn = getConn.GetConnetion(); theConn.Open(); SqlCommand sqlCmd = new SqlCommand(); sqlCmd.Connection = theConn; sqlCmd.CommandType = CommandType.Text; sqlCmd.CommandText = Insert Into tbYHXXB Values(10001,张三,收银员); sqlCmd.ExecuteNonQuery();

4、catch (SqlException sqlEx) MessageBox.Show(sqlEx.Message, SQL错误, MessageBoxButtons.OK, MessageBoxIcon.Error); catch(Exception Ex) MessageBox.Show(Ex.Message, SQL错误, MessageBoxButtons.OK, MessageBoxIcon.Error); finally if (theConn.State = System.Data.ConnectionState.Open) theConn.Close(); MessageBox.

5、Show(记录添加成功!请查看数据库中的表tbYHXXB, 添加记录, MessageBoxButtons.OK, MessageBoxIcon.Information); private void button2_Click(object sender, EventArgs e) /用SQL命令修改用户参数:将“10001,张三,收银员”改为 /“10001,我的名字,经理” MySQL getConn = new MySQL(); SqlConnection theConn = null; try theConn = getConn.GetConnetion(); theConn.Open

6、(); SqlCommand sqlCmd = new SqlCommand(); sqlCmd.Connection = theConn; sqlCmd.CommandType = CommandType.Text; sqlCmd.CommandText = Update tbYHXXB Set 用户名=我的名字,密码=,用户类型=经理; sqlCmd.ExecuteNonQuery(); catch (SqlException sqlEx) MessageBox.Show(sqlEx.Message, SQL错误, MessageBoxButtons.OK, MessageBoxIcon.

7、Error); catch (Exception Ex) MessageBox.Show(Ex.Message, SQL错误, MessageBoxButtons.OK, MessageBoxIcon.Error); finally if (theConn.State = System.Data.ConnectionState.Open) theConn.Close(); MessageBox.Show(记录修改成功!请查看数据库中的表tbYHXXB, 修改记录, MessageBoxButtons.OK, MessageBoxIcon.Information); private void b

8、utton3_Click(object sender, EventArgs e) /用SQL命令删除一个用户:“10001,张三,收银员” MySQL getConn = new MySQL(); SqlConnection theConn = null; try theConn = getConn.GetConnetion(); theConn.Open(); SqlCommand sqlCmd = new SqlCommand(); sqlCmd.Connection = theConn; sqlCmd.CommandType = CommandType.Text; sqlCmd.Comm

9、andText = Delete from tbYHXXB where 用户ID=10001; sqlCmd.ExecuteNonQuery(); catch (SqlException sqlEx) MessageBox.Show(sqlEx.Message, SQL错误, MessageBoxButtons.OK, MessageBoxIcon.Error); catch (Exception Ex) MessageBox.Show(Ex.Message, SQL错误, MessageBoxButtons.OK, MessageBoxIcon.Error); finally if (the

10、Conn.State = System.Data.ConnectionState.Open) theConn.Close(); MessageBox.Show(记录删除成功!请查看数据库中的表tbYHXXB, 删除记录, MessageBoxButtons.OK, MessageBoxIcon.Information); 【附录2】:private void button4_Click(object sender, EventArgs e) /调用存储过程AddUser添加一个用户:“10001,张三,收银员” MySQL getConn = new MySQL(); SqlConnection theConn = null; try theConn = getConn.GetConnetion(); theConn.Open(); SqlCommand sqlCmd = new SqlCommand(); sqlCmd.Connection = theConn; sqlCmd.CommandType = CommandType.StoredProcedure; sqlCmd.CommandText = AddUser; Sql

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 其它中学文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号