c#mysql源码_C#+MYSQL数据库操作(附源码)在这个⽇志的启发下,我做出了⾃⼰的⼀个简单的系统,数据库录⼊,数据库查询~我的代码附在这⾥C#+MYSQL
另外,把我写的⼀些SQL代码也贴出来吧:
drop sttable1;
create sttable1(a char(10) not null,b char(18) not null,primary key (a),unique(a),key(b));
delete sttable1;
insert sttable1(a,b) values ("00000001","11111111111111111X");
insert sttable1(a,b) values ("00000002","00000000000000000X");
insert sttable1(a,b) values ("00000003","22222222222222222X");
select b sttable1 where a="00000001";#到了对应a的那个b.
select * sttable1;#这句话可以显⽰所有
show  columns from  sttable1;#这个会显⽰列
show tables from test1;
show index sttable1;#显⽰table⾥⾯的index
show table status from test1;#就会返回这个表的状态
show variables like 'b%';#查像的变量
show status;其实慢慢看mysql的中⽂参考是蛮有好处的,特别是第七章和第⼋章。也⼀起上传到这⾥。
下⾯是那篇我看过参考过的⽇志的内容:
using System;
using System.Configuration;
using MySql.Data.MySqlClient;
///
/// TestDatebase 的摘要说明
///
public class TestDatebase
{
public TestDatebase()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static void Main(String[] args)
{
MySqlConnection mysql = getMySqlCon();
//查询sql
String sqlSearch = "select * from student";
//插⼊sql
String sqlInsert = "insert into student values (12,'张三',25,'⼤专')";
//修改sql
String sqlUpdate = "update student set name='李四' where id= 3";
//删除sql
String sqlDel = "delete from student where id = 12";
//打印SQL语句
Console.WriteLine(sqlDel);
/
/四种语句对象
//MySqlCommand mySqlCommand = getSqlCommand(sqlSearch, mysql);
//MySqlCommand mySqlCommand = getSqlCommand(sqlInsert, mysql);
//MySqlCommand mySqlCommand = getSqlCommand(sqlUpdate, mysql);
MySqlCommand mySqlCommand = getSqlCommand(sqlDel, mysql);
mysql.Open();
//getResultset(mySqlCommand);
//getInsert(mySqlCommand);
//getUpdate(mySqlCommand);
getDel(mySqlCommand);
//记得关闭
mysql.Close();
String readLine = Console.ReadLine();
}
///
/// 建⽴mysql数据库链接
///
///
public static MySqlConnection getMySqlCon()
{
String mysqlStr = "Database=test;Data Source=127.0.0.1;User
Id=root;Password=root;pooling=false;CharSet=utf8;port=3306";
/
/ String mySqlCon = ConfigurationManager.ConnectionStrings["MySqlCon"].ConnectionString;
MySqlConnection mysql = new MySqlConnection(mysqlStr);
return mysql;
}
///
/// 建⽴执⾏命令语句对象
///
///
///
///
public static MySqlCommand getSqlCommand(String sql,MySqlConnection mysql)
简单的mysql语句
{
MySqlCommand mySqlCommand = new MySqlCommand(sql, mysql);
//  MySqlCommand mySqlCommand = new MySqlCommand(sql);
// mySqlCommand.Connection = mysql;
return mySqlCommand;
}
///
/// 查询并获得结果集并遍历
///
///
public static void getResultset(MySqlCommand mySqlCommand)
{
MySqlDataReader reader = mySqlCommand.ExecuteReader();
try
{
while (reader.Read())
{
if (reader.HasRows)
{
Console.WriteLine("编号:" + reader.GetInt32(0) + "|姓名:" + reader.GetString(1) + "|年龄:" + reader.GetInt32(2) + "|学历:" + reader.GetString(3));
}
}
}
catch (Exception)
{
Console.WriteLine("查询失败了!");
}
finally
{
reader.Close();
}
}
///
/
// 添加数据
///
///
public static void getInsert(MySqlCommand mySqlCommand) {
try
{
mySqlCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
String message = ex.Message;
Console.WriteLine("插⼊数据失败了!" + message);
}
}
///
/// 修改数据
///
///
public static void getUpdate(MySqlCommand mySqlCommand) {
try
{
mySqlCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
String message = ex.Message;
Console.WriteLine("修改数据失败了!" + message);
}
}
///
/// 删除数据
///
///
public static void getDel(MySqlCommand mySqlCommand) {
try
{
mySqlCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
String message = ex.Message;
Console.WriteLine("删除数据失败了!" + message);
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。