0

我的代码中的 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);
            }

        }

    }
}
4

2 回答 2

0

这行是错误的:(DataRow row in _login._database因为_database是字符串类型。您必须遍历具有DataRowlike的东西DataTable

无论如何,我会将您的代码更改为:

private void _loginButton_Click(object sender, EventArgs e)
{
        string username = _usernameTextBox.Text;
        string password = HashPass(_passwordTextBox.Text);

        if (_login.LoginQuery(username, password))
        {
            _usernameTextBox.Text = String.Empty;
            _passwordTextBox.Text = String.Empty;

            MessageBox.Show("Login Success");
        }
        else
        {
            MessageBox.Show("Username/Password incorrect");
        }
}

和 LoginCONnect.cs 方法LoginQuery返回bool

public bool 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)
        {
            return true;
        }                           
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

    return false;     
}
于 2013-10-01T06:52:03.310 回答
0

问题是在这个声明中

foreach (DataRow row in _login._database)

您正在循环 a string,因此枚举 of_login._database是 an IEnumerableofchars而不是 of DataRow,因此row变量将是 achar而不是 a DataRow

我建议您将数据检索到内部 DataTable 中,您的 LoginConnect 代码将如下所示:

namespace Chat
{    
    class LoginConnect
    {
        private MySqlConnection _connection = new MySqlConnection();
        private string _server;
        public string _database;
        private string _uid;
        private string _password;
        private StringList _messagelist = new StringList();
        private string _port;
        private DataTable _dataTable;

        public LoginConnect()
        {
            Initialize();
        }

        public DataTable Data
        { get { return _dataTable; } }

        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);
                 MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
                 _dataTable = new DataTable();
                 adp.Fill(_dataTable);
                 var count = _dataTable.Rows.Count;   
            } 
            catch{}//your handling   
        }
    }
}

所以你可以循环Data

 foreach (DataRow row in _login.Data.AsEnumerable())
于 2013-10-01T06:50:55.953 回答