c# WinForms 通过ComboBox控件 引用数据库表与修改数据库

 

using System;<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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代码写出来供大家参考。

转载于:https://www.cnblogs.com/black/archive/2008/09/17/5171991.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值