117 lines
4.0 KiB
C#
117 lines
4.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using MySql.Data;
|
|
using MySql.Data.MySqlClient;
|
|
|
|
namespace 垂直剖面浮标临时测试软件
|
|
{
|
|
public class Mysql
|
|
{
|
|
static bool _save = true;
|
|
//定义数据库连接信息
|
|
private static string strMySQLConnection = "server=127.0.0.1;port=3306;user=root;password=1234;database=20210501_czpmfb;";
|
|
public static void write(string sql)
|
|
{
|
|
//创建公共的连接信息
|
|
MySqlConnection connect = new MySqlConnection(strMySQLConnection);
|
|
connect.Open();
|
|
MySqlCommand t1 = new MySqlCommand(sql, connect);
|
|
try
|
|
{
|
|
if (t1.ExecuteNonQuery() > 0)
|
|
{
|
|
// Console.WriteLine("数据插入成功了!");
|
|
}
|
|
}
|
|
catch (Exception err)
|
|
{
|
|
Console.WriteLine(err.Message);
|
|
throw;
|
|
}
|
|
connect.Close();
|
|
}
|
|
|
|
public static void write_ob(Object ob, string a)
|
|
{
|
|
if (_save)
|
|
{
|
|
DateTime time_DataBase = DateTime.Now;
|
|
//遍历结构体赋值
|
|
System.Reflection.PropertyInfo[] properties = ob.GetType().GetProperties();
|
|
string para = "";
|
|
string value = "";
|
|
for (int i = 0; i < properties.Length; i++)
|
|
{
|
|
if (properties[i].Name.Equals("datetime"))
|
|
{
|
|
time_DataBase = (DateTime)properties[i].GetValue(ob);
|
|
}
|
|
else
|
|
{
|
|
if (i != properties.Length - 1)
|
|
{
|
|
para += properties[i].Name + ",";
|
|
value += properties[i].GetValue(ob) + "','";
|
|
}
|
|
else
|
|
{
|
|
para += properties[i].Name;
|
|
value += properties[i].GetValue(ob);
|
|
}
|
|
}
|
|
|
|
}
|
|
string sql = "INSERT INTO " + a + "(datetime," + para + ") VALUES('" + time_DataBase + "','" + value + "');";
|
|
//创建公共的连接信息
|
|
MySqlConnection connect = new MySqlConnection(strMySQLConnection);
|
|
connect.Open();
|
|
MySqlCommand t1 = new MySqlCommand(sql, connect);
|
|
try
|
|
{
|
|
if (t1.ExecuteNonQuery() > 0)
|
|
{
|
|
//Console.WriteLine("数据插入成功了!");
|
|
}
|
|
}
|
|
catch (Exception err)
|
|
{
|
|
Console.WriteLine(err.Message);
|
|
throw;
|
|
}
|
|
connect.Close();
|
|
}
|
|
}
|
|
|
|
public static string[] select(string sql)
|
|
{
|
|
//创建公共的连接信息
|
|
MySqlConnection connect = new MySqlConnection(strMySQLConnection);
|
|
connect.Open();
|
|
MySqlCommand beckoff_1_environment = new MySqlCommand(sql, connect);
|
|
MySqlDataReader read = beckoff_1_environment.ExecuteReader();
|
|
string[] a = null;
|
|
a = new string[1];
|
|
while (read.Read())
|
|
{
|
|
a[0] = read["state"].ToString();
|
|
}
|
|
connect.Close();
|
|
return a;
|
|
}
|
|
|
|
public static MySqlDataReader search(string sql)
|
|
{
|
|
//创建公共的连接信息
|
|
MySqlConnection connect = new MySqlConnection(strMySQLConnection);
|
|
connect.Open();
|
|
MySqlCommand beckoff_1_environment = new MySqlCommand(sql, connect);
|
|
MySqlDataReader read = beckoff_1_environment.ExecuteReader();
|
|
return read;
|
|
connect.Close();
|
|
}
|
|
}
|
|
}
|