C# + MySqlDataAdapter 連接 MySQL |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using MySql.Data.MySqlClient; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { get_result(); } private void get_result() { String cs = "server=<Host Name>;UId=<Login>;Pwd=<Password>;database=<Database Name>"; MySqlConnection conn = new MySqlConnection(cs); String SQL_Statement = "SELECT * FROM `news_table` LIMIT 0 , 30 "; MySqlDataAdapter da = new MySqlDataAdapter(); DataSet ds = new DataSet(); da.SelectCommand = new MySqlCommand(SQL_Statement, conn); da.Fill(ds); this.dataGridView1.DataSource = ds.Tables[0].DefaultView; conn.Close(); conn.Dispose(); } } } |