C# 可视化程序设计机试知识点汇总,DBhelper类代码

Lan
Lan
2020-06-05 / 0 评论 / 950 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2020年06月05日,已超过1392天没有更新,若内容或图片失效,请留言反馈。

image.png

打开窗体

HotelType ht = new HotelType();
ht.Show();//非模式窗体   ht.ShowDialog();//模式窗体

在父窗体中打开子窗体

HotelType ht = new HotelType();
ht.MdiParent = this;
ht.Show();//非模式窗体   ht.ShowDialog();//模式窗体

退出系统

Application.Exit();

清空文本框

两种方式       
this.TextBox.clear();
this.TextBox.text=””;

窗体加载时查询绑定到DataGridView控件中(Load事件,查询)

//定义SQL语句
string sql1 = "select * from RoomType";
//调用DBHelper类的查询方法,返回DataTable类型数据
DataTable dt = DBHelper.getDataTable(sql1);
//将返回的结果绑定到DataGridView控件的数据源中
this.dataGridView1.DataSource = dt;

数据中查出数据绑定到DataGridView控件中(Load事件,查询,给下拉框赋值)

//定义SQL语句
string sql1 = "select * from RoomType";
//调用DBHelper类的查询方法,返回DataTable类型数据
DataTable dt = DBHelper.getDataTable(sql1);
// DisplayMember为显示的文本值,ValueMember为真实的值一般为主键
this.comboBox1.DisplayMember = "typeName";
this.comboBox1.ValueMember = "typeID"
//将返回的结果绑定到DataGridView控件中
this.comboBox1.DataSource = dt;

根据条件查询并重新绑定到DataGridView控件中(点击查询按钮,模糊查询)

一、单条件模糊查询
//获得界面上输入的查询的条件
string typeName = this.textBox1.Text;
//定义包含查询条件的sql语句
string sql = string.Format("select * from RoomType where TypeName like '%{0}%'", typeName);
//调用DBHelper类的查询方法,返回DataTable类型数据
DataTable dt = DBHelper.getDataTable(sql);
//将返回的结果绑定到DataGridView控件中
this.dataGridView1.DataSource = dt;

 

二、多条件模糊查询(eg:按名称模糊查询,按日期查询)

//获得界面上输入的查询的条件
string typeName = this.textBox1.Text;
string dateValue= this.textBox2.Text;
//定义包含查询条件的sql语句
string sql = string.Format("select * from RoomType where 1=1");
if(typeName!=””){
sql +=” and TypeName like '%”+ money1 +”%'” ;
}
if(dateValue!=””){
sql +=” and  dateValue = '”+ dateValue +” '” ;
}
 
//调用DBHelper类的查询方法,返回DataTable类型数据
DataTable dt = DBHelper.getDataTable(sql);
//将返回的结果绑定到DataGridView控件中
this.dataGridView1.DataSource = dt;

 

选中DataGridView中的行,将所有列的数据一个个放入到文本控件中(cellClick事件)。

//在cellClick事件外定义typeID
string typeID;
//判断选中的行数是否>0
    if (this.dataGridView1.SelectedRows.Count>0)
     {
//”=”号左边给全局变量typeID赋值, ”=”号右边获得选中第一行第一列的值转为string类型(列标号以数据库中的顺序为准)
       typeID = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
//”=”号左边给文本框赋值, ”=”号右边获得选中第一行第二列的值转为string类型
       this.textBox2.Text = this.dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
//”=”号定义变量接收, ”=”号右边获得选中第一行第三列的值转为string类型(根据值选中复选框)
string IsAddBed = this.dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
//去掉变量中isAddBed数据中的空格
        IsAddBed = IsAddBed.Trim();
//如果IsAddBed的内容是等于”是“,就选中复选框,否则不选中
        if (IsAddBed=="是"){
           this.checkBox1.Checked = true;
        }else{
           this.checkBox1.Checked = false;
        }
//”=”号定义变量接收, ”=”号右边获得选中第一行第四列的值转为string类型(根据值选中单选按钮)
string radioButton= this.dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
//如果radioButton的内容是等于”男“,就选中所对应的单选按钮
if (IsAddBed=="男"){
     this.radioButton1.Checked = true;
}
//如果radioButton的内容等于”女“,就选中所对应的单选按钮
if (IsAddBed=="女"){
     this.radioButton1.Checked = true;
}
}

添加(click事件)

 

第一步、获取值

//(获得文本框的值)
string TypeName = this.textBox2.Text;
//(判断复选框是否选中)选中了给IsAddBed赋值为”是“,否则为”否“
            string IsAddBed = "";
            if (this.checkBox1.Checked)
            {
                IsAddBed = "是";
            }else{
                IsAddBed = "否";
            }
            //(判断单选按钮是否选中)如果男性单选按钮选中了,给sex赋值为”男“,否则为”女“
          string sex = "";
            if (this.radioButton1.Checked)
            {
                sex = "男";
            }
If(this.radioButton2.Checked){
                sex = "女";
            }
          //(获取下拉框中选中的value值)
            string index= this.comboBox1.SelectedValue.ToString();
             //(获得下拉框中选中的文本值)
            string gender = this.comboBox1.text;

 

第二步、为空判断

if (TypeName==""|| IsAddBed ==""|| sex ==""|| gender =="")
            {
                MessageBox.Show("信息填写不完整,请重新填写!");
//返回
                return;
            }


 

