我是 C# 新手,我正在尝试创建一个登录页面,用户可以在其中输入用户名和密码。然后,从数据库中读取它以验证用户名和密码是否在其中。我已经建立了连接,它似乎正在工作,因为在这段代码中,如果我输入列的标题,它将让我登录,但不能登录该列中的任何值。我如何让它成为它接受的值?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace BrokerSolution
{
public partial class LogIn : Form
{
public static String User ;
public static String pass;
public LogIn()
{
InitializeComponent();
}
private bool ValidateFunction(string User, string pass)
{
bool boolReturnvalue = false;
String strConnection = "Server=****; Database=*****; User Id=******; password=*****";
SqlConnection UGIcon = new SqlConnection(strConnection);
String LogInQuery = "SELECT user_name, sign_in FROM Users";
SqlCommand loginCommand = new SqlCommand(LogInQuery, UGIcon);
SqlDataReader dr;
UGIcon.Open();
dr = loginCommand.ExecuteReader();
while (dr.Read())
{
if ((User == dr["user_name"].ToString()) & (pass == dr["sign_in"].ToString()))
{
boolReturnvalue = true;
}
dr.Close();
return boolReturnvalue;
}
return boolReturnvalue;
}
private void button1_Click(object sender, EventArgs e)
{
User = UsernametextBox.Text; //
pass = PasswordtextBox.Text;
SqlDataReader dr;
if (User.Equals(_40029562DataSet.users.user_nameColumn.ToString()))
{new Dashboard().Show();}
else
{
new NewQuote().Show();
}
}
}
}