2章 上级1-3 关键代码
2016-12-06 16:49
399 查看
using System; using System.Collections.Generic; using System.Text; using System.Data.SqlClient; using System.Data; using System.Threading.Tasks; using System.Linq; namespace WindowsFormsApplication1 { /// <summary> /// 此类维护数据库连接字符串,和 Connection 对象 /// </summary> public class DBHelper { // 数据库连接字符串 private string connString = @"Data Source=.;Initial Catalog=MySchool;Integrated Security=True"; // 数据库连接 Connection 对象 private SqlConnection connection; /// <summary> /// Connection对象 /// </summary> public SqlConnection Connection { get { if (connection == null) { connection = new SqlConnection(connString); } return connection; } } /// <summary> /// 打开数据库连接 /// </summary> public void OpenConnection() { if (Connection.State == ConnectionState.Closed) { Connection.Open(); } else if (Connection.State == ConnectionState.Broken) { Connection.Close(); Connection.Open(); } } /// <summary> /// 关闭数据库连接 /// </summary> public void CloseConnection() { if (Connection.State == ConnectionState.Open || Connection.State == ConnectionState.Broken) { Connection.Close(); } } } } namespace WindowsFormsApplication1 { public partial class Form4 : Form { public Form4() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { this.Close(); } private void Form4_Load(object sender, EventArgs e) { MessageBox.Show("系统发生错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.); BindGrade(); } private void nianji_SelectedIndexChanged(object sender, EventArgs e) { } public bool BindGrade() { DBHelper db = new DBHelper(); try { string sql = "SELECT * FROM [Grade]"; db.OpenConnection(); SqlCommand comm = new SqlCommand(sql, db.Connection); SqlDataReader m = comm.ExecuteReader(); while (m.Read()) { string g = m["GradeName"].ToString(); nianji.Items.Add(g); } m.Close(); return false; } catch (Exception e) { Console.WriteLine(e); MessageBox.Show("系统发生错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } finally { db.CloseConnection(); } } } } namespace WindowsFormsApplication1 { public partial class Form3 : Form { public Form3() { InitializeComponent(); } private void toolStripButton1_Click(object sender, EventArgs e) { Form4 s = new Form4(); s.MdiParent = this; s.Show(); } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { this.Close(); } private void button1_Click(object sender, EventArgs e) { this.Close(); } } }
相关文章推荐
- c#实现cmd功能
- C#判断某元素是否存在数组中
- eclipse常用快捷键
- JMS、AMQP和MQTT主要特性
- 【软工学习笔记】【web开发环境】解决eclipse中web项目出现Project facet Java version 1.8 is not supported.的问题
- webservice 是什么
- 下载Detours源代码、编译Detours Express Lib库(32位静态库,版本3.0)
- Java集合之HashMap
- windows系统numpy的下载与安装教程
- jdk 与 maven环境变量配置引发的问题及处理
- java SWT:基于Composite定制背景透明的浮动图像按钮(image button)
- java导入和导出
- JDBC(1)JAVA连接数据库之 基础知识学习
- 基本概念学习(七)---ANSI(字符代码)与字节、位元
- Is necessary to close fille in python?
- HeadFirst学习之单例模式
- Eclipse如何查看版本号
- 在win10 64位下编译,提示[C++ Error] E2075 Incorrect project override option: (x86)\borland\cbuilder6\lib\vcl60.csm
- 从零开始学Python06作业源码(仅供参考)
- [C#] 简单的 Helper 封装 -- CookieHelper