我有一个表单 (F1),用户将在其中提供各自的凭证 username 和 password 。
成功登录后,控件将移动到客户表单 (F2) 并在其标签中显示欢迎用户名。
客户表格包含:
- 标签和文本框(姓名、地址、职能……)
- 按钮插入
- DataGridView 绑定(bind)到数据库(名称、地址、函数、..、UserId)
现在,我想插入一个客户端。
填充文本框后,我想添加一个客户端,并显示它由已连接的用户添加。
Ex: 如果我在添加客户端后使用用户名 Rose 登录,在我的 datagridView 中,显示我由 Rose 添加的插入行。
我的登录代码并将用户名传递给客户表单
private void btnLogin_Click(object sender, EventArgs e)
{
try
{
//textBox2.Text = Encrypt(textBox2.Text);
SqlConnection con = new SqlConnection("Data Source=User-PC\\SQLEXPRESS;Initial Catalog=timar;Integrated Security=True");
SqlDataAdapter sda = new SqlDataAdapter("select Username from [User] where Username='" + textBox1.Text + "' and Password='" + textBox2.Text + "'", con);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count == 1)
{
this.Hide();
Client c = new Client(dt.Rows[0][0].ToString());
v.Show();
}
else if (dt.Rows.Count > 1)
{
MessageBox.Show("Nom d'utilisateur et Mot de passe dupliqué !");
}
else
MessageBox.Show("Nom d'utilisateur ou Mot de passe incorrecte !");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
这是我的插入代码:
public Client(string username)
{
InitializeComponent();
lblUser.Text = username;
DisplayData();
FillData();
}
private void button1_Click(object sender, EventArgs e)
{
if ( comboBox2.SelectedValue != null && textBox1.Text != string.Empty && textBox2.Text != string.Empty && textBox4.Text != string.Empty)
{
string cmdStr = "Insert into Client (idUser,name,address,function,telephone,commentaire)values (@idUser,@name,@,address,@function,@telephone,@commentaire)";
SqlConnection con = new SqlConnection("Data Source=User-PC\\SQLEXPRESS;Initial Catalog=timar;Integrated Security=True");
SqlCommand cmd = new SqlCommand(cmdStr, con);
con.Open();
//The problem in the line below how Can I get the id of username,Error cannot convert string Rose to int.
cmd.Parameters.AddWithValue("@idUser",label.Text);
cmd.Parameters.AddWithValue("@name", (comboBox2.SelectedValue));
cmd.Parameters.AddWithValue("@,address", textBox1.Text);
cmd.Parameters.AddWithValue("@function", textBox2.Text);
cmd.Parameters.AddWithValue("@telephone", textBox4.Text);
cmd.Parameters.AddWithValue("@commentaire",txtArchive.Text);
int LA = cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Le Client a été ajouter avec succés !","Saisie Rendez-vous", MessageBoxButtons.OK, MessageBoxIcon.Information);
DisplayData();
ClearData();
}
else
{
MessageBox.Show("Vérifier que tous les champs sont remplis !","Erreur",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
我不知道该怎么做,我是 c# 的新手,正在尝试学习。
提前致谢。
最佳答案
检查登录时,这样写你的查询:
SELECT [Id], [UserName] from [Users] WHERE [UserName]=@UserName AND [Password]=@Password
然后存储登录成功时查询得到的[Id]
和[UserName]
(结果集包含一条记录)。这样您就可以在每次需要时使用登录用户的用户名和 ID。
例如:
var cmd = @"SELECT [Id], [UserName] FROM [Users] " +
@"WHERE [UserName] = @UserName AND [Password] = @Password";
var cn = @"Data Source=User-PC\SQLEXPRESS;Initial Catalog=timar;Integrated Security=True";
var da = new SqlDataAdapter(cmd, cn);
da.SelectCommand.Parameters.AddWithValue("@UserName", textBox1.Text);
da.SelectCommand.Parameters.AddWithValue("@Password", textBox2.Text);
var dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count == 1)
{
int id = dt.Rows[0].Field<int>("Id");
string userName = dt.Rows[0].Field<string>("UserName");
//...
}
注意:
- 您应该使用参数化查询来防止 SQL 注入(inject)攻击。
关于c# - 按连接 Winforms 的用户插入记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38264076/