当前位置:网站首页>C # connect to the database to complete the operation of adding, deleting, modifying and querying
C # connect to the database to complete the operation of adding, deleting, modifying and querying
2022-06-28 20:03:00 【Game programming】
Preface
Introduce C# Connect to the database to complete the operations of adding, deleting, modifying and querying
One 、 download
1、 Download package
Select menu bar tool ->NuGet Package manager -> Manage solutions NuGet Package , Select browse , Input MySql.Data, Search and download .

Two 、 Use steps
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(); // increase /*Console.WriteLine(" Please enter a user name :"); string name = Console.ReadLine(); Console.WriteLine(" Please enter age :"); 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(" Registered successfully !"); } else { Console.WriteLine(" Registration failed !"); }*/ // Delete /* Console.WriteLine(" Please enter 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(" Delete failed !"); } else { Console.WriteLine(" Delete successful !"); } mysqlconnection.Close();*/ // modify Console.WriteLine(" Please enter ID:"); string id = Console.ReadLine(); Console.WriteLine(" Please enter age :"); 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(" Modification failed !"); } else { Console.WriteLine(" Modification successful !"); } mysqlconnection.Close(); // Inquire about /* 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();*/ } }}author : the greatest hero in the world tr
Game programming , A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .
边栏推荐
猜你喜欢

I. The HR system is put on the enterprise wechat ISV to enhance the in-depth application of enterprise wechat in service chain retail and other industries

28 rounds of interviews with 10 companies in two and a half years

csdn涨薪技术-Selenium自动化测试全栈总结

2022焊工(初级)特种作业证考试题库及答案

Markdown Mermaid planting grass (1)_ Introduction to Mermaid

Autumn recruitment experience sharing | how to prepare for bank interview

rsync远程同步

2022茶艺师(中级)考试模拟100题及模拟考试

Nanopc-t4 (rk3399) Game1 OLED (I2C) display time weather temperature

How to obtain the coordinates of the aircraft passing through both ends of the radar
随机推荐
Figure introduction to neural networks (GNN, GCN)
F (x) construct the equation, calculate the partial derivative by gradient descent, determine the partial derivative adjustment by loss function, and deal with nonlinear problems by activation functio
如何通过W3school学习JS/如何使用W3school的JS参考手册
Pyinstaller打包pikepdf失败的问题排查
Severity code description project file line suppress status error lnk2038 detected a mismatch of "runtimelibrary": the value "md\u dynamicrelease" does not match the value "mdd\u dynamicde"
Troubleshooting of pyinstaller failed to pack pikepdf
R language GLM generalized linear model: logistic regression, Poisson regression fitting mouse clinical trial data (dose and response) examples and self-test questions
ArrayList of collection
Concours de segmentation des images gastro - intestinales de kaggle Baseline
2022年T电梯修理考试题库模拟考试平台操作
2788.Cifera
The white paper on the panorama of the digital economy and the digitalization of consumer finance were released
28 rounds of interviews with 10 companies in two and a half years
Markdown mermaid種草(1)_ mermaid簡介
管道 | 与重定向 >
Software supply chain security risk guide for enterprise digitalization and it executives
Server configuration estimation of core IOT Bluetooth AOA positioning system
首部元宇宙概念小说《元宇宙2086》获得2022年上袭元宇宙奖
grep文本搜索工具
Markdown mermaid种草(1)_ mermaid简介