定义操作类
class DataBase
{
INIFileHelper inifilehelper = new INIFileHelper("./set.ini");
...
...
}
属性
#region 属性
private bool m_Usefull = true;
public bool Usefull
{
get { return m_Usefull; }
}
private MySqlConnection m_Conn = null;
public MySqlConnection Conn
{
get { return m_Conn; }
}
private MySqlCommand m_Cmd = null;
public MySqlCommand Cmd
{
get { return m_Cmd; }
}
private string m_ServerIP = null;
public string ServerIP
{
get { return m_ServerIP; }
set
{
bool isIPUseful = CommonUse.CheckIP(value);
if (isIPUseful)
{
inifilehelper.IniWriteValue("DataBase", "Server1", value);
m_ServerIP = value;
}
else
{
XtraMessageBox.Show("IP地址格式不正确", "软件提示");
m_ServerIP = null;
}
}
}
private string m_SlaveIP = null;
public string SlaveIP
{
get { return m_SlaveIP; }
set
{
bool isIPUseful = CommonUse.CheckIP(value);
if (isIPUseful)
{
inifilehelper.IniWriteValue("DataBase", "Server2", value);
m_SlaveIP = value;
}
else
{
XtraMessageBox.Show("IP地址格式不正确", "软件提示");
m_SlaveIP = null;
}
}
}
private string m_Status = null;
public string Status
{
get { return m_Status; }
}
private string m_UserId = null;
public string UserId
{
get { return m_UserId; }
}
private string m_Pwd = null;
public string Pwd
{
get { return m_Pwd; }
}
private string m_DataBaseName = null;
public string DataBaseName
{
get { return m_DataBaseName; }
}
private string m_MySQLPath = null;
public string MySQLPath
{
get { return m_MySQLPath; }
set
{
inifilehelper.IniWriteValue("DataBase", "MySQLPath", value);
m_MySQLPath = value;
}
}
private string m_MyINIPath = null;
public string MyINIPath
{
get
{
return m_MyINIPath;
}
}
#endregion
构造函数
public DataBase()
{
m_ServerIP = inifilehelper.IniReadValue("DataBase", "Server1");
m_SlaveIP = inifilehelper.IniReadValue("DataBase", "Server2");
m_UserId = inifilehelper.IniReadValue("DataBase", "UserID");
m_Pwd = inifilehelper.IniReadValue("DataBase", "Pwd");
m_DataBaseName = inifilehelper.IniReadValue("DataBase", "DataBaseName");
m_MySQLPath = inifilehelper.IniReadValue("DataBase", "MySQLPath");
m_MyINIPath = GetMyIniPath();
if (!string.IsNullOrEmpty(m_MyINIPath))
{
iniFilehelperOfmy = new INIFileHelper(m_MyINIPath);
}
if (CommonUse.TestNetConnectity(m_ServerIP))
{
string strConn1 = string.Format("Server = '{0}';Database = '{1}' ;User id= '{2}' ; PWD = '{3}';Allow User Variables=True;",
m_ServerIP, m_DataBaseName, m_UserId, m_Pwd);
try
{
m_Conn = new MySqlConnection(strConn1);
m_Cmd = new MySqlCommand();
m_Cmd.Connection = m_Conn;
m_Status = "正在使用主用服务器...";
}
catch (Exception)
{
MessageBox.Show("请检查参数设置!", "软件提示");
m_Usefull = false;
}
}
else if (CommonUse.TestNetConnectity(m_SlaveIP))
{
try
{
string strConn2 = string.Format("Server = '{0}';Database = '{1}' ;User id= '{2}' ; PWD = '{3}';Allow User Variables=True;",
m_SlaveIP, m_DataBaseName, m_UserId, m_Pwd);
m_Conn = new MySqlConnection(strConn2);
m_Cmd = new MySqlCommand();
m_Cmd.Connection = m_Conn;
m_Status = "正在使用备用服务器...";
}
catch (Exception)
{
MessageBox.Show("请检查参数设置!", "软件提示");
m_Usefull = false;
}
}
else
{
m_Status = "与服务器连接失败...";
m_Usefull = false;
}
}
通过Transact-SQL语句提交数据
public int ExecDataBySql(string strSql)
{
int intReturnValue = -1;
m_Cmd.CommandType = CommandType.Text;
m_Cmd.CommandText = strSql;
try
{
if (m_Conn.State == ConnectionState.Closed) m_Conn.Open();
intReturnValue = m_Cmd.ExecuteNonQuery();
}
catch (Exception e)
{
MessageBox.Show(e.Message, "软件提示");
}
finally
{
m_Conn.Close();
}
return intReturnValue;
}
通过事务执行多条Transact-SQL语句提交数据
public bool ExecDataBySqls(List<string> strSqls)
{
bool booIsSucceed;
if (m_Conn.State == ConnectionState.Closed)
{
try
{
m_Conn.Open();
}
catch (Exception)
{
MessageBox.Show("请检查网络连接和参数设置!", "软件提示");
return false;
}
}
MySqlTransaction sqlTran = m_Conn.BeginTransaction();
try
{
m_Cmd.Transaction = sqlTran;
foreach (string item in strSqls)
{
m_Cmd.CommandType = CommandType.Text;
m_Cmd.CommandText = item;
m_Cmd.ExecuteNonQuery();
}
sqlTran.Commit();
booIsSucceed = true;
}
catch (Exception e)
{
MessageBox.Show(e.Message, "软件提示");
sqlTran.Rollback();
booIsSucceed = false;
}
finally
{
m_Conn.Close();
strSqls.Clear();
}
return booIsSucceed;
}
写入数据,数据库中有该记录时则更新,没有时则插入
public bool WriteData(string strTable, string keyName, string keyValue, string targetName, string targetValue)
{
bool isSuccessed = false;
string strSql = "";
if (IsRecordExist(strTable, keyName, keyValue))
{
strSql = string.Format("UPDATE {0} SET {1}='{2}' WHERE {3} = '{4}'", strTable, targetName,
targetValue, keyName, keyValue);
if (ExecDataBySql(strSql) > 0)
isSuccessed = true;
}
else
{
strSql = string.Format("INSERT INTO {0} ({1},{2}) VALUES ('{3}','{4}')", strTable, keyName, targetName,
keyValue, targetValue);
if (ExecDataBySql(strSql) > 0)
isSuccessed = true;
}
return isSuccessed;
}
通过Transact-SQL语句得到DataSet实例
public DataSet GetDataSet(string strSql, string strTable = "table0")
{
DataSet ds = null;
try
{
MySqlDataAdapter sda = new MySqlDataAdapter(strSql, m_Conn);
ds = new DataSet();
sda.Fill(ds, strTable);
}
catch (Exception e)
{
ds = null;
MessageBox.Show(e.Message, "软件提示");
}
return ds;
}
重新封装ExecuteScalar方法,得到结果集中的第一行的第一列
public object GetSingleObject(string strSql)
{
object obj = null;
m_Cmd.CommandType = CommandType.Text;
m_Cmd.CommandText = strSql;
try
{
if (m_Conn.State == ConnectionState.Closed)
{
m_Conn.Open();
}
obj = m_Cmd.ExecuteScalar();
}
catch (Exception e)
{
obj = null;
MessageBox.Show(e.Message, "软件提示");
}
finally
{
m_Conn.Close();
}
return obj;
}
获取DataAdapter实例
public MySqlDataAdapter GetDataAdapter(string strSql)
{
MySqlDataAdapter sda = null;
try
{
sda = new MySqlDataAdapter(strSql, m_Conn);
}
catch (Exception e)
{
sda = null;
MessageBox.Show(e.Message, "软件提示");
}
return sda;
}
将改动后的dataset更新到数据库对应表中
public bool UpdateDataSet(string strSql, DataSet ds)
{
bool bo = false;
if (ds == null) return false;
DataSet dsChanged = ds.GetChanges();
if (dsChanged == null) return true;
if (dsChanged.Tables[0].Rows.Count <= 0) return true;
DataRow drr = dsChanged.Tables[0].Rows[0];
try
{
MySqlDataAdapter sda = GetDataAdapter(strSql);
MySqlCommandBuilder scb = new MySqlCommandBuilder(sda);
sda.FillSchema(ds, SchemaType.Mapped);
sda.Update(ds, ds.Tables[0].TableName);
ds.AcceptChanges();
bo = true;
sda.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "更新失败");
bo = false;
}
return bo;
}
通过Transact-SQL语句,得到DataTable实例
public DataTable GetDataTable(string strSqlCode, string strTableName = "table1")
{
DataTable dt = null;
MySqlDataAdapter sda = null;
try
{
sda = new MySqlDataAdapter(strSqlCode, m_Conn);
dt = new DataTable(strTableName);
sda.Fill(dt);
}
catch (Exception e)
{
dt = null;
MessageBox.Show(e.ToString());
}
return dt;
}