using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 补片.DAL
{
public class MySqlSqHelper
{
///
/// 连接数据库
///
///
///
private static MySqlConnection GetConnection(string databasename, string ip = "")
{
try
{
StringBuilder sb = new StringBuilder();
#if ZYZ
sb.AppendFormat("server={0};","192.168.74.132");
sb.AppendFormat("user id={0};", "root");
sb.AppendFormat("password={0};", "123456");
#else
sb.AppendFormat("server={0};", ip == "" ? BuPianBll.ServerIP : ip);
sb.AppendFormat("user id={0};", "root");
sb.AppendFormat("password={0};", BuPianBll.ServerIP == "127.0.0.1" ? " " : "beibo.123/ ");
#endif
sb.AppendFormat("persistsecurityinfo={0};", "true");
sb.AppendFormat("sslmode={0};", "none");
sb.AppendFormat("database={0};", ip == "" ? "sap" + databasename : databasename);
MySqlConnection conn = new MySqlConnection(sb.ToString());
conn.Open();
return conn;
}
catch (MySqlException e)
{
//throw new Exception(e.Message);
gongchenglog.ShowErrorMessage("无法连接到任何指定的MySQL主机。\r\n请检查与MySQL主机网络连接是否正常。\r\n" + e.Message, "数据库连接提示");
return null;
}
}
#region
///
/// 执行一句SQL
///
///
///
///
public static bool ExecuteSql(string sql, string databaseName)
{
try
{
using (MySqlConnection connection = GetConnection(databaseName))
{
if (connection == null)
{
return false;
}
using (MySqlCommand cmd = new MySqlCommand(sql, connection))
{
int rows = cmd.ExecuteNonQuery();
connection.Close();
return rows > 0;
}
}
}
catch (MySqlException e)
{
throw new Exception(e.Message);
}
}
#endregion
}
}