C# 窗体应用编写一个登录页面跳转窗体连接sql数据库
时间: 2023-06-24 16:05:10 浏览: 120
好的,以下是实现步骤:
1. 创建一个 C# 窗体应用程序,命名为 LoginDemo。
2. 在 LoginDemo 窗体中设计一个登录页面,包括用户名和密码的输入框、登录按钮和注册按钮。
3. 设计一个注册页面,包括用户名、密码、确认密码和邮箱的输入框以及注册按钮。
4. 在数据库中创建一个名为 LoginDemo 的数据库,在其中创建一个名为 Users 的表,包括 id、username 和 password 三个字段。
5. 在 Visual Studio 中打开 Server Explorer,连接到本地的 SQL Server 数据库,找到 LoginDemo 数据库,在其中创建 Users 表。
6. 在 LoginDemo 窗体中,添加连接数据库的代码。在窗体中右键单击,选择“添加->新建项->数据->数据集”,在数据集设计器中添加一个新的数据集,命名为 LoginDemoDataSet。
7. 在数据集设计器中添加一个新的数据表适配器,命名为 UsersTableAdapter,设置它的 SQL 语句为“SELECT id, username, password FROM Users”。
8. 在 LoginDemo 窗体中,添加代码连接到数据库并获取用户输入的用户名和密码,判断用户名和密码是否正确,并根据结果跳转到主窗体或者弹出错误提示框。
9. 在注册页面中,添加代码将用户输入的用户名和密码插入到数据库中,并跳转到登录页面。
示例代码如下:
```
// 连接数据库
SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=LoginDemo;Integrated Security=True");
conn.Open();
// 获取用户输入的用户名和密码
string username = txtUsername.Text;
string password = txtPassword.Text;
// 查询数据库中是否存在该用户
SqlCommand cmd = new SqlCommand("SELECT * FROM Users WHERE username=@username AND password=@password", conn);
cmd.Parameters.AddWithValue("@username", username);
cmd.Parameters.AddWithValue("@password", password);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
// 用户名和密码正确,跳转到主窗体
Mainform mainform = new Mainform();
mainform.Show();
this.Hide();
}
else
{
// 用户名或密码错误,弹出错误提示框
MessageBox.Show("用户名或密码错误!");
}
// 关闭数据库连接
conn.Close();
```
在注册页面中,添加插入数据的代码:
```
// 连接数据库
SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=LoginDemo;Integrated Security=True");
conn.Open();
// 获取用户输入的用户名、密码和邮箱
string username = txtUsername.Text;
string password = txtPassword.Text;
string email = txtEmail.Text;
// 插入数据到数据库中
SqlCommand cmd = new SqlCommand("INSERT INTO Users(username, password, email) VALUES(@username, @password, @email)", conn);
cmd.Parameters.AddWithValue("@username", username);
cmd.Parameters.AddWithValue("@password", password);
cmd.Parameters.AddWithValue("@email", email);
cmd.ExecuteNonQuery();
// 关闭数据库连接
conn.Close();
// 跳转到登录页面
Loginform loginform = new Loginform();
loginform.Show();
this.Hide();
```
注意:以上示例代码仅供参考,具体实现根据你的需求进行修改。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)