第三步、定义sql语句

string sql = string.Format("insert into RoomType values('{0}','{1}','{2}','{3}','{4}')", TypeName, TypePrice, AddBedPrice, IsAddBed, Remark);


 

第四步、调用dbhelper类增删改方法

int result = DBHelper.Zsg(sql);


 

第五步、判断执行结果

if (result>0)
            {
                MessageBox.Show("添加成功!");
             }
            else
            {
                MessageBox.Show("添加失败!");
            }


修改(click事件)

第一步、获取值

//(获得文本框的值)
string TypeName = this.textBox2.Text;
//(判断复选框是否选中)选中了给IsAddBed赋值为”是“,否则为”否“
            string IsAddBed = "";
            if (this.checkBox1.Checked)
            {
                IsAddBed = "是";
            }else{
                IsAddBed = "否";
            }
            //(判断单选按钮是否选中)如果男性单选按钮选中了,给sex赋值为”男“,否则为”女“
          string sex = "";
            if (this.radioButton1.Checked)
            {
                sex = "男";
            }
If(this.radioButton2.Checked){
                sex = "女";
            }
          //(获取下拉框中选中的value值)
            string index= this.comboBox1.SelectedValue.ToString();
             //(获得下拉框中选中的文本值)
            string gender = this.comboBox1.text;

 

第二步、为空判断

if (TypeName==""|| IsAddBed ==""|| sex ==""|| gender =="")
            {
                MessageBox.Show("信息填写不完整,请重新填写!");
//返回
                return;
            }


 

第三步、定义sql语句(根据typeId修改)

//(typeID是全局变量,从DataGridView控件的cellClick事件中获取选中的隐藏的类型ID)
           string sql = string.Format("update RoomType set TypeName='{0}',TypePrice='{1}',AddBedPrice='{2}',IsAddBed='{3}',Remark='{4}' where TypeID='{5}'", TypeName, TypePrice, AddBedPrice, IsAddBed, Remark, typeID);
第四步、调用dbhelper类增删改方法
            int result = DBHelper.Zsg(sql);
 
第五步、判断执行结果
             if (result>0)
            {
                MessageBox.Show("修改成功!");
     //这个是刷新DataGridView列表
string sql1 = "select * from RoomType";
DataTable dt = DBHelper.getDataTable(sql1);
this.dataGridView1.DataSource = dt;
 
             }
            else
            {
                MessageBox.Show("修改失败!");
}

删除(Click事件)

//定义sql语句(typeID是全局变量,从DataGridView控件的cellClick事件中获取选中的隐藏的类型ID)
string sql = string.Format("delete from RoomType where TypeID='{0}'",  typeID);
//调用dbhelper类增删改方法
int result = DBHelper.Zsg(sql);
 
//判断执行结果
if (result > 0)
{
MessageBox.Show("删除成功!");
 
//这个是刷新DataGridView列表
string sql1 = "select * from RoomType";
DataTable dt = DBHelper.getDataTable(sql1);
this.dataGridView1.DataSource = dt;
 
 
}
else
     {
           MessageBox.Show("删除失败!");
      }

 

点击关闭时再次确定是否关闭(FormClosing事件)

//弹出自定义对话框
DialogResult dr = MessageBox.Show("确定要关闭吗?","提示:",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
//判断用户点击哪个按钮
if (dr==DialogResult.Yes)
{
      //如果用户选择了“是”,执行关闭
      Application.Exit();
} else{
      //如果用户选择了“否”,取消窗体关闭事件
      e.Cancel = true;
}

 最后再来一个DBHelper类的笔记:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GMP
{
    class DBHelper
    {
        //数据库链接字符串
        public static string ConnString = "server=.;database=;uid=;pwd=";
        //数据库链接对象
        public static SqlConnection Conn = null;
        // 初始化数据库链接
        public static void InitConnection()
        {
            // 如果链接对象不存在,则打开链接
            if (Conn == null)
            {
                Conn = new SqlConnection(ConnString);
            }
            // 如果链接对象关闭,则打开链接
            if (Conn.State == System.Data.ConnectionState.Closed)
            {
                Conn.Open();
            }
            // 如果链接中断,则重启链接
            if (Conn.State == System.Data.ConnectionState.Broken)
            {
                Conn.Close();
                Conn.Open();
            }

        }
        // 查询,获取DataReader
        public static SqlDataReader GetDataReader(string SqlStr)
        {
            InitConnection();
            SqlCommand cmd = new SqlCommand(SqlStr, Conn);
            // CommandBehavior.CloseConnection 命令行为,当DataReader对象被关闭时,自动关闭
            // 占用的链接对象
            return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
        }
        //增、删、改操作
        public static bool ExecuteNonQuery(string sqlStr)
        {
            InitConnection();
            SqlCommand cmd = new SqlCommand(sqlStr, Conn);
            int result = cmd.ExecuteNonQuery();
            Conn.Close();
            return result > 0;
        }
        // 执行集合函数
        public static object ExecuteScalar(string sqlStr)
        {
            InitConnection();
            SqlCommand cmd = new SqlCommand(sqlStr, Conn);
            object result = cmd.ExecuteScalar();
            Conn.Close();
            return result;
        }
    }
}


0

评论 (0)

取消