SQL登录系统

时间:2012-12-04 23:32:20

标签: c# sql database login

这是我第一次在StackOverflow上提问,所以如果我问某人不当,我会提前道歉。在过去几天研究这个问题时,我找不到任何帮助我,所以提前感谢任何想要帮助的人。

我正在建立一个允许人们注册和登录的数据库。我在VS2012中使用C#。 下面是我的登录代码,我在测试时遇到了一些麻烦。它遍历数据库中的每个人,并告诉我登录失败,直到它到达正确的用户。

    private void button1_Click_1(object sender, EventArgs e)
    {
        try
        {
            cn.Open();
        }
        catch (Exception)
        {
            MessageBox.Show("Did not connect");
        }


        SqlCommand cmd = new SqlCommand("SELECT * FROM [Users]", cn);
        cmd.Connection = cn;
        SqlDataReader reader = null;
        reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            if (textBox1.Text == (reader["Username"].ToString()) && textBox2.Text == (reader["Password"].ToString()))
            {
                MessageBox.Show("Logged in");
            }
            else
            {
                MessageBox.Show("Login has failed. Please check your Username and Password.");
            }
        }
        cn.Close();
    }

至于我的注册部分,我不确定它是VS2012的东西还是什么,但是在我结束调试之后信息没有保存到数据库中,然后又重新进行调试。

    private void button1_Click_1(object sender, EventArgs e)
    {
        cn.Open();
        SqlCommand cm1 = new SqlCommand("INSERT INTO Users (Username, Password) VALUES (@Username, @Password)", cn);
        SqlCommand cm2 = new SqlCommand("INSERT INTO Contact(Name, Address, City, State, PostalCode, Email, PhoneNumber) VALUES(@Name, @Address, @City, @State, @PostalCode, @Email, @PhoneNumber)", cn);



        cm1.Parameters.AddWithValue("@Username", textBox1.Text);
        cm1.Parameters.AddWithValue("@Password", textBox2.Text);
        cm2.Parameters.AddWithValue("@Name", textBox3);
        cm2.Parameters.AddWithValue("@Address", textBox4);
        cm2.Parameters.AddWithValue("@City", textBox5);
        cm2.Parameters.AddWithValue("@State", textBox6);
        cm2.Parameters.AddWithValue("@PostalCode", textBox7);
        cm2.Parameters.AddWithValue("@Email", textBox8);
        cm2.Parameters.AddWithValue("@PhoneNumber", textBox9);

        try
        {
            int affectedRows =  cm1.ExecuteNonQuery(); //+cm2.ExecuteNonQuery();

            if (affectedRows > 0)
            {
                MessageBox.Show("Insert Sucsess!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("Insert Failed!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        cn.Close();
    }

2 个答案:

答案 0 :(得分:4)

当您在项目中有数据库文件并构建项目时,可以将数据库文件从根项目文件夹复制到输出(bin \ debug或bin \ release)文件夹中。
此行为由数据库文件的Copy To Output Directory属性控制。

如果您将此属性设置为Copy Always,则每次构建项目时,都会将数据库文件的新副本从根项目文件夹复制到输出目录,覆盖已存在的文件并销毁更改你在之前的调试会话中做过。

建议的解决方法是将此属性更改为Copy NeverCopy if Newer

请参阅MSDN at this page

的详细说明

对于问题的第一部分,您可以避免循环每个用户向您的sql文本添加WHERE子句。请注意,您永远不应该使用字符串连接来构建您的sql字符串,而是使用ALWAYS参数。 (为什么?你避免使用Sql Injection和文本单引号解析/加倍)

string sqlText = "SELECT * FROM [Users] WHERE Username = @usr AND [Password] = @psw";
SqlCommand cmd = new SqlCommand(sqlText, cn);
cmd.Parameters.AddWithValue("@usr", textbox1.Text);
cmd.Parameters.AddWithValue("@psw", textbox2.Text);
SqlDataReader reader = cmd.ExecuteReader();
if(reader.HasRows)
    // You have found the user....

另一点建议。不要将密码以明文形式存储在数据库中。始终存储此字符串的哈希值,并在搜索时计算哈希值并搜索它而不是清除密码。

答案 1 :(得分:2)

为了使您能够正常工作,您需要WHERE中的SELECT条款。但是,我不建议使用

SqlCommand cmd = new SqlCommand("SELECT * FROM [Users] WHERE Username='" + textBox1.Text + "'", cn);

因为可能SQL injection

请了解如何使用Stored Procedureshow to Execute them from your C# code

相关问题