今天把之前的数据库项目改了改,能抽的都抽出来了,不过还差个删除操作没写,等具体有需要的时候再写吧。
感觉用自己写的东西还是满舒坦的。。
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using MySql.Data.MySqlClient;
public class MySqlMgr {
private static readonly MySqlMgr _Instance = new MySqlMgr();
public static MySqlMgr Instance{
get
{
return _Instance;
}
}
public MySqlConnection connect;
public MySqlCommand cmd;
#region 连接数据库
/// <summary>
/// str 数据库名
/// </summary>
/// <param name="str"></param>
public void Connet(string str)
{
string connectStr = "Database=" + str + ";" + "Data Source = 10.192.69.9 ;port=3306;UserId=root;Password=root";
System.DateTime curDate = System.DateTime.Now;
string data = curDate.Year + "年" + curDate.Month + "月" + curDate.Day + "日";
connect = new MySqlConnection(connectStr);
connect.Open();
}
#endregion
#region 增
/// <summary>
/// baseName 数据库名
/// tableName 表名
/// key[] 传入添加的对应表项
/// value[] 值
/// </summary>
/// <param name="baseName"></param>
/// <param name="tableName"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public void Add(string baseName, string tableName, string[] key, string[] value)
{
string command = "INSERT INTO `" + baseName + "`.`" + tableName + "` (";
bool isflag = false;
foreach (var keyItem in key)
{
if (isflag)
command += ",";
command += "`" + keyItem + "`";
isflag = true;
}
isflag = false;
command += ") VALUES(";
foreach (var valueItem in value)
{
if (isflag)
command += ", ";
command += "'" + valueItem + "'";
isflag = true;
}
command += ")";
cmd = new MySqlCommand(command, connect);
cmd.ExecuteNonQuery();
}
#endregion
#region 改
/// <summary>
/// baseName数据库名, tableName表名,key value对应的修改项和值, wherekey wherevalue对应的修改哪里的项,值
/// </summary>
/// <param name="baseName"></param>
/// <param name="tableName"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="whereKey"></param>
/// <param name="whereValue"></param>
public void Update(string baseName, string tableName, string[] key, string[] value, string[] whereKey, string[] whereValue)
{
//UPDATE `test`.`user` SET `name`= '444', `tel`= '3333' WHERE `id`= '4';
string command = "UPDATE `" + baseName + "`.`" + tableName + "` SET ";
bool isflag = false;
for (int index = 0; index < key.Count(); index++)
{
if (isflag)
command += ", ";
command += "`" + key[index] + "`='" + value[index] + "'";
isflag = true;
}
command += " WHERE ";
isflag = false;
for (int index = 0; index < whereKey.Count(); index++)
{
if (isflag)
command += ", ";
command += "`" + whereKey[index] + "`='" + whereValue[index] + "'";
isflag = true;
}
cmd = new MySqlCommand(command, connect);
cmd.ExecuteNonQuery();
}
#endregion
#region 查找
/// <summary>
/// 查找表项, 查找对应的key值 ,返回一个obj的list
/// </summary>
/// <param name="tableName"></param>
/// <param name="key"></param>
/// <returns></returns>
public ArrayList Search(string tableName, string[] key)
{
cmd = new MySqlCommand("select * from "+tableName, MySqlMgr.Instance.connect);
MySqlDataReader reader = cmd.ExecuteReader();
ArrayList ans = new ArrayList();
while (reader.Read())
{
string[] values = new string[key.Count()];
string res = "";
for (int index = 0; index < key.Count(); index++)
{
if (index != 0) res += " ";
values[index] = reader.GetString(key[index]);
res += values[index];
}
ans.Add(res);
}
ans.Remove(ans[ans.Count-1]);
reader.Close();
return ans;
}
#endregion
}