小编典典

C#登录屏幕-在SQL表和更新字段中查找用户

sql

我目前正在编写一个小应用程序,以跟踪货币的来龙去脉,这只是为了提高我的C#常规技能。目前,对于我的登录屏幕,我具有以下代码

    private void Login_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'spendingInsAndOutsDataSet.Users' table. You can move, or remove it, as needed.
        this.usersTableAdapter.Fill(this.spendingInsAndOutsDataSet.Users);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string userNameText = userName.Text;
        string passwordText = password.Text;

        foreach (DataRow row in spendingInsAndOutsDataSet.Users)
        {
            if (row.ItemArray[4].Equals(userNameText) && row.ItemArray[5].Equals(passwordText))
            {
                MessageBox.Show("Login Successful");

                MainGUI newForm = new MainGUI();
                this.Visible = false;
                newForm.Show();
                break;
            }
            else
            {
                userName.Text = String.Empty;
                password.Text = String.Empty;
                MessageBox.Show("Login Failed");
                break;
            }
        }
    }

登录成功后,我要执行的操作是将当前PC的MachineName写入我的SQL数据库中“用户”表中的字段。这样,当我开始创建记录时,我可以快速找到我的UsersId(这是我的Transactions表中的外键)。

我知道您可以使用System.Enviroments路径获得“活动计算机名称”,但是我不确定确切如何去编写更新。我知道如何使用SqlCommand来做到这一点,但我想知道是否有一种更简单的方法来使用在ForEach循环中使用的DataRows。

预先谢谢您,任何问题都让我知道。

詹姆士


阅读 181

收藏
2021-03-08

共1个答案

小编典典

假设它是一个Access数据库(如果没有,则进行必要的更改):

使用适配器将结果填充到表格中。然后将行列与用户提供的信息进行比较。不要忘记使用参数来避免可能会破坏您的数据库或将您的用户信息暴露给黑客的注入。

DataTable dt = new DataTable();
String sql = "SELECT * FROM users WHERE user = @user and password=@password"
OleDbConnection connection = getAccessConnection();
OleDbDataAdapter da = new OleDbDataAdapter(sql, connection);
da.SelectCommand.Parameters.Add("@user", OleDbType.VarChar).Value = userNameText;
da.SelectCommand.Parameters.Add("@password", OleDbType.VarChar).Value = password.Text;
try
{
   connection.Open();
   da.Fill(dt);
   connection.Close();
}
catch(OleDbException ex)
{
   connection.Close();
   MessageBox.Show(ex.ToString());
}

if(dt.Rows.Count == 1)
    return true; //username && password matches
else if(dt.Rows.Count == 0)
    return false; // does not match

您也可以使用AddWithValue作为参数。

da.SelectCommand.Parameters.AddWithValue("@user", userNameText);

getAccessConnection()是预定义的OleDbConnection函数,具有与数据库设置的连接,并为您(我为自己创建的)创建连接的新实例。

public OleDbConnection getAccessConnection()
{
    this.connection = new OleDbConnection();
    this.connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" 
            + Classified.SOURCE + ";Jet OLEDB:Database Password=" 
            + Classified.PASS + ";";
    return this.connection;
}

对于可能加入该项目的开发人员,最好为所有这些功能创建类。还请阅读有关C#测试驱动的开发的文章。

同样,即使该一条记录失败,您的循环也会中断,仅允许其转到第一条记录。

创建自己的数据集并用查询表填充数据集也很有用。这是一个简单的示例:

DataSet ds = new DataSet();
ds.Tables.Add(dt, "userSearchedTable");
ds.Tables["userSearchedTable"].Rows[0][1].ToString();

然后,您可以在需要时在集合中声明特定的数据表。

2021-03-08