使用C#登录数据库验证

时间:2015-08-25 04:46:40

标签: c# mysql login

最近,我与Xamarin Studio合作使用C#构建程序。我知道这是一个非常简单的问题,但我真的无法得到它。我想知道我必须使用哪种代码来验证登录MySQL数据库。感谢

using System;
using System.Data;
using MySql.Data.MySqlClient;
using Gtk;

public partial class MainWindow: Gtk.Window
{
    public MainWindow () : base ("RepSys ICDX 1.0 - Login")
    {
        SetDefaultSize (282, 142);
        SetPosition (WindowPosition.Center);
        DeleteEvent += OnDeleteEvent;

        Label uid = new Label ("Username\t: ");
        Label pass = new Label ("Password\t: ");

        Entry uide = new Entry();
        Entry passe = new Entry ();
        passe.Visibility = false;

        Button login = new Button ("Log-In");
        Button exit = new Button (Stock.Cancel);
        login.SetSizeRequest (75, 30);
        exit.SetSizeRequest (75, 30);

        Fixed fix = new Fixed ();
        fix.Put (uid, 20, 30);
        fix.Put (pass, 20, 60);
        fix.Put (uide, 100, 26);
        fix.Put (passe, 100, 56);
        fix.Put (login, 101, 90);
        fix.Put (exit, 186, 90);

        Add (fix);
        ShowAll ();

        login.Clicked += delegate {
            //What should I do here to authenticate user login???
        };
        exit.Clicked += delegate {
            Application.Quit();
        };
    }

    private void OnDeleteEvent (object sender, DeleteEventArgs a)
    {
        Application.Quit ();
        a.RetVal = true;
    }
}

1 个答案:

答案 0 :(得分:0)

this tutorial。该示例由Visual Studio完成,但使用其他IDE执行此操作应该没有问题。

您的点击处理程序需要修改:

login.Clicked += delegate {
    MySqlConnection connection = new MySqlConnection("Server=localhost;Database=testdb;Uid=<your user>;Pwd=<your password>");
    MySqlCommand command = connection.CreateCommand();
    command.CommandText = "SELECT * FROM table;"
    MySqlDataReader reader = command.ExecuteReader();
    while (reader.Read())
    {
        Console.WriteLine(String.Format("{0}", reader[0]));
    }
    reader.Close();
    command.Close();
    connection.Close();
};

不要忘记添加使用声明:

using: MySql.Data.MySqlClient;

您还需要添加MySql.dll(Win应用)和MySql.web.dll(网络应用)作为参考。

相关问题