C#连接MySQL数据库

腾讯云ip 地址 上海  115.159.180.185

效果查看

在winform框架下,添加using

 

using MySql.Data.MySqlClient;

主要代码

static string MyconnecString = "Server=115.159.180.185;Database=han;Uid=root;Pwd=123";

控件定义

private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button btn_lj;
private System.Windows.Forms.Button btn_dq;
private System.Windows.Forms.DataGridView dataGridView1;

 

private void btn_lj_Click(object sender, EventArgs e)
        {
            textBox1.Text = MyconnecString;
            MySqlConnection con = new MySqlConnection(MyconnecString);
            MySqlCommand cmd = new MySqlCommand();
            con.Open();
            cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "INSERT INTO stu VALUES (NULL,'Abcdd','Xuanwumen 10',122,'Abcdd','Xuanwumen 10')";
            cmd.ExecuteNonQuery();
            con.Close();

            btn_lj.Text = "连接完成";
        }


        private void btn_dq_Click(object sender, EventArgs e)
        {

            textBox2.Text = MyconnecString;
            MySqlConnection con = new MySqlConnection(MyconnecString);
            MySqlCommand cmd = new MySqlCommand();
            con.Open();
            cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            //SELECT * FROM mz;
            cmd.CommandText = "SELECT * FROM stu";
            MySqlDataAdapter adap = new MySqlDataAdapter(cmd);

            DataSet ds = new DataSet();
            adap.Fill(ds);
            //dataGridView1.DataSource = ds.Tables[1].DefaultView;
            dataGridView1.DataSource = ds.Tables[0].DefaultView;
            ds.Dispose();

            btn_dq.Text = "读取完成";

        }

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值