c#datarow的问题

时间:2013-10-01 06:43:04

标签: c# mysql

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


        }

    }
}

LoginCOnnect.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);
            }

        }

    }
}

2 个答案:

答案 0 :(得分:0)

问题在于本声明

foreach (DataRow row in _login._database)

您正在循环string,因此_login._database的枚举是IEnumerable chars而不是DataRow,所以{{1变量将是row而不是char

我建议您将数据检索到内部DataTable中,您的LoginConnect代码将是这样的:

DataRow

所以你可以遍历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

答案 1 :(得分:0)

这一行是错误的:(DataRow row in _login._database因为_database是字符串的类型。您必须迭代DataRow 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;     
}