您的位置:首页 > 其它

电影记录管理系统5[用户注册]

2012-09-20 19:03 465 查看
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Configuration;

using System.Data.SqlClient;

namespace 电影记录管理系统

{

public partial class FrmRegister1 : Form

{

public FrmRegister1()

{

InitializeComponent();

}

//连接到配置文件App.config

static string connStr = ConfigurationManager.ConnectionStrings["str"].ConnectionString;

private void btnOK_Click(object sender, EventArgs e)

{

//否则判断 当前用户名在数据库中是否存在

//使用sql select语句 按 用户文本框中的文本内容 来选取数据库中的用户名

string sql = "select MUserName from MovieAccount where MUserName='" + txtUid.Text + "'";

//使用sql语句来连接数据库

SqlConnection conn = new SqlConnection(connStr);

//创建一个cmd对象

SqlCommand cmd = new SqlCommand(sql, conn);

//打开数据库连接

conn.Open();

//使用SqlDataReader 对cmd 执行读取指令的数据 进行逐行读取

SqlDataReader sdr = cmd.ExecuteReader();

//如果读到数据了,说明 数据库中已存在 文本框中输入的用户名

if (sdr.Read())

{

//如果读到相同的用户名 则提示 用户名已存在

lblUidMsg.Text = "用户名已存在,请重新输入!";

return;

}

//如果用户名不存在 则判断下用户名密码是否为空

else if (txtUid.Text.Trim() == "")

{

//如果用户名 不为空, 将提示语句赋给label框 并在窗体上

lblUidMsg.Text = "用户名不能为空!";

}

//判断密码是否为空

else if (txtPwd.Text.Trim() == "")

{

//同理

lblPwdMsg.Text = "密码不能为空!";

//同时清除 用户名 提示框的内容

lblUidMsg.Text = "";

}

//判断再次输入密码是否为空

else if (txtPwdConfirm.Text.Trim() == "")

{

//同理

lblPwdConfirmMsg.Text = "验证密码不能为空!";

//同时清除 用户名 提示框 和 第一次密码输入提示框的内容

lblUidMsg.Text = "";

lblPwdMsg.Text = "";

}

//判断2次密码输入是否相同

else if (txtPwd.Text.Trim() != txtPwdConfirm.Text.Trim())

{

//同理

lblPwdMsg.Text = "2次密码必须一样!";

lblPwdConfirmMsg.Text = "请重新输入!";

return;

}

else

{

//清空上面所有label框的提示

lblUidMsg.Text = "";

lblPwdMsg.Text = "";

lblPwdConfirmMsg.Text = "";

//关闭数据库,节省资源

conn.Close();

//使用sql 数据插入语句

string sqlInsert = "insert into MovieAccount(MUserName,MUserPwd) values(@MUserName,@MUserPwd)";

SqlParameter[] param = {

new SqlParameter("@MUserName",txtUid.Text),

new SqlParameter("@MUserPwd",txtPwd.Text)

};

//使用 SQL数据库连接

SqlConnection connInsert = new SqlConnection(connStr);

//使用SQL command 指令来装载 SQL查询语句和SQL数据库连接

SqlCommand cmdInsert = new SqlCommand(sqlInsert, connInsert);

//再次打开数据连接 执行插入数据操作

connInsert.Open();

//使用cmd指令 添加所有参数

cmdInsert.Parameters.AddRange(param);

//定义一个整型变量 用于接受 cmd的 执行查询指令

int n = cmdInsert.ExecuteNonQuery();

//判断该值是否为空, 如果为空, 说明插入数据失败

if (n == 0)

{

MessageBox.Show("注册失败!,请重新输入");

return;

}

else

{

MessageBox.Show("注册成功!");

}

//关闭数据库连接

connInsert.Close();

}

}

private void btnBack_Click(object sender, EventArgs e)

{

//new一个main窗体

FrmMain main = new FrmMain();

//显示main窗体

main.Show();

//隐藏当前的窗体

this.Hide();

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: