C#复习资料

上传人:206****923 文档编号:90208976 上传时间:2019-06-09 格式:DOCX 页数:8 大小:38.60KB
返回 下载 相关 举报
C#复习资料_第1页
第1页 / 共8页
C#复习资料_第2页
第2页 / 共8页
C#复习资料_第3页
第3页 / 共8页
C#复习资料_第4页
第4页 / 共8页
C#复习资料_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《C#复习资料》由会员分享,可在线阅读,更多相关《C#复习资料(8页珍藏版)》请在金锄头文库上搜索。

1、小二班C#复习资料注:第一步:附加数据库,看清楚数据库名称及数据库内的表名。第二步:打开素材里的vs素材。做老师要求你们编写的代码。三个步骤:1定义一个string sql = String.Format(select * from UserInfo where UserName = 0 and UserPassword = 1, txtUserName.Text, txtUserPassword.Text);2调用DaaSet ds = DBHelper.GetDataSet(sql);3.判断if (ds.Tables0.Rows.Count = 0)插入数据: insert into 表

2、名 values 值(values后的值写的是占位符就加String.Format())修改数据: update 表名set 字段1=值1 where 值删除数据: delete 表名 where值(修改update、删除delete别写错单词了)红色的字体是解析,绿色的是注释,绿色的注释考试时不用写的。一.定义CDataBase公共类:using System.Data.SqlClient;using System.Data;using System.Windows.Forms;解析:这三个需要导入公共类里。public static SqlConnection conn = new Sql

3、Connection(Data Source =.; Initial Catalog = Hotel; Integrated Security = SSPI);解析:注意括号内的,第一个分号是服务器的名称,第二个是数据库名称,到时注意老师给的是什么数据库,最后一个分号不用改。 public static DataSet GetDataSet(string SQLString) / 执行查询语句,返回DataSet try SqlDataAdapter sda = new SqlDataAdapter(SQLString, conn); DataSet ds = new DataSet(); s

4、da.Fill(ds);return ds;catch (System.Data.SqlClient.SqlException ex) throw new Exception(ex.Message); finally 解析:GetDataSet适用所有的select命令。ExecuteSql适用修改、插入语句。public static int ExecuteSql(string SQLString) / 执行SQL语句,获得数据库中因为执行sql命令后受到影响的记录数 try conn.Open(); SqlCommand cmd = new SqlCommand(SQLString, co

5、nn); int rows = cmd.ExecuteNonQuery(); return rows; catch (System.Data.SqlClient.SqlException e) throw e; finally conn.Close(); 二登录代码:public static string na;解析:定义一个变量nastring sql = String.Format(select * from UserInfo where UserName = 0 and UserPassword = 1, txtUserName.Text, txtUserPassword.Text);

6、解析:考试时注意from后面的表名 。 DaaSet ds = DBHelper.GetDataSet(sql); /将执行的结果放在ds中解析: 调用公共类的GetDataSetif (ds.Tables0.Rows.Count = 0) MessageBox.Show(该用户名或密码不存在!); txtUser.Text = ; txtPassword.Text = ; txtUser.Focus(); 解析:通过检测ds中表的行数是否等于0来判断能否查找的到数据,显示消息,清空用户名,密码文本框,光标放在用户名文本框上。 else na = txtUser.Text; HotelMana

7、ge f = new HotelManage(); f.Show(); this.Hide();/隐藏登录窗体解析:这个frmHotelManage是酒店管理窗体,到时候你可以根据老师给你的主窗体的名称是什么进行修改。 三进入系统时欢迎:private void HotelManage_Load(object sender, EventArgs e) ts.Text = Login.na + ,欢迎进入酒店管理系统; 解析:这是是对HotelManage_窗体加载的编程。标签名称+用户名名称。 Login.na 的na在登录编码中定义了na四退出代码: Application.Exit();/

8、或this.Close();五添加代码:string sql = string.Format(insert into UserInfo values(0,1,2),txtUserName.Text,txtUserPassword.Text,cboUserType.Text);解析:定义变量,把UserInfo表的三个值添加进来显示在文本框内if (txtUserName.Text != & txtUserPassword.Text != & cboUserType.Text != )解析;用户名,密码,类型,不为空值就调用下面DBHelper 中的ExecuteSql int result =

9、 DBHelper.ExecuteSql(sql); if (result = 1) /根据返回影响行数判断是否删除数据成功 MessageBox.Show(用户添加成功!, 成功提示, MessageBoxButtons.OK, MessageBoxIcon.Information); else MessageBox.Show(用户添加失败!, 错误提示, MessageBoxButtons.OK, MessageBoxIcon.Error); else MessageBox.Show(请检查数据输入的正确性!, 错误提示, MessageBoxButtons.OK, MessageBoxI

10、con.Information); 六修改代码: string sql = string.Format(update UserInfo set UserPassword=0,UserType=1 where UserName=2,txtUserPassword.Text, cboUserType.Text, txtUserName.Text); int result;/定义修改语句执行后的影响行数 if (txtUserName.Text != & txtUserPassword.Text != & cboUserType.Text != ) result = DBHelper.Execute

11、Sql(sql);/执行修改语句,返回影响行数 if (result = 1)/根据返回影响行数判断是否修改数据成功 MessageBox.Show(用户修改成功!, 成功提示, MessageBoxButtons.OK, MessageBoxIcon.Information); DataBind();/刷新数据库 else MessageBox.Show(用户修改失败!, 错误提示, MessageBoxButtons.OK, MessageBoxIcon.Error); else MessageBox.Show(请检查数据输入的正确性!, 错误提示, MessageBoxButtons.OK, MessageBoxIcon.Information); 七删除代码: if(txtUserName.Text=admin) MessageBox.Show(不能删除admin用户!,提示, MessageBoxButtons.OK, MessageBoxIcon.Warning); else if (txtUserName.Text = Login.na) MessageBox.Show(不能删除当前登录用户!, 提示, MessageBo

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


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

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