您的位置:首页 > 数据库

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

2008-09-17 22:44 471 查看





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;//初始化ID为0
//注意:这里的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代码写出来供大家参考。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