使用MySQL的存储过程登录C#

时间:2014-06-29 10:44:03

标签: c# mysql wpf

我正在创建一个首先登录数据库服务器的userform,然后从其中一个具有用户详细信息的表进行身份验证。

我在MySQL中创建了一个存储过程,我想从我的userform使用该存储过程登录。

这是我的代码,我无法弄清楚我错过了什么。亲切的帮助。 错误:找不到数据库

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MySql.Data.MySqlClient;

namespace OmegaServe
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                string myconnection = "datasource=" + Hostname.Text + ";port=" + PortNumber.Text + ";username=" + DBuname.Text + ";password=" + DBupass.Password + "";
                MySqlConnection myConn = new MySqlConnection(myconnection);
                //MySqlDataAdapter myDataAdapter = new MySqlDataAdapter();
                //MySqlCommand SelectCommand = new MySqlCommand("select name from " + Database.Text + ".userdetails where emailID='" + LoginID.Text + "' and password='" + LoginPass.Password + "';", myConn);
                //MySqlCommandBuilder cd = new MySqlCommandBuilder(myDataAdapter);
                MySqlCommand SelectCommand = new MySqlCommand("authenticateUser", myConn);
                SelectCommand.CommandType = System.Data.CommandType.StoredProcedure;
                MySqlParameter email = new MySqlParameter("@iEmailID", LoginID.Text);
                MySqlParameter password = new MySqlParameter("@iPassword", LoginPass.Password);
                MySqlParameter ip = new MySqlParameter("@iIpAddr", "00:00:00:00");
                MySqlParameter role = new MySqlParameter("@iRoleId", 1);
                SelectCommand.Parameters.Add(email);
                SelectCommand.Parameters.Add(password);
                SelectCommand.Parameters.Add(ip);
                SelectCommand.Parameters.Add(role);
                myConn.Open();

                MySqlDataReader myreader;

                myreader = SelectCommand.ExecuteReader();
                int count = 0;
                while (myreader.Read())
                {
                    count = count + 1;
                }
                if (count == 1)
                {
                    MessageBox.Show("Login Successful");

                }
                else if (count > 1)
                {
                    MessageBox.Show("Duplicate Users Available");

                }
                else
                {
                    MessageBox.Show("Login Failed");
                    myConn.Close();

                }





            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的连接字符串格式应为:

connectionString="server=myServerAddress;port=1234;username=myUsername;password=myPassword;database=myDataBase"

我怀疑您是通过datasource=" + Hostname.Text

捕获服务器和数据库名称