请写出c#代码
时间: 2024-02-25 11:57:34 浏览: 245
好的,下面是一个简单的C#用户登录管理系统的示例代码:
1. 创建用户数据表的SQL语句:
```
CREATE TABLE [dbo].[users] (
[id] INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
[username] VARCHAR(50) NOT NULL,
[password] VARCHAR(50) NOT NULL,
[name] VARCHAR(50) NOT NULL,
[phone] VARCHAR(50) NOT NULL,
[email] VARCHAR(50) NOT NULL
);
```
2. 用户登录界面的代码:
```
private void btnLogin_Click(object sender, EventArgs e)
{
string username = txtUsername.Text.Trim();
string password = txtPassword.Text.Trim();
// 查询数据库中是否存在该用户
string sql = "SELECT * FROM users WHERE username=@username AND password=@password";
SqlParameter[] parameters = {
new SqlParameter("@username", SqlDbType.VarChar, 50),
new SqlParameter("@password", SqlDbType.VarChar, 50)
};
parameters[0].Value = username;
parameters[1].Value = password;
DataTable dt = SqlHelper.ExecuteDataTable(sql, parameters);
if (dt.Rows.Count > 0)
{
// 登录成功,保存用户信息并关闭当前窗口
User user = new User();
user.Id = Convert.ToInt32(dt.Rows[0]["id"]);
user.Username = dt.Rows[0]["username"].ToString();
user.Name = dt.Rows[0]["name"].ToString();
user.Phone = dt.Rows[0]["phone"].ToString();
user.Email = dt.Rows[0]["email"].ToString();
this.DialogResult = DialogResult.OK;
this.Close();
}
else
{
MessageBox.Show("用户名或密码错误!");
}
}
```
3. 用户管理界面的代码:
```
private void btnAdd_Click(object sender, EventArgs e)
{
UserForm form = new UserForm();
if (form.ShowDialog() == DialogResult.OK)
{
// 添加新用户到数据库
string sql = "INSERT INTO users(username, password, name, phone, email) VALUES(@username, @password, @name, @phone, @email)";
SqlParameter[] parameters = {
new SqlParameter("@username", SqlDbType.VarChar, 50),
new SqlParameter("@password", SqlDbType.VarChar, 50),
new SqlParameter("@name", SqlDbType.VarChar, 50),
new SqlParameter("@phone", SqlDbType.VarChar, 50),
new SqlParameter("@email", SqlDbType.VarChar, 50)
};
parameters[0].Value = form.User.Username;
parameters[1].Value = form.User.Password;
parameters[2].Value = form.User.Name;
parameters[3].Value = form.User.Phone;
parameters[4].Value = form.User.Email;
int rows = SqlHelper.ExecuteNonQuery(sql, parameters);
if (rows > 0)
{
MessageBox.Show("添加用户成功!");
BindUserList();
}
else
{
MessageBox.Show("添加用户失败!");
}
}
}
private void btnEdit_Click(object sender, EventArgs e)
{
if (dgvUserList.SelectedRows.Count > 0)
{
int id = Convert.ToInt32(dgvUserList.SelectedRows[0].Cells["Id"].Value);
UserForm form = new UserForm();
form.User = GetUserById(id);
if (form.ShowDialog() == DialogResult.OK)
{
// 更新用户信息到数据库
string sql = "UPDATE users SET username=@username, password=@password, name=@name, phone=@phone, email=@email WHERE id=@id";
SqlParameter[] parameters = {
new SqlParameter("@username", SqlDbType.VarChar, 50),
new SqlParameter("@password", SqlDbType.VarChar, 50),
new SqlParameter("@name", SqlDbType.VarChar, 50),
new SqlParameter("@phone", SqlDbType.VarChar, 50),
new SqlParameter("@email", SqlDbType.VarChar, 50),
new SqlParameter("@id", SqlDbType.Int)
};
parameters[0].Value = form.User.Username;
parameters[1].Value = form.User.Password;
parameters[2].Value = form.User.Name;
parameters[3].Value = form.User.Phone;
parameters[4].Value = form.User.Email;
parameters[5].Value = id;
int rows = SqlHelper.ExecuteNonQuery(sql, parameters);
if (rows > 0)
{
MessageBox.Show("修改用户成功!");
BindUserList();
}
else
{
MessageBox.Show("修改用户失败!");
}
}
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (dgvUserList.SelectedRows.Count > 0)
{
int id = Convert.ToInt32(dgvUserList.SelectedRows[0].Cells["Id"].Value);
// 从数据库中删除该用户
string sql = "DELETE FROM users WHERE id=@id";
SqlParameter[] parameters = {
new SqlParameter("@id", SqlDbType.Int)
};
parameters[0].Value = id;
int rows = SqlHelper.ExecuteNonQuery(sql, parameters);
if (rows > 0)
{
MessageBox.Show("删除用户成功!");
BindUserList();
}
else
{
MessageBox.Show("删除用户失败!");
}
}
}
private void BindUserList()
{
string sql = "SELECT * FROM users";
DataTable dt = SqlHelper.ExecuteDataTable(sql);
dgvUserList.DataSource = dt;
}
private User GetUserById(int id)
{
string sql = "SELECT * FROM users WHERE id=@id";
SqlParameter[] parameters = {
new SqlParameter("@id", SqlDbType.Int)
};
parameters[0].Value = id;
DataTable dt = SqlHelper.ExecuteDataTable(sql, parameters);
User user = new User();
user.Id = Convert.ToInt32(dt.Rows[0]["id"]);
user.Username = dt.Rows[0]["username"].ToString();
user.Password = dt.Rows[0]["password"].ToString();
user.Name = dt.Rows[0]["name"].ToString();
user.Phone = dt.Rows[0]["phone"].ToString();
user.Email = dt.Rows[0]["email"].ToString();
return user;
}
```
以上代码仅供参考,具体实现还需要根据实际需求进行调整和完善。
阅读全文