using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using Dapper;
namespace ConsoleApp7
{
class Program
{
static void Main(string[] args)
{
string connStr = "server=192.168.65.128;userid=root;pwd=root;port=3306;database=datamip;SslMode=none";
//1.用ado.net
//MySqlConnection mySqlConnection = new MySqlConnection(connStr);
//MySqlCommand
//2.用MySqlHelper
//select
var ds = MySqlHelper.ExecuteDataset(connStr, "select* from users");
var info = JsonConvert.SerializeObject(ds);
//update
var result = MySqlHelper.ExecuteNonQuery(connStr, "update users set name='zhaoliu' where id=1");
//只有这种比较复杂
//var reader = MySqlHelper.ExecuteReader(connStr, "select * from users");
//List<User> list = new List<User>();
//while (reader.Read())
//{
// User user = new User();
// user.id = reader.GetInt32("id");
// user.name = reader.GetString("name");
// list.Add(user);
//}
//reader.Close();
//3.dapper的使用
MySqlConnection mySqlConnection = new MySqlConnection(connStr);
var list = mySqlConnection.Query<User>("select * from users");
var list2= mySqlConnection.Query<User>("select * from users where id=@id",new { id = 1 });
var result2 = mySqlConnection.Execute("update users set name=@name where id=@id", new {name="zhaoliu", id = 1 });
}
}
public class User
{
public int id { get; set; }
public string name { get; set; }
}
}