已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1. DBHelper类class DBHelper String connstr = Data Source=.;Initial Catalog=FlowerSys;User ID=sa; SqlConnection conn; /封装数据库对象 public SqlConnection Conn get if (conn = null) conn = new SqlConnection(connstr); return conn; /打开数据库连接 public void OpenConn() if (Conn.State = ConnectionState.Broken) Conn.Close(); Conn.Open(); else if (Conn.State = ConnectionState.Closed) Conn.Open(); /关闭数据库连接 public void CloseConn() if (Conn.State = ConnectionState.Broken | Conn.State = ConnectionState.Open) Conn.Close(); 2. 连接式 增,删,改/sql代码使用string.Format()方法组合出。 string sql = string.Format(update FlowerSale set SaleCount=0,SalePrice=1, Remark=2 where SaleId=3, txtCount.Text.Trim(), txtPrice.Text.Trim(), txtRemark.Text.Trim(), Id); try db.OpenConn();/连接数据库 /生成Command对象将sql语句和连接对象传入 SqlCommand comm = new SqlCommand(sql, db.Conn); /调用ExecuteNonQuery()方法执行sql操作并返回受影响的行数 int num = comm.ExecuteNonQuery(); if (num = 1) MessageBox.Show(更新成功!); else MessageBox.Show(更新失败!); catch (Exception ex) MessageBox.Show(ex.Message);/发生异常时显示错误信息 finally db.CloseConn();/关闭数据库连接 this.Close();/更新完之后关闭3. 连接式 查询操作string sql = select fs.SaleId,fs.SaleCount,fs.SalePrice,fs.Remark from FlowerSale as fs where fs.SaleId= + Id; SqlDataReader reader = null;/声明一个对象用来保存查询结果 try db.OpenConn();/打开连接 SqlCommand comm = new SqlCommand(sql, db.Conn); /查询操作使用ExecuteReader()方法,注意返回结果为SqlDataReader类型 reader = comm.ExecuteReader(); /循环获取查询内容 while (reader.Read() lblNo.Text = readerSaleId.ToString(); txtCount.Text = readerSaleCount.ToString(); txtPrice.Text = readerSalePrice.ToString(); txtRemark.Text = readerRemark.ToString(); catch (Exception ex) /发生异常时显示错误信息 MessageBox.Show(ex.Message); finally reader.Close();/关闭SqlDataReader。 db.CloseConn();/关闭连接 4. 非空验证foreach (Control item in this.Controls) if (item is TextBox) if (item.Text.Trim() = string.Empty) MessageBox.Show(请输入所有手机信息); return; 5. 断开式 comboBox数据绑定/查询语句 String sql = select * from MobileBrand; SqlDataAdapter sda = new SqlDataAdapter(sql, db.Conn); if (ds.TablesMobileBrand != null) ds.TablesMobileBrand.Clear(); sda.Fill(ds, MobileBrand); /在上表中第一行添加一行 DataRow row = ds.TablesMobileBrand.NewRow();/新建数据行,创建格式 row0 = 0; row1 = 请选择; ds.TablesMobileBrand.Rows.InsertAt(row,0); /绑定comboBox控件 cboBrand.DataSource = ds.TablesMobileBrand; cboBrand.ValueMember = BrandId; cboBrand.DisplayMember = Brand;6. 断开式 dataGridViews数据绑定 /查询语句string sql = select fs.SaleId,f.FlowerId,f.FlowerName,fs.SaleCount,fs.SalePrice,fs.SaleDate,fs.SaleClert,fs.Remark from FlowerSale as fs inner join Flower as f on (fs.FlowerId=f.FlowerId); /SqlDataAdapter对象负责执行sql语句并得到结果 SqlDataAdapter sda = new SqlDataAdapter(sql, db.Conn); /先判断表是否存在,存在是否为空,清空操作 if (ds.TablesFlower != null) ds.TablesFlower.Clear(); /将SqlDataAdapter填充数据到DataSet对象中 sda.Fill(ds, Flower); /dataGridView绑定数据表 dgvShow.DataSource = ds.TablesFlower;7. 清空操作foreach (Control item in this.Controls) if (item is TextBox) item.Text = ; 8. 连接式 查询数据绑定ListView控件lvPhone.View = View.Details;/设置listView控件视图为Details lvPhone.Items.Clear();/清空Details视图表中的数据 String sql = select mb.Brand,mi.Type,mi.Price,mi.Size,mi.Weight,mi.Style from MobileInfo mi inner join MobileBrand as mb on(mi.BrandId=mb.BrandId); SqlDataReader reader = null; try db.OpenConn(); SqlCommand comm = new SqlCommand(sql, db.Conn); reader = comm.ExecuteReader(); while (reader.Read() /声明一个listView行,将读取出的数据按行添加入listView行 ListViewItem item = new ListViewItem(readerBrand.ToString(), 0); item.SubItems.Add(readerType.ToString(); item.SubItems.Add(readerPrice.ToString(); item.SubItems.Add(readerSize.ToString(); item.SubItems.Add(readerWeight.ToString
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 七年级语文上册期末复习专题3:标点符号 课件共24张
- 异位妊娠的中医护理光疗法
- 2025年小学英语教案设计模板
- 2025年内蒙古自治区公需课学习-国民营养计划实施方案1094
- 2026年高校科研考核试题及答案
- 2026年大学大四(烹饪工艺与营养)中式菜肴制作综合测试题及答案
- 2026年儿童保健服务培训考核真题及答案解析
- 2026年大气污染防治中心招聘真题及参考答案
- 护理专业课程中的临床护理应用
- 2026及未来5年中国农业产业化联合体行业市场行情监测及投资方向研究报告
- 2024年湖南省高考历史试卷真题(含答案解析)
- 临时用电应急预案方案
- 13J104蒸压加气混凝土砌块板材构造
- 住房按揭借款合同
- 四年级四年级下册阅读理解20篇(附带答案解析)经典
- GB/T 17846-2024小艇电动舱底泵
- 2024地面用晶体硅光伏组件环境适应性测试要求第1部分:一般气候条件
- 洼田饮水试验评定量表
- 《煤气安全作业》培训教材
- 概率论与数理统计期末考试卷附答案
- 穴位注射水针专题宣讲
评论
0/150
提交评论