usingMySql.Data.MySqlClient;usingSystem;usingSystem.Data;usingSystem.Collections;public class DatabaseManager : CSingleton{protected static string m_databaseIP = "localhost"; //IP地址
protected static string m_databasePort = "3306"; //端口号
protected static string m_userID = "root"; //MySQL数据库用户名
protected static string m_password = "123456"; //MySQL登陆密码
protected static string m_databaseName = "crh_emergency"; //链接的数据库的库名
protected static string m_connectionString; //数据库连接字符串
///
///测试是否链接上数据库///
///
public boolTestConnection()
{bool isConnected = true;//发送数据库连接字段 创建连接通道
using (MySqlConnection connection = newMySqlConnection(m_connectionString))
{try{//打开连接通道
connection.Open();
}catch(MySqlException E)
{//如果有异常 则连接失败
isConnected = false;throw newException(E.Message);
}finally{//关闭连接通道
connection.Close();
}
}returnisConnected;
}///
///利用字符串组拼方式来编写数据库的连接///
public static voidInit()
{
m_connectionString= string.Format("Server = {0}; port = {1}; Database = {2}; User ID = {3}; Password = {4}; Pooling=true; Charset = utf8;", m_databaseIP, m_databasePort, m_databaseName, m_userID, m_password);
}///
///创建表///
/// 表名
/// 字段名
/// 字段类型
///
public static int CreateTable(string name, string[] cols, string[] colType)
{if (cols.Length !=colType.Length)
{throw new Exception("columns.Length != colType.Length");
}string query = "CREATE TABLE" + name + "(" + cols[0] + " " + colType[0];for (int i = 1; i < cols.Length; ++i)
{
query+= "," + cols[i] + " " +colType[i];
}
query+= ")";returnExecuteNonQuery(query);
}///
///创建表默认ID自动增长///
/// 表名
/// 字段名
/// 字段类型
///
public static int CreateTableAutoID(string name, string[] cols, string[] colType)
{if (cols.Length !=colType.Length)
{throw new Exception("columns.Length != colType.Length");
}string query = "CREATE TABLE" + name + "(" + cols[0] + " " + colType[0] + "NOT NULL AUTO_INCREMENT";for (int i = 1; i < cols.Length; ++i)
{
query+= "," + cols[i] + " " +colType[i];
}
query+= ", PRIMARY KEY (" + cols[0] + ")" + ")";returnExecuteNonQuery(query);
}///
///插入一条数据,包括所有,不适用自动累加ID。///
/// 表名
///
///
public static int InsertInto(string tableName, string[] values)
{string query = "INSERT INTO" + tableName + "VALUES (" + "'" + values[0] + "'";for (int i = 1; i < values.Length; ++i)
{
query+= "," + "'" + values[i] + "'";
}
query+= ")";returnExecuteNonQuery(query);
}///
///插入部分ID///
/// 表名
///
///
///
public static int InsertInto(string tableName, string[] cols, string[] values)
{if (cols.Length !=values.Length)
{throw new Exception("columns.Length != colType.Length");
}string query = "INSERT INTO" + tableName + "(" + cols[0];for (int i = 1; i < cols.Length; ++i)
{
query+= "," +cols[i];
}
query+= ") VALUES (" + "'" + values[0] + "'";for (int i = 1; i < values.Length; ++i)
{
query+= "," + "'" + values[i] + "'";
}
query+= ")";returnExecuteNonQuery(query);
}///
///当指定字段满足一定条件时,更新指定字段的数据///例如更新在user这个表中字段名为userAccount的值等于10086时,将对应userPwd字段的值改成newMd5SumPassword///("users", new string[] { "userPwd" }, new string[] { newMd5SumPassword }, "userAccount", "10086")///
/// 表名
/// 字段
/// 字段值
/// 指定的字段
/// 指定字段满足的条件
///
public static int UpdateInto(string tableName, string[] cols, string[] colsValues, string selectKey, stringselectValue)
{string query = "UPDATE" + tableName + "SET" + cols[0] + "=" + "'" + colsValues[0] + "'";for (int i = 1; i < colsValues.Length; ++i)
{
query+= "," + cols[i] + "=" &