当前位置:网站首页>C#连接数据库完成增删改查操作
C#连接数据库完成增删改查操作
2022-06-28 18:44:00 【游戏编程】
前言
介绍C#连接数据库完成增删改查操作
一、下载
1、下载程序包
选择菜单栏工具->NuGet包管理器->管理解决方案的NuGet程序包,选择浏览,输入MySql.Data,进行搜索并下载。

二、使用步骤
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 [email protected]"; 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 [email protected] where [email protected]"; 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();*/ } }}作者:盖世英雄tr
游戏编程,一个游戏开发收藏夹~
如果图片长时间未显示,请使用Chrome内核浏览器。
边栏推荐
猜你喜欢

About Critical Values

BioVendor游离轻链(κ和λ)Elisa 试剂盒检测步骤

Graduation project - Design and development of restaurant management game based on unity (with source code, opening report, thesis, defense PPT, demonstration video and database)

Business layer modification - reverse modification based on the existing framework
![[unity3d] camera follow](/img/11/6309450f2b3ef33df558104549dc4c.png)
[unity3d] camera follow

Lumiprobe ProteOrange 蛋白质凝胶染料说明书

【C#】详解值类型和引用类型区别

牛津大学教授Michael Wooldridge:AI社区近40年如何看待神经网络
![[unity3d] emission (raycast) physical ray (Ray)](/img/46/a9fda743f597db9584c982b10c191c.png)
[unity3d] emission (raycast) physical ray (Ray)

怎样去除DataFrame字段列名
随机推荐
Mindspire series one loading image classification data set
19.2 容器分类、array、vector容器精解
Record an emotet Trojan horse handling case
Sound network releases lingfalcon Internet of things cloud platform, which can build sample scenarios in one hour
中金财富开户安全吗?开过中金财富的讲一下
Anonymous function variable problem
【Unity3D】相机跟随
About Statistical Distributions
Some error prone points of C language pointer
百度时间因子添加
声网发布灵隼物联网云平台 可一小时构建示例场景
About Critical Values
上传文件列表(文件名重复加括号标识)
深入解析kubernetes中的选举机制
数字化转型的1个目标,3大领域,6大因素和9个环节
Anonymous function this pointing and variable promotion
Steam education to break the barriers between disciplines
sqrt()函数的详解和用法「建议收藏」
Voice network VQA: make the user's subjective experience of unknown video quality in real-time interaction known
向上转型和向下转型