zoukankan      html  css  js  c++  java
  • c# WinForms 通过ComboBox控件 引用数据库表与修改数据库

     

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    using System.Data.SqlClient;

     

    namespace QQ

    {

        public partial class frmRegist : Form

        {

            public frmRegist()

            {

                InitializeComponent();

            }

     

            private void frmRegist_Load(object sender, EventArgs e)

            {

                DBHelper.conn.Open();

                //将数据库的星座表数据添加到注册窗体的星座选项

                SqlCommand cmdStar = new SqlCommand("select Star from Star",DBHelper.conn);

                SqlDataReader drStar = cmdStar.ExecuteReader();

                while (drStar.Read())

                {

                    this.cboStar.Items.Add((string)drStar[0]);

                }

                drStar.Close();

                //将数据库的血型表数据添加到注册窗体的血型选项

                SqlCommand cmdBlood = new SqlCommand("select BloodType from BloodType",DBHelper.conn);

                SqlDataReader drBlood = cmdBlood.ExecuteReader();

                while (drBlood.Read())

                {

                    this.cboBlood.Items.Add(drBlood[0]);

                }

                drBlood.Close();

                DBHelper.conn.Close();

            }

            //单击取消按钮来关闭窗口

            private void btnCancle_Click(object sender, EventArgs e)

            {

                this.Close();

            }

            //单击注册按钮来验证注册

            private void btnReg_Click(object sender, EventArgs e)

            {

                if (this.txtNickRge.Text=="")

                {

                    MessageBox.Show("昵称不能为空!","注册提示",MessageBoxButtons.OK,MessageBoxIcon.Information);

                }

                else if (this.txtNickRge.Text.Length>12)

                {

                    MessageBox.Show("昵称长度限制为12字符!(每个汉字占2个字符)", "注册提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }

                else if(this.txtAgeReg.Text=="")

                {

                    MessageBox.Show("年龄不能为空!", "注册提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }

                else if (Convert.ToInt32(this.txtAgeReg.Text) < 0)

                {

                    MessageBox.Show("年龄必须为不小于0", "注册提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }

                else if(this.txtPswReg.Text==""||this.txtPswConfReg.Text=="")

                {

                    MessageBox.Show("密码不能为空", "注册提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }

                else if (this.txtPswReg.Text!=this.txtPswConfReg.Text)

                {

                    MessageBox.Show("两次输入的密码不一致!", "注册提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }

                    //如果输入的信息合法则插入数据库

                else

                {

                    DBHelper.conn.Open();

                    int starID = getStarID();//调用方法getStarID()获得对应的星座ID

                    //int bloodId = getBloodTypeID();//调用方法getBloodTypeID()获得对应的血型ID

                    //执行插入操作

                    SqlCommand cmd = new SqlCommand(String.Format("insert into Users(NickName,LoginPsw,UserState,FriendShipPolicyID,Sex,Age,Name,StarID,BloodTypeID) Values('{0}','{1}','离线',2,'{2}',{3},'{4}',{5},2)", this.txtNickRge.Text, this.txtPswReg.Text, (this.rdoManReg.Checked == true) ? this.rdoManReg.Text : this.rdoWomanReg.Text, Convert.ToInt32(this.txtAgeReg.Text), this.txtRealName.Text,starID), DBHelper.conn);

                    int result = cmd.ExecuteNonQuery();

                    //若插入成功

                    if (result==1)

                    {

                        try

                        {

                            //查询刚才插入的记录对应的自动增长列(为ID

                            SqlCommand cmd2 = new SqlCommand(string.Format("select top 1 ID from Users order by ID DESC"),DBHelper.conn);

                            string ID = cmd2.ExecuteScalar().ToString();

                            MessageBox.Show("恭喜您!注册成功!/n您的QQ号码为:" + ID, "注册提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        }

                        catch (Exception ex)

                        {

                            MessageBox.Show(ex.Message,"位置1");

                        }

                        finally

                        {

                            DBHelper.conn.Close();

                        }

                    }

                   

                }

            }

     

            //获得星座ID

            private int getStarID()

            {

                int ID = 0;//初始化ID0

                //注意:这里的cboStar.SelectedItem就是在窗体中选择的星座,不同于selectedIndex(所选择项的索引)

                SqlCommand cmd = new SqlCommand(string.Format("select ID from Star where Star='{0}'", this.cboStar.SelectedItem.ToString()), DBHelper.conn);

                SqlDataReader dr = cmd.ExecuteReader();

                //查询到后就将Star表中的ID赋值给返回值ID

                if (dr.Read())

                {

                    ID = Convert.ToInt32(dr[0]);

                }

                //别忘了关闭DataReader

                dr.Close();

                return ID;

            }

     

            //获得血型ID,getStarID()神似!

            private int getBloodTypeID()

            {

                int ID = 0;

                return ID;

            }

     

        }

    }

    这里一个类QQ的局域网聊天工具的注册界面为例,与直接利用VS控件绑定数据库不同的是,利用代码来实现窗体与数据库的联动效果。将窗体frmRegist 的部分CS代码写出来供大家参考。

  • 相关阅读:
    【KVM系列 virt-v2v】virt-v2v过程中的报错
    大机与超级计算机的区别
    多路径 on linux
    ansible 管控 windows
    Linux启动盘
    RHCA 环境命令
    Xshell 4 连接 Ubuntu/Kali 报错 "找不到匹配的key exchange算法"
    security group & ACL
    windows powercfg
    OKD
  • 原文地址:https://www.cnblogs.com/black/p/5171991.html
Copyright © 2011-2022 走看看