97 lines
2.5 KiB
C#
97 lines
2.5 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Configuration;
|
|||
|
|
using System.Data;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using AutomaticApp.Model;
|
|||
|
|
using LiveCharts;
|
|||
|
|
using LiveCharts.Defaults;
|
|||
|
|
using LiveCharts.Wpf;
|
|||
|
|
using MySql.Data.MySqlClient;
|
|||
|
|
|
|||
|
|
namespace AutomaticApp.DataAccess
|
|||
|
|
{
|
|||
|
|
//用于对数据库的操作
|
|||
|
|
public class LocalDataAccess
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
private static LocalDataAccess instance;
|
|||
|
|
public LocalDataAccess() { }
|
|||
|
|
public static LocalDataAccess GetInstance()
|
|||
|
|
{
|
|||
|
|
return instance ?? (instance = new LocalDataAccess());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
List<CourseSeriesModel> cModelList = new List<CourseSeriesModel>();
|
|||
|
|
|
|||
|
|
//创建数据库相关的连接变量
|
|||
|
|
MySqlConnection conn;
|
|||
|
|
MySqlCommand comm;
|
|||
|
|
MySqlDataAdapter adapter;
|
|||
|
|
|
|||
|
|
//统一的数据库操作完成的结束方法
|
|||
|
|
private void Dispose()
|
|||
|
|
{
|
|||
|
|
if (adapter != null)
|
|||
|
|
{
|
|||
|
|
adapter.Dispose();
|
|||
|
|
adapter = null;
|
|||
|
|
}
|
|||
|
|
if (comm != null)
|
|||
|
|
{
|
|||
|
|
comm.Dispose();
|
|||
|
|
comm = null;
|
|||
|
|
}
|
|||
|
|
if (conn != null)
|
|||
|
|
{
|
|||
|
|
conn.Close();
|
|||
|
|
conn.Dispose();
|
|||
|
|
conn = null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//统一的全局数据库连接
|
|||
|
|
private bool DBConnection()
|
|||
|
|
{
|
|||
|
|
//从资源文件中获取名字为“db”的数据库连接信息
|
|||
|
|
string connStr = ConfigurationManager.ConnectionStrings["db"].ToString();
|
|||
|
|
if (conn == null)
|
|||
|
|
conn = new MySqlConnection(connStr);
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
conn.Open();
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
catch (Exception)
|
|||
|
|
{
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public void write(string sql)
|
|||
|
|
{
|
|||
|
|
if (DBConnection())
|
|||
|
|
{
|
|||
|
|
MySqlCommand t1 = new MySqlCommand(sql, conn);
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
if (t1.ExecuteNonQuery() > 0)
|
|||
|
|
{
|
|||
|
|
// Console.WriteLine("数据插入成功了!");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception err)
|
|||
|
|
{
|
|||
|
|
Console.WriteLine(err.Message);
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
finally
|
|||
|
|
{
|
|||
|
|
this.Dispose();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|