C#连接数据库完成增删改查操作

文章目录

  • 前言
  • 一、下载
    • 1、下载程序包
  • 二、使用步骤


前言

介绍C#连接数据库完成增删改查操作


一、下载

1、下载程序包

选择菜单栏工具->NuGet包管理器->管理解决方案的NuGet程序包,选择浏览,输入MySql.Data,进行搜索并下载。
C#连接数据库完成增删改查操作_第1张图片

二、使用步骤

using System;
using MySql.Data.MySqlClient;

namespace Demo
{
    class Program
    {
        private static MySqlConnectionStringBuilder mysqlbuilder = new MySqlConnectionStringBuilder();
        private static MySqlConnection mysqlconnection;
        static void Main(String[] args)
        {
            mysqlbuilder.UserID = "root";
            mysqlbuilder.Password = "123456";
            mysqlbuilder.Server = "localhost";
            mysqlbuilder.Database = "springboot";
            mysqlconnection = new MySqlConnection(mysqlbuilder.ConnectionString);
            mysqlconnection.Open();

            //增加
            /*Console.WriteLine("请输入用户名:");
            string name = Console.ReadLine();
            Console.WriteLine("请输入年龄:");
            string age = Console.ReadLine();
            string sql = "insert into users(name,age) values(@name,@age)";
            MySqlCommand cmd = new MySqlCommand(sql, mysqlconnection);
            cmd.Parameters.Add(new MySqlParameter("@name", name));
            cmd.Parameters.Add(new MySqlParameter("@age", age));
            int rows=cmd.ExecuteNonQuery();
            if (rows == 1)
            {
                Console.WriteLine("注册成功!");
            }
            else
            {
                Console.WriteLine("注册失败!");

            }*/

            //删除
            /* Console.WriteLine("请输入id:");
             string id = Console.ReadLine();
             string sql = "delete from users where id=@id";
             MySqlCommand cmd = new MySqlCommand(sql, mysqlconnection);
             cmd.Parameters.Add(new MySqlParameter("@id", id));
             int rows = cmd.ExecuteNonQuery();
             if (rows < 0)
             {
                 Console.WriteLine("删除失败!");
             }
             else
             {
                 Console.WriteLine("删除成功!");
             }
             mysqlconnection.Close();*/

            //修改
            Console.WriteLine("请输入ID:");
            string id = Console.ReadLine();
            Console.WriteLine("请输入年龄:");
            string age = Console.ReadLine();
            string sql = "update users set age=@age where id=@id";
            MySqlCommand cmd = new MySqlCommand(sql, mysqlconnection);
            cmd.Parameters.Add(new MySqlParameter("@age", age));
            cmd.Parameters.Add(new MySqlParameter("@id", id));
            int rows = cmd.ExecuteNonQuery();
            if (rows < 0)
            {
                Console.WriteLine("修改失败!");
            }
            else
            {
                Console.WriteLine("修改成功!");
            }
            mysqlconnection.Close();



            //查询
            /*   string sql = "select * from users";
               MySqlCommand cmd = new MySqlCommand(sql, mysqlconnection);
               MySqlDataReader reader = cmd.ExecuteReader();
               Console.WriteLine("id\tname\tage");
               while(reader.Read())
               {
                   Console.Write(reader.GetInt32(0) + "\t");
                   if(reader.IsDBNull(1))
                   {
                       Console.Write("\t");
                   }
                   else
                   {
                       Console.Write(reader.GetString("name") + "\t");
                   }
                   if (reader.IsDBNull(2))
                   {
                       Console.Write(" ");
                   }
                   else
                   {
                       Console.WriteLine(reader.GetString(2) + "\t");
                   }
               }
               mysqlconnection.Close();*/
        }
    }
}


你可能感兴趣的:(C#,c#,数据库,开发语言)