我的代码中的 c# DataRow 命令有一个小问题。因为当我试图通过我的mysql数据库循环时,我有这样的foreach:
foreach (DataRow row in _login._database)
{
//And search for Username and Pass that match
if (row.ItemArray[0].Equals(username) && row.ItemArray[1].Equals(password))
{
_usernameTextBox.Text = String.Empty;
_passwordTextBox.Text = String.Empty;
MessageBox.Show("Login Success");
break;
}
//If not, then show this message.
else
{
MessageBox.Show("Username/Password incorrect");
break;
}
}
会出现这个错误:
Error 1 Cannot convert type 'char' to 'System.Data.DataRow'
有人可以帮助/向我解释我做错了什么。
这是代码的其余部分:
namespace Chat
{
public partial class StartupForm : Form
{
private LoginConnect _login = new LoginConnect();
public StartupForm()
{
InitializeComponent();
_login.OpenConnection();
}
private void _loginButton_Click(object sender, EventArgs e)
{
string username = _usernameTextBox.Text;
string password = HashPass(_passwordTextBox.Text);
//Loop through database
foreach (DataRow row in _login._database)
{
//And search for Username and Pass that match
if (row.ItemArray[0].Equals(username) && row.ItemArray[1].Equals(password))
{
_usernameTextBox.Text = String.Empty;
_passwordTextBox.Text = String.Empty;
MessageBox.Show("Login Success");
break;
}
//If not, then show this message.
else
{
MessageBox.Show("Username/Password incorrect");
break;
}
}
_login.LoginQuery(username, password);
}
private void button1_Click(object sender, EventArgs e)
{
var register = new RegisterForm();
register.ShowDialog();
}
public string HashPass(string password)
{
MD5 mdvijf = new MD5CryptoServiceProvider();
//compute hash from the bytes of text
mdvijf.ComputeHash(ASCIIEncoding.ASCII.GetBytes(password));
//get hash result after compute it
byte[] result = mdvijf.Hash;
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < result.Length; i++)
{
//change it into 2 hexadecimal digits
//for each byte
strBuilder.Append(result[i].ToString("x2"));
}
return strBuilder.ToString();
}
}
}
登录连接.cs:
namespace Chat
{
class LoginConnect
{
private MySqlConnection _connection = new MySqlConnection();
private string _server;
public string _database;
private string _uid;
private string _password;
//public String MessageRecieved;
private StringList _messagelist = new StringList();
//private string _table = "logingegevens";
private string _port;
//private bool succes = false;
public LoginConnect()
{
Initialize();
}
public void Initialize()
{
_server = "localhost";
_port = "3307";
_database = "testlogin";
_uid = "root";
_password = "usbw";
string connectionString = "Server=" + _server + ";" + "Port=" + _port + ";" + "Database=" +
_database + ";" + "Uid=" + _uid + ";" + "Pwd=" + _password + ";";
_connection = new MySqlConnection(connectionString);
}
public bool OpenConnection()
{
try
{
_connection.Open();
return true;
}
catch (MySqlException ex)
{
switch (ex.Number)
{
case 0:
MessageBox.Show("Cannot connect to server");
break;
case 1042:
MessageBox.Show("Unable to connect to any of the specified MySQL hosts");
break;
case 1045:
MessageBox.Show("Invalid username/password");
break;
}
return false;
}
}
public void LoginQuery(string username, string password)
{
string loginquery = "SELECT * FROM logingegevens WHERE Username='" + username + "'AND Password='" + password + "';";
try
{
MySqlCommand cmd = new MySqlCommand(loginquery, _connection);
MySqlDataReader dataReader = cmd.ExecuteReader();
int count = 0;
while (dataReader.Read())
{
count = count + 1;
}
if (count == 1)
{
MessageBox.Show("Login Succesfull");
}
else if (count > 1)
{
MessageBox.Show("Acces denied");
}
else
{
MessageBox.Show("Username or passowrd is not correct.");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}