一、创建数据库 学生表
create database test; create table student( sNo varchar(50) primary key, sName varchar(50) not null )
补充:数据的增删改查相关操作
insert into student values('01','张三'); update student set sName="李四" where sNo="01"; delete from student where sNo="01"; select * from student;
二、引用MySql程序集
C# 链接MySQL数据库的方式:
具体操作:
vs 解决方案 ,选择 “引用”,右键添加 对应.NET版本 文件路径的 MySql.Data.dll 动态库。
三、 进行数据的增删改查
示例:
static void SQLOperator() { MySqlConnection msc = null; MySqlCommand msco = null; try { //1.连接数据库 string cStr = "server=127.0.0.1;port=3306;user=root;password=1234;database=test"; msc = new MySqlConnection(cStr); msc.Open(); //2.创建数据库操作对象并进行具体操作 //2.1 增删改 string sqlInsert = "insert into student values('02','李四');"; //string sqlUpdate = "update student set sName='王天霸' where sNo='02';"; //string sqlDelete = "delete from student where sNo='02';"; msco = new MySqlCommand(sqlInsert, msc); msco.ExecuteNonQuery(); //2.2 查 string sqlQuery = "select * from student;"; msco = new MySqlCommand(sqlQuery, msc); MySqlDataReader msdr=msco.ExecuteReader(); while (msdr.Read()) { Console.Write(msdr[0].ToString() + " " + msdr[1].ToString() + '\\n'); } } catch { } finally { msco.Dispose(); msc.Close(); } }
标签:student,C#,sNo,改查,msco,MySQL,增删,msc,string From: https://www.cnblogs.com/swbna/p/16960367.html