using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
namespace CsharpToMySQL
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(VerifyUser("ycb", "89"));
Console.ReadKey();
}
static bool VerifyUser(string username,string password) {
string connectStr = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=root;";
MySqlConnection conn = new MySqlConnection(connectStr);
try
{
conn.Open();
//string sql = "select * from users where username= '" + username + "' and password=' " + password + "'";
string sql = "select *from users where username =@para1 and password =@para2";
MySqlCommand cmd = new MySqlCommand(sql, conn);
cmd.Parameters.AddWithValue("para1", username);
cmd.Parameters.AddWithValue("para2", password);
MySqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
conn.Close();
return true;
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
conn.Close();
}
return true;
}
static void Insert()
{
string connectStr = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=root;";
MySqlConnection conn = new MySqlConnection(connectStr);
try
{
conn.Open();
string sql = "insert into users(username,password,registerdate) values('ycb4','89','" + DateTime.Now + "')";
MySqlCommand cmd = new MySqlCommand(sql, conn);
int result = cmd.ExecuteNonQuery(); //返回值是数据库中受影响的数据行数
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
conn.Close();
}
}
static void Update()
{
string connectStr = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=root;";
MySqlConnection conn = new MySqlConnection(connectStr);
try
{
conn.Open();
string sql = "update users set username='new ycb',password='775'where id = 4";
MySqlCommand cmd = new MySqlCommand(sql, conn);
int result = cmd.ExecuteNonQuery(); //返回值是数据库中受影响的数据行数
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
conn.Close();
}
}
static void ReadUsersCount() {
string connectStr = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=root;";
MySqlConnection conn = new MySqlConnection(connectStr);
try
{
conn.Open();
string sql = "select count(*) from users";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader reader = cmd.ExecuteReader();
reader.Read();
int count = Convert.ToInt32(reader[0].ToString());
Console.WriteLine(count);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
conn.Close();
}
}
static void ExcuteScalar ()
{
string connectStr = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=root;";
MySqlConnection conn = new MySqlConnection(connectStr);
try
{
conn.Open();
string sql = "select count(*) from users";
MySqlCommand cmd = new MySqlCommand(sql, conn);
object res = cmd.ExecuteScalar();
int count = Convert.ToInt32(res.ToString());
Console.WriteLine(count);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
conn.Close();
}
}
}
}
笔记二:客户端输入,服务端检测用户密码是否正确
最新推荐文章于 2022-08-09 20:25:10 发布