编程语言
首页 > 编程语言> > C#学习记录

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();

标签:MySqlCommand,记录,C#,cmd,学习,sql,using,password,conn
来源: https://blog.csdn.net/weixin_54025128/article/details/112971300