程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# MySql 讀寫數據的示例代碼

C# MySql 讀寫數據的示例代碼

編輯:C#入門知識

 

通過MySql connector net組件操作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 DatabaseWindowsApp1 

    public partial class Form1 : Form 

    { 

        private static string DB_CON_STR = "server=localhost;uid=root;pwd=root;database=test"; 

        public Form1() 

        { 

            InitializeComponent(); 

        } 

 

        private void bindListView() 

        { 

            listView1.Clear(); 

            listView1.Columns.Add("ID"); 

            listView1.Columns.Add("Student"); 

 

            MySqlConnection con = new MySqlConnection(DB_CON_STR); 

            con.Open(); 

 

            MySqlCommand cmd = new MySqlCommand("student"); 

            cmd.Connection = con; 

            cmd.CommandType = CommandType.TableDirect; 

            MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.Default); 

            while (dr.Read()) 

            { 

                System.Console.WriteLine(dr.GetInt32(0).ToString() + ": " + dr.GetString(1)); 

 

                // Create three items and three sets of subitems for each item. 

                ListViewItem item1 = new ListViewItem(dr.GetInt32(0).ToString()); 

                // Place a check mark next to the item. 

                item1.Checked = true; 

                item1.SubItems.Add(dr.GetString(1)); 

 

                //Add the items to the ListView. 

                listView1.Items.Add(item1); 

            } 

 

            dr.Close(); 

            con.Close(); 

        } 

 

        private void Form1_Load(object sender, EventArgs e) 

        { 

            bindListView(); 

 

        } 

 

        private void button1_Click(object sender, EventArgs e) 

        { 

            MySqlConnection con = new MySqlConnection(DB_CON_STR); 

            con.Open(); 

 

            MySqlCommand cmd = new MySqlCommand("INSERT INTO student (name) value (@name)"); 

            cmd.Connection = con; 

            cmd.Prepare(); 

            cmd.Parameters.AddWithValue("@name", textBox1.Text); 

            int i = cmd.ExecuteNonQuery(); 

            if (i > 0) 

                MessageBox.Show("插入記錄成功"); 

 

            bindListView(); 

        } 

 

 

    } 

 

 

數據庫結構:

 

 

mysql> describe class; 

+-------+-------------+------+-----+---------+----------------+ 

| Field | Type        | Null | Key | Default | Extra          | 

+-------+-------------+------+-----+---------+----------------+ 

| sid   | int(11)     | NO   | PRI | NULL    | auto_increment | 

| name  | varchar(32) | YES  |     | NULL    |                | 

+-------+-------------+------+-----+---------+----------------+ 

 

 

 

mysql> describe student; 

+-------+-------------+------+-----+---------+----------------+ 

| Field | Type        | Null | Key | Default | Extra          | 

+-------+-------------+------+-----+---------+----------------+ 

| sid   | int(32)     | NO   | PRI | NULL    | auto_increment | 

| name  | varchar(32) | YES  |     | NULL    |                | 

+-------+-------------+------+-----+---------+----------------+ 

 

 

摘自michaelpp的專欄

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved