使用c#登录数据库验证
本文关键字:验证 数据库 登录 使用 | 更新日期: 2023-09-27 18:09:44
最近,我正在使用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;
}
}
参见本教程。这个例子是由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声明:
using: MySql.Data.MySqlClient;
您还需要添加MySql.dll
(Win apps)和MySql.web.dll
(Web apps)作为参考