C#学习记录

链接MySQL数据库
首先要引用MySQL,在添加引用里面找到如下图示
在这里插入图片描述
之后在代码里面引用

using MySql.Data.MySqlClient;

连接语句

String connetStr = "server=127.0.0.1;port=3306;user=root;password=root; database=minecraftdb;";
MySqlConnection conn = new MySqlConnection(connetStr);
try
{    
      conn.Open();//打开通道,建立连接,可能出现异常,使用try catch语句
      Console.WriteLine("已经建立连接");
      //在这里使用代码对数据库进行增删查改
}
catch (MySqlException ex)
{
      Console.WriteLine(ex.Message);
}
finally
{
      conn.Close();
}

查询语句(查询条件自己定)

string sql = "select * from user where username='"+username+"' and password='"+password+"'"; //我们自己按照查询条件去组拼
string sql = "select * from user where username=@para1 and password=@para2";//在sql语句中定义parameter,然后再给parameter赋值
MySqlCommand cmd = new MySqlCommand(sql, conn);
cmd.Parameters.AddWithValue("para1", username);
cmd.Parameters.AddWithValue("para2", password);

MySqlDataReader reader = cmd.ExecuteReader();

下面一个例子

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 WindowsFormsApp4
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        private void Form5_Load(object sender, EventArgs e)
        {
          

        }

        private void button1_Click(object sender, EventArgs e)
        {
            String connetStr = "server=364020x59r.qicp.vip;port=16819;user=root;password=123456; database=zk;charset = 'utf8'";
            MySqlConnection conn = new MySqlConnection(connetStr);
            try
            {
                conn.Open();//打开通道,建立连接,可能出现异常,使用try catch语句
                Console.WriteLine("已经建立连接");
                //在这里使用代码对数据库进行增删查改
                string n ="2", m = "2";
                string sql = "select * from user where username='" + n + "'and password='" + m + "'";

                MySqlCommand cmd = new MySqlCommand(sql, conn);
                MySqlDataReader reader = cmd.ExecuteReader();//执行ExecuteReader()返回一个MySqlDataReader对象
                if (reader.Read())//初始索引是-1,执行读取下一行数据,返回值是bool
                {
                    label1.Text = "有查询结果";

                }
                else
                {
                    label1.Text = "没有查询结果";
                }

            }
            catch (MySqlException ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }
    }
}

结果如下
在这里插入图片描述
在这里插入图片描述
插入

 string sql = "insert into user(username,password) values('"+n+"','"+m+"')";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                int result = cmd.ExecuteNonQuery();

删除

 string sql = "delete from user where username='"+n+"'";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                int result = cmd.ExecuteNonQuery();

更改

 string sql = "update user set username='1',password='5' where username='1'";
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                int result = cmd.ExecuteNonQuery();
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值