using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSVtoDataBase
{
public class SqlSugarHelper
{
public static string ConnectionString = string.Empty;
public static SqlSugarClient db
{
get => new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = ConnectionString,
DbType = DbType.Sqlite,
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.SystemTable
});
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSVtoDataBase
{
public class SqlSugarService
{
public static void SetConnectionStr(string ConnectionStr)
{
SqlSugarHelper.ConnectionString = ConnectionStr;
}
}
}
using Entitys;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSVtoDataBase
{
public interface IResponsitory
{
List<ActualData> GetActualDatas();
DataTable GetDataTableActualDatas();
int InsertActualData(List<ActualData> actualDatas);
DataTable GetDataTableSysAdmin();
int InsertSysAdmin(List<Sysadmins> sysadmins);
}
}
using Entitys;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSVtoDataBase
{
public class SugarResponsitory: IResponsitory
{
private SqlSugarClient _db = SqlSugarHelper.db;
public SqlSugarClient Db { get => _db; set => _db = value; }
public List<ActualData> GetActualDatas()
{
return _db.Queryable<ActualData>().OrderBy(x => x.Id,OrderByType.Desc).Take(50).ToList();
}
public DataTable GetDataTableActualDatas()
{
return _db.Queryable<ActualData>()
.OrderBy(x => x.Id, OrderByType.Desc)
.Take(50)
.Select(x => new
{
InsertTime = x.InsertTime,
VarName = x.VarName,
Remark = x.Remark
}).ToDataTable();
}
public int InsertActualData(List<ActualData> actualDatas)
{
int count = _db.Insertable(actualDatas).ExecuteCommand();
return count;
}
public DataTable GetDataTableSysAdmin()
{
return _db.Queryable<Sysadmins>()
.Select(x => new
{
UserManage = x.UserManage,
LoginName = x.LoginName,
Report = x.Report,
LoginPwd = x.LoginPwd,
SysLog = x.SysLog,
SysSet = x.SysSet,
Trend = x.Trend,
HandCtrl = x.HandCtrl,
AutoCtrl = x.AutoCtrl,
}).ToDataTable();
}
public int InsertSysAdmin(List<Sysadmins> sysadmins)
{
return _db.Insertable(sysadmins).ExecuteCommand();
}
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSVtoDataBase
{
public class CSVHelper
{
public void WriteCSV(string fileName, DataTable dt)
{
FileStream fs;
StreamWriter sw;
string data = null;
if (!File.Exists(fileName))
{
fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
sw = new StreamWriter(fs, Encoding.UTF8);
for (int i = 0; i < dt.Columns.Count; i++)
{
data += dt.Columns[i].ColumnName.ToString();
if (i < dt.Columns.Count - 1)
{
data += ",";
}
}
sw.WriteLine(data);
}
else
{
fs = new FileStream(fileName, FileMode.Append, FileAccess.Write);
sw = new StreamWriter(fs, Encoding.UTF8);
}
for (int i = 0; i < dt.Rows.Count; i++)
{
data = null;
for (int j = 0; j < dt.Columns.Count; j++)
{
data += dt.Rows[i][j].ToString();
if (j < dt.Columns.Count - 1)
{
data += ",";
}
}
sw.WriteLine(data);
}
sw.Close();
fs.Close();
}
public DataTable ReadCSV(string fileName)
{
DataTable dt = new DataTable();
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, Encoding.UTF8);
string strLine = null;
string[] arrayLine = null;
string[] separators = { "," };
bool isFirst = true;
int dtColumns = 0;
while ((strLine = sr.ReadLine()) != null)
{
strLine = strLine.Trim();
arrayLine = strLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);
if (isFirst)
{
dtColumns = arrayLine.Length;
for (int i = 0; i < dtColumns; i++)
{
dt.Columns.Add(arrayLine[i]);
}
isFirst = false;
}
else
{
DataRow dataRow = dt.NewRow();
for (int j = 0; j < dtColumns; j++)
{
dataRow[j] = arrayLine[j];
}
dt.Rows.Add(dataRow);
}
}
sr.Close();
fs.Close();
return dt;
}
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace CSVtoDataBase
{
public class ModelHelper<T> where T : new()
{
public IList<T> DataTableToModel(DataTable dt)
{
IList<T> list = new List<T>();
Type type = typeof(T);
string tempName = "";
foreach (DataRow dr in dt.Rows)
{
T t = new T();
PropertyInfo[] propertys = t.GetType().GetProperties();
foreach (PropertyInfo pro in propertys)
{
tempName = pro.Name;
if (dt.Columns.Contains(tempName))
{
if (!pro.CanWrite) continue;
object value = dr[tempName];
if (value != DBNull.Value)
pro.SetValue(t, value, null);
}
}
list.Add(t);
}
return list;
}
}
}
using Entitys;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSVtoDataBase
{
internal class Program
{
static void Main(string[] args)
{
string path = Directory.GetCurrentDirectory();
string str1 = @"\DataBase\sb1.csv";
string str2 = @"\DataBase\Monitor.db";
SqlSugarService.SetConnectionStr("Data Source=" + path + str2);
SugarResponsitory sugar = new SugarResponsitory();
CSVHelper cSVHelper = new CSVHelper();
TabletoList tabletoList = new TabletoList();
ModelHelper<Sysadmins> modelHelper = new ModelHelper<Sysadmins>();
DataTable sys = sugar.GetDataTableSysAdmin();
cSVHelper.WriteCSV(path+str1, sys);
DataTable sys1 = cSVHelper.ReadCSV(path + str1);
Console.ReadKey();
}
}
}
|