c#注册界面的代码
时间: 2023-10-28 20:14:21 浏览: 71
以下是一个简单的 C# 注册界面代码示例,其中包括了一些基本的输入验证和数据存储功能。
```csharp
using System;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace RegisterForm
{
public partial class Register : Form
{
public Register()
{
InitializeComponent();
}
private void registerButton_Click(object sender, EventArgs e)
{
string username = usernameTextBox.Text.Trim();
string password = passwordTextBox.Text.Trim();
string confirmPassword = confirmPasswordTextBox.Text.Trim();
// 判断输入是否为空
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(confirmPassword))
{
MessageBox.Show("请输入完整信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
// 判断密码和确认密码是否一致
if (password != confirmPassword)
{
MessageBox.Show("密码和确认密码输入不一致!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
try
{
// 连接数据库
SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=TestDB;Integrated Security=True");
conn.Open();
// 检查用户名是否已存在
SqlCommand checkUser = new SqlCommand("SELECT COUNT(*) FROM UserInfo WHERE UserName=@UserName", conn);
checkUser.Parameters.AddWithValue("@UserName", username);
int count = (int)checkUser.ExecuteScalar();
if (count > 0)
{
MessageBox.Show("该用户名已存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
conn.Close();
return;
}
// 插入新用户数据
SqlCommand insertUser = new SqlCommand("INSERT INTO UserInfo(UserName, Password) VALUES (@UserName, @Password)", conn);
insertUser.Parameters.AddWithValue("@UserName", username);
insertUser.Parameters.AddWithValue("@Password", password);
insertUser.ExecuteNonQuery();
MessageBox.Show("注册成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show("注册失败:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void cancelButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
```
在代码中,我们首先获取用户输入的用户名、密码和确认密码,然后进行了一些基本的输入验证,例如判断输入是否为空,判断密码和确认密码是否一致等。接着使用 `SqlConnection` 类连接数据库,检查用户名是否已存在,如果不存在则将新用户数据插入到数据库中。最后显示相应的提示信息,完成注册功能。需要注意的是,上述代码仅为示例,实际应用中还需要进行更加完善的安全性处理。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)