#region-----------------------MySql, 数据库操作 示例-----------------------------------------------
/// <summary>
/// MySql 数据库操作 using MySQLDriverCS
/// </summary>
public static void MySQLDemo()
{
// string mySqlConnStr = "server = localhost;database = database; login = root; pass = 123456;port = 3306";
Console.WriteLine("-----------------Mysql 连接--------------------------------");
MySQLConnection mySQLConn = new MySQLConnection(new MySQLConnectionString("localhost", "database", "root", "123456", 3306).AsString);
mySQLConn.Open();
#region-----------------查询,利用MySQLDataAdapter查询----------------------
Console.WriteLine("-----------------查询 操作--------------------------------");
string querySQL = "select * from testdata LIMIT 100";
MySQLDataAdapter mySQLAdapter = new MySQLDataAdapter(querySQL,mySQLConn);
DataTable mySQLDataTable = new DataTable();
mySQLAdapter.Fill(mySQLDataTable);
foreach (DataRow dr in mySQLDataTable.Rows)
{
for (int j = 0; j < dr.ItemArray.Length; j++)
{
string str = dr[j].ToString();
}
Console.WriteLine("数据"+dr["data"]);
}
Console.WriteLine("条数:" + mySQLDataTable.Rows.Count);
#endregion
#region---------------查询,利用 MySQLDataReader,依次读取每一条数据-------------------
MySQLCommand mySQLCommand = new MySQLCommand(querySQL,mySQLConn);
MySQLDataReader mySQLReader = (MySQLDataReader)mySQLCommand.ExecuteReader();
int i = 0;
while (mySQLReader.Read())
{
i++;
for (int j = 0; j < mySQLReader.FieldCount; j++)
{
string str = mySQLReader[j].ToString();
}
Console.WriteLine("数据:" + mySQLReader["data"].ToString());
}
mySQLReader.Close();
Console.WriteLine("-----------------操作结束--------------------------------");
#endregion
#region ---------------------------插入操作------------------------------------
Console.WriteLine("---------------------直接插入 操作--------------------------");
string x = "11101111";
int x1 = 101;
int x2 = 101;
int x3 = 101;
int x4 = 101;
string insertSQL = "insert into testdata(data,data1,data2,data3,data4) values(" + x + "," + x1 + "," + x2 + "," + x3 + "," + x4 + ")";
MySQLCommand insertCommand = new MySQLCommand(insertSQL,mySQLConn);
insertCommand.ExecuteNonQuery();
Console.WriteLine("-----------------操作结束--------------------------------");
#endregion
Console.WriteLine("-----------------更新 操作--------------------------------");
string updateSQL = "UPDATE TESTDATA SET DATA=11011,DATA1=11011,DATA2=11011,DATA3=11011,DATA4=11011 WHERE data = 1011";
MySQLCommand updateCmd = new MySQLCommand(updateSQL, mySQLConn);
updateCmd.ExecuteNonQuery();
Console.WriteLine("-----------------操作结束--------------------------------");
mySQLConn.Close();
}
#endregion
相关文章
- C# MySQL 数据库操作类
- WebAPI调用笔记 ASP.NET CORE 学习之自定义异常处理 MySQL数据库查询优化建议 .NET操作XML文件之泛型集合的序列化与反序列化 Asp.Net Core 轻松学-多线程之Task快速上手 Asp.Net Core 轻松学-多线程之Task(补充)
- MySQL数据库学习笔记(六)----MySQL多表查询之外键、表连接、子查询、索引
- JDBC操作数据库,第一:jsp插入mysql数据库,坎坷摸索分享
- 将mysql数据库设置成utf8编码时插入查询中文乱码终极解决方案
- .net使用SqlBulkCopy类操作DataTable批量插入数据库数据,然后分页查询坑
- hibernate连接mysql数据库查询数据报错
- 【MySQL】数据库约束与聚合查询和联合查询等进阶操作知识汇总
- 一个通用数据库操作组件DBUtil(c#)、支持SqlServer、Oracle、Mysql、Access、SQLITE
- JAVA对数据库进行操作,实现数据库中数据的插入,查询,更改,删除操作