定義操作類
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;
}