使用sql数据库登录表单c#

时间:2014-10-08 17:06:10

标签: c# sql winforms login sql-server-ce

我正在尝试制作一个注册页面。你将用户名和密码及其保存放在sql数据库中。

注意:evrey一直有效,直到我添加第二列(密码)。

这是密码代码(用户名是相同的):

    static public void delete(string _Password)
    {
        try
        {
            connection.Open();
            SqlCeCommand commandInsert = new SqlCeCommand("INSERT INTO [Table] VALUES(@Password)", connection);
            commandInsert.Parameters.Add("Password", _Password);
            commandInsert.ExecuteNonQuery();
        }
        catch (SqlCeException expection)
        {
            MessageBox.Show(expection.ToString());
        }
        finally
        {
            connection.Close();
        }
    }

这是按钮设置:

 private void button1_Click(object sender, EventArgs e)
            {
                if (deleteBox.Text != "")
                {
                    SQLFunctions.Insert(insertBox.Text);
                    SQLFunctions.delete(deleteBox.Text);
                    SQLFunctions.Refresh(this.dataGridView1);

                }
                else
                {
                    MessageBox.Show("login failed");
                }
            }

感谢

1 个答案:

答案 0 :(得分:0)

我想你需要像这样修改你的代码

SqlCeCommand commandInsert = new SqlCeCommand("INSERT INTO [Table] VALUES(@username,@Password)", connection);

你应该传递两个参数,因为你现在在db中有两个字段,如果你不想传递用户名,那么你应该像这样指定这个东西

SqlCeCommand commandInsert = new SqlCeCommand("INSERT INTO [Table](password) VALUES(@Password)", connection);

以及

commandInsert.Parameters.Add("Password", _Password);

这应该写成如下

commandInsert.Parameters.Add("@Password", _Password);

并将值传递给此参数,您可以像这样执行

  command.Parameters.AddWithValue("@Password", insertBox.Text);