------ #### ryControls V3.0.2110.0101 - *.[改进]ObjectListView的FindMatchInRange方法改为包含关系而不是开头匹配。
392 lines
13 KiB
C#
392 lines
13 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data.SqlClient;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using DataParameter =MySql.Data.MySqlClient.MySqlParameter;
|
||
using DataCommand = MySql.Data.MySqlClient.MySqlCommand;
|
||
using DataAdapter = MySql.Data.MySqlClient.MySqlDataAdapter;
|
||
using ryCommonDb;
|
||
|
||
namespace ryCommonDb
|
||
{
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public class MySqlDataProvider:IDbInterface
|
||
{
|
||
ClsMySQLDb myDb = new ClsMySQLDb();
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="sql"></param>
|
||
/// <returns></returns>
|
||
public int ConnDb(string sql)
|
||
{
|
||
myDb.fv_ConnStr = sql;
|
||
return myDb.ConnDb();
|
||
}
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public int CloseDb()
|
||
{
|
||
myDb.CloseDb();
|
||
return 1;
|
||
}
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="tableName"></param>
|
||
/// <param name="wheresql"></param>
|
||
/// <returns></returns>
|
||
public int GetCount(string tableName, string wheresql)
|
||
{
|
||
string m_where = "";
|
||
if (wheresql != "")
|
||
{
|
||
m_where = " where " + wheresql;
|
||
}
|
||
string sql = "select count(*) from " + tableName + m_where;
|
||
return Convert.ToInt32(myDb.ExecuteSQL(sql, (DataParameter[])GetParameter(), "0")[0]);
|
||
}
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="newPwd"></param>
|
||
/// <returns></returns>
|
||
public int ChangePwd(string newPwd)
|
||
{
|
||
return -1000;
|
||
}
|
||
private List<SQLIitem> list_param = new List<SQLIitem>();
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="name"></param>
|
||
/// <param name="value"></param>
|
||
public void AddParameter(string name,object value)
|
||
{
|
||
list_param.Add(new SQLIitem(name, value));
|
||
}
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="name"></param>
|
||
/// <param name="value"></param>
|
||
public void ClearParameter(object name, object value)
|
||
{
|
||
list_param.Clear();
|
||
}
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public object[] GetParameter()
|
||
{
|
||
DataParameter[] defPar = new DataParameter[list_param.Count];
|
||
for (int i = 0; i < list_param.Count; i++)
|
||
{
|
||
SQLIitem item = (SQLIitem)list_param[i];
|
||
defPar[i] = new DataParameter("@" + item.Field.TrimStart('@').TrimStart('[').TrimEnd(']'), item.value);
|
||
}
|
||
return defPar;
|
||
}
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="mySQL"></param>
|
||
/// <returns></returns>
|
||
public object[] GetParameter(RyQuickSQL mySQL)
|
||
{
|
||
DataParameter[] defPar = new DataParameter[mySQL.List.Count + mySQL.List_param.Count];
|
||
for (int i = 0; i < mySQL.List.Count; i++)
|
||
{
|
||
SQLIitem item = (SQLIitem)mySQL.List[i];
|
||
defPar[i] = new DataParameter("@" + item.Field.TrimStart('@').TrimStart('[').TrimEnd(']'), item.value);
|
||
}
|
||
for (int i = mySQL.List.Count; i < mySQL.List.Count + mySQL.List_param.Count; i++)
|
||
{
|
||
SQLIitem item = (SQLIitem)mySQL.List_param[i - mySQL.List.Count];
|
||
defPar[i] = new DataParameter("@" + item.Field.TrimStart('@').TrimStart('[').TrimEnd(']'), item.value);
|
||
}
|
||
return defPar;
|
||
}
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="tableName"></param>
|
||
/// <param name="wheresql"></param>
|
||
/// <param name="orderSQL"></param>
|
||
/// <returns></returns>
|
||
public string GetPageSQL(string tableName, string wheresql, string orderSQL)
|
||
{
|
||
return GetPageSQL("*", tableName, wheresql, orderSQL);
|
||
}
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="field"></param>
|
||
/// <param name="tableName"></param>
|
||
/// <param name="wheresql"></param>
|
||
/// <param name="orderSQL"></param>
|
||
/// <returns></returns>
|
||
public string GetPageSQL(string field, string tableName, string wheresql, string orderSQL)
|
||
{
|
||
string m_where = "";
|
||
if (wheresql != "")
|
||
{
|
||
m_where = " where " + wheresql;
|
||
}
|
||
string m_order = "";
|
||
if (orderSQL != "")
|
||
{
|
||
m_order = " " + orderSQL;
|
||
}
|
||
return "select " + field + " from " + tableName + m_where + m_order + " limit {recordnum},{pagesize}";
|
||
}
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="tableName"></param>
|
||
/// <param name="wheresql"></param>
|
||
/// <param name="orderSQL"></param>
|
||
/// <returns></returns>
|
||
public string GetPageSQL2(string tableName, string wheresql, string orderSQL)
|
||
{
|
||
return "";
|
||
}
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="field"></param>
|
||
/// <param name="tableName"></param>
|
||
/// <param name="wheresql"></param>
|
||
/// <param name="orderSQL"></param>
|
||
/// <returns></returns>
|
||
public string GetPageSQL2(string field, string tableName, string wheresql, string orderSQL)
|
||
{
|
||
return "";
|
||
}
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="sql"></param>
|
||
/// <param name="Parameter"></param>
|
||
/// <returns></returns>
|
||
public bool ContainsData(string sql, object[] Parameter)
|
||
{
|
||
System.Data.DataSet ds = ReadData(sql, Parameter);
|
||
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
|
||
{
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="sql"></param>
|
||
/// <param name="Parameter"></param>
|
||
/// <returns></returns>
|
||
public System.Data.DataSet ReadData(string sql, object[] Parameter)
|
||
{
|
||
DataCommand cmd = myDb.SQL_cn.CreateCommand();
|
||
cmd.Parameters.Clear();
|
||
if (Parameter != null)
|
||
cmd.Parameters.AddRange(Parameter);
|
||
cmd.CommandText = sql;
|
||
DataAdapter ad = new DataAdapter(cmd);
|
||
System.Data.DataSet ds=new System.Data.DataSet();
|
||
ad.Fill(ds);
|
||
ad.Dispose();
|
||
cmd.Parameters.Clear();
|
||
cmd.Dispose();
|
||
return ds;
|
||
}
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="sql"></param>
|
||
/// <param name="mySQL"></param>
|
||
/// <returns></returns>
|
||
public System.Data.DataSet ReadData(string sql, RyQuickSQL mySQL)
|
||
{
|
||
return ReadData(sql,GetParameter(mySQL));
|
||
}
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="sql"></param>
|
||
/// <returns></returns>
|
||
public System.Data.DataSet ReadData(string sql)
|
||
{
|
||
object[] Parameter = null;
|
||
return ReadData(sql, Parameter);
|
||
}
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="tableName"></param>
|
||
/// <param name="id"></param>
|
||
/// <returns></returns>
|
||
public System.Data.DataSet ReadData(string tableName, string id)
|
||
{
|
||
object[] Parameter = null;
|
||
return ReadData("select * from " + tableName + " where id=" + id, Parameter);
|
||
}
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="sql"></param>
|
||
/// <param name="Parameter"></param>
|
||
/// <returns></returns>
|
||
public int ExecuteNonQuery(string sql, object[] Parameter)
|
||
{
|
||
DataCommand cmd = myDb.SQL_cn.CreateCommand();
|
||
cmd.Parameters.Clear();
|
||
if(Parameter!=null)
|
||
cmd.Parameters.AddRange(Parameter);
|
||
cmd.CommandText = sql;
|
||
int i= cmd.ExecuteNonQuery();
|
||
cmd.Parameters.Clear();
|
||
cmd.Dispose();
|
||
return i;
|
||
}
|
||
/// <summary>
|
||
/// 执行SQL语句
|
||
/// </summary>
|
||
/// <param name="mySQL"></param>
|
||
/// <param name="wheresql">只针对IsAdd为false才生效,where 以后的sql语句</param>
|
||
/// <param name="IsAdd">是新增还是更新</param>
|
||
/// <returns></returns>
|
||
public int ExecuteNonQuery(RyQuickSQL mySQL, string wheresql, bool IsAdd)
|
||
{
|
||
if (IsAdd)
|
||
{
|
||
return ExecuteNonQuery(mySQL.GetInsertSQL(), mySQL);
|
||
}
|
||
else
|
||
{
|
||
if (wheresql.Length == 0) { return -1; }
|
||
return ExecuteNonQuery(mySQL.GetUpdateSQL() + " where " + wheresql, mySQL);
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 插入数据库
|
||
/// </summary>
|
||
/// <param name="mySQL"></param>
|
||
/// <returns></returns>
|
||
public int Insert(RyQuickSQL mySQL)
|
||
{
|
||
return ExecuteNonQuery(mySQL.GetInsertSQL(), mySQL);
|
||
}
|
||
/// <summary>
|
||
/// 更新数据库
|
||
/// </summary>
|
||
/// <param name="mySQL"></param>
|
||
/// <param name="wheresql">where 以后的sql语句</param>
|
||
/// <returns></returns>
|
||
public int Update(RyQuickSQL mySQL, string wheresql)
|
||
{
|
||
if (wheresql.Length == 0) { return -1; }
|
||
return ExecuteNonQuery(mySQL.GetUpdateSQL() + " where " + wheresql, mySQL);
|
||
}
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="sql"></param>
|
||
/// <param name="mySQL"></param>
|
||
/// <returns></returns>
|
||
public int ExecuteNonQuery(string sql, RyQuickSQL mySQL)
|
||
{
|
||
return ExecuteNonQuery(sql, GetParameter(mySQL));
|
||
}
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="sql"></param>
|
||
/// <returns></returns>
|
||
public int ExecuteNonQuery(string sql)
|
||
{
|
||
object[] pram = null;
|
||
return ExecuteNonQuery(sql, pram);
|
||
}
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="tableName"></param>
|
||
/// <param name="id"></param>
|
||
/// <returns></returns>
|
||
public int DelById(string tableName, string id)
|
||
{
|
||
object[] param = null;
|
||
return ExecuteNonQuery("delete from " + tableName + " where id=" + id, param);
|
||
}
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="mySQL"></param>
|
||
/// <returns></returns>
|
||
public int CreateDb(RyQuickSQL mySQL)
|
||
{
|
||
string tmpSQL = "CREATE TABLE " + mySQL.TableName + " ([ID] INTEGER PRIMARY KEY,";
|
||
for (int i = 0; i < mySQL.List.Count; i++)
|
||
{
|
||
SQLIitem item = (SQLIitem)mySQL.List[i];
|
||
if (item.value is string)
|
||
{
|
||
if (item.len == 0)
|
||
{
|
||
tmpSQL += "[" + item.Field + "] [nvarchar](max),";
|
||
}
|
||
else
|
||
{ tmpSQL += "[" + item.Field + "] [nvarchar]("+ item.len + "),"; }
|
||
}
|
||
else if (item.value is int || item.value is Int64)
|
||
{
|
||
tmpSQL += "[" + item.Field + "] [int] 0,";
|
||
}
|
||
else if (item.value is double || item.value is float)
|
||
{
|
||
tmpSQL += "[" + item.Field + "] [float] 0,";
|
||
}
|
||
else if (item.value is DateTime)
|
||
{
|
||
tmpSQL += "[" + item.Field + "] [DATETIME],";
|
||
}
|
||
else if (item.value is byte[])
|
||
{
|
||
tmpSQL += "[" + item.Field + "] [LONGBLOB],";
|
||
}
|
||
}
|
||
object[] param = null;
|
||
return ExecuteNonQuery(tmpSQL.Substring(0, tmpSQL.Length - 1) + ")", param);
|
||
}
|
||
MySql.Data.MySqlClient.MySqlTransaction tr;
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public void BeginTransaction()
|
||
{
|
||
tr = myDb.SQL_cn.BeginTransaction();
|
||
}
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public void Commit()
|
||
{
|
||
tr.Commit();
|
||
}
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public void Free()
|
||
{
|
||
list_param.Clear();
|
||
myDb.CloseDb();
|
||
}
|
||
}
|
||
}
|