C#從資料庫mysql讀取資料

來源:互聯網
上載者:User

標籤:open   text   set   bool   void   insert   base   and   grid   

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using MySql.Data.MySqlClient;namespace 電子商務{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {        }        private void button1_Click(object sender, EventArgs e)        {            string str = "Host=localhost;User ID=root;Password=;Port = 3306;DataBase=animals;Charset=utf8;";            MySqlConnection conn = new MySqlConnection(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.訪問資料庫的五大步驟              2.拿到要發送的sql語句              3.執行 sql 語句              4.拿到執行後的聚過              5.關閉串連 切記關閉串連            */            ////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.訪問資料庫的五大步驟              2.拿到要發送的sql語句              3.執行 sql 語句              4.拿到執行後的聚過              5.關閉串連 切記關閉串連            */            //1 拿到資料庫的串連 並開啟串連            string url = "User ID=root;Password=;Host=localhost;Port=3306;Database=students;";            MySqlConnection con = new MySqlConnection(url);            con.Open();            //2.要拿到要發送的sql語句            string sql = "select *from student where name=‘kobe‘";            MySqlCommand com = new MySqlCommand(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(string name)        {            bool b = false;            string url = "User ID=root;Password=root;Host=localhost;Port=3306;Database=students;";            MySqlConnection con = new MySqlConnection(url);            con.Open();            string sql = "select *from studnet where name=‘{0}‘ ";            sql = string.Format(sql, name);            // Console.WriteLine(sql);            MySqlCommand cmd = new MySqlCommand(sql, con);            MySqlDataReader reader = cmd.ExecuteReader();            b = reader.Read();            return b;        }    }}

 

C#從資料庫mysql讀取資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.