usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingMySql.Data.MySqlClient;namespace電子商務
{public partial classForm1 : Form
{publicForm1()
{
InitializeComponent();
}private void Form1_Load(objectsender, EventArgs e)
{
}private void button1_Click(objectsender, EventArgs e)
{string str = "Host=localhost;User ID=root;Password=;Port = 3306;DataBase=animals;Charset=utf8;";
MySqlConnection conn= newMySqlConnection(str);
conn.Open();// 拿到資料庫并打開連接配接
//string sql = "select *from student";//MySqlCommand cmd = new MySqlCommand(sql, conn);//MySqlDataAdapter mda = new MySqlDataAdapter(cmd);//DataSet ds = new DataSet();//mda.Fill(ds);//dataGridView1.DataSource = ds.Tables[0];
MySqlDataReader dr = cmd.ExecuteReader();
//conn.Close();
1 拿到資料庫的連接配接 并打開連接配接
//string url = "User ID=root;Password=;Host=localhost;Port=3306;Database=xx;charset=xx;";//MySqlConnection con = new MySqlConnection(url);//con.Open();
2.要拿到要發送的sql語句
//string sql = "select id from student where name='kobe'";//MySqlCommand com = new MySqlCommand(sql, con);
3 執行 sql 語句
//MySqlDataReader reader = com.ExecuteReader();//reader.Read();//Read() 每次調用 都會從結果集中傳回一行資料//reader.GetInt32(0);
//1 拿到資料庫的連接配接 并打開連接配接
string url = "User ID=root;Password=;Host=localhost;Port=3306;Database=students;";
MySqlConnection con= newMySqlConnection(url);
con.Open();//2.要拿到要發送的sql語句
string sql = "select *from student where name='kobe'";
MySqlCommand com= newMySqlCommand(sql, con);//3 執行 sql 語句
MySqlDataReader reader =com.ExecuteReader();
reader.Read();//Read() 每次調用 都會從結果集中傳回一行資料//reader.GetInt32(0);
Console.WriteLine(reader.GetInt32(0));
con.Close();對資料庫進行 增 删 改 查1 拿到資料庫的連接配接 并打開連接配接
//string url = "User ID=root;Password=root;Host=localhost;Port=3306;Database=students;";//MySqlConnection con = new MySqlConnection(url);//con.Open();//string sql = "insert into student values(11,'hahahaha','man',6)";//添加資訊//MySqlCommand cmd = new MySqlCommand(sql, con);//int result = cmd.ExecuteNonQuery();//Console.WriteLine("資料庫打開結果:"+result);//con.Close();//FindStudent("kobe");
}public static bool FindStudent(stringname)
{bool b = false;string url = "User ID=root;Password=root;Host=localhost;Port=3306;Database=students;";
MySqlConnection con= newMySqlConnection(url);
con.Open();string sql = "select *from studnet where name='{0}'";
sql= string.Format(sql, name);//Console.WriteLine(sql);
MySqlCommand cmd = newMySqlCommand(sql, con);
MySqlDataReader reader=cmd.ExecuteReader();
b=reader.Read();returnb;
}
}
}