using System; using System.Collections.Generic; namespace ryCommonDb { /// /// SQL字段类型 /// public class SQLIitem { /// /// SQL字段类型 /// /// /// public SQLIitem(string _field,object _value) { Field = _field; value = _value; len = 0; } /// /// SQL字段类型 /// /// /// /// public SQLIitem(string _field, object _value,int _len) { Field = _field; value = _value; len = _len; } /// /// SQL字段名 /// public string Field = ""; /// /// SQL字段值 /// public object value; /// /// SQL字段长度 /// public int len = 0; /// /// 不是真的值,而是用于计算的值 /// public bool isNoValue = false; } /// /// SQL快速操作类 /// public class RyQuickSQL { /// /// SQL快速操作类 /// /// public RyQuickSQL(string _tableName) { TableName = _tableName; } /// /// 表名 /// public string TableName{get;set;} /// /// 操作字段列表 /// public List List { get; } = new List(); /// /// 清理使用资源 /// public void Free() { Clear(); } /// /// 清理使用资源 /// public void Dispose() { Free(); } /// /// 清理使用资源 /// ~RyQuickSQL() { Free(); } /// /// 操作参数列表 /// public List List_param { get; } = new List(); /// /// 操作参数列表 /// private List List_Nullparam { get; } = new List(); private void ListAdd(SQLIitem item) { if(!List.Contains(item)) List.Add(item); } /// /// 添加字符串字段 /// /// /// public void AddField(string field,string value) { ListAdd(new SQLIitem(field, value)); } /// /// 添加null值字段 /// /// public void AddNullField(string field) { if (!List_Nullparam.Contains(field)) List_Nullparam.Add(field); } /// /// 添加日期字段 /// /// /// public void AddField(string field, DateTime value) { ListAdd(new SQLIitem(field, value)); } /// /// 添加int字段 /// /// /// public void AddField(string field, int value) { ListAdd(new SQLIitem(field, value)); } /// /// 添加double字段 /// /// /// public void AddField(string field, double value) { ListAdd(new SQLIitem(field, value)); } /// /// 添加decimal字段 /// /// /// public void AddField(string field, decimal value) { ListAdd(new SQLIitem(field, value)); } /// /// 添加byte[]字段 /// /// /// public void AddField(string field, byte[] value) { ListAdd(new SQLIitem(field, value)); } /// /// 添加bool字段 /// /// /// public void AddField(string field, bool value) { ListAdd(new SQLIitem(field, value)); } /// /// 添加用于计算的值 /// /// /// public void AddFieldCalc(string field, string value) { List_calcparam.Add(new SQLIitem(field, value) { isNoValue=true }); } /// /// 添加Int64字段 /// /// /// public void AddField(string field, Int64 value) { ListAdd(new SQLIitem(field, value)); } /// /// 清空内容 /// public void Clear() { List.Clear(); List_param.Clear(); List_calcparam.Clear(); List_Nullparam.Clear(); } /// /// 数量 /// public int Count { get { return List.Count + List_param.Count; } } /// /// /// public List List_calcparam { get; set; } = new List(); /// /// 输出多条件搜索。 /// /// 字段名,多个字段请用逗号分隔。 /// 多个条件之间请用空格隔开 /// public string GetSearchSQL(string Fields, string SearchText) { string tmpSQL = ""; string tmpFieldSQL = "("; string[] m_name = Fields.Split(",,".ToCharArray()); string[] tmpFieldNames = new string[m_name.Length]; for (int i = 0; i < m_name.Length; i++) { #region 提取字段信息 tmpFieldNames[i] = m_name[i]; if (i == 0) { tmpFieldSQL += tmpFieldNames[i] + " like @SearchText"; } else { tmpFieldSQL += " or " + tmpFieldNames[i] + " like @SearchText"; } #endregion } tmpFieldSQL += ")"; string[] m_SearchText = SearchText.Split(' '); string tmpSearchSQL = ""; for (int i = 0; i < m_SearchText.Length; i++) { if (i == 0) { tmpSearchSQL = tmpFieldSQL.Replace("@SearchText", "@st_ry" + i.ToString()); } else { tmpSearchSQL += " and " + tmpFieldSQL.Replace("@SearchText", "@st_ry" + i.ToString()); } AddParameter("@st_ry" + i.ToString(), "%" + m_SearchText[i] + "%"); } return tmpSQL + tmpSearchSQL; } /// /// 获取插入SQL语句 /// /// public string GetInsertSQL() { string tmpSQL = "insert into " + TableName + " "; string tmpFieldSQL = "("; string valueSQL = "("; for (int i = 0; i /// 获取更新SQL语句 /// /// public string GetUpdateSQL() { string tmpSQL = "update " + TableName + " set "; for (int i = 0; i < List.Count; i++) { SQLIitem item = (SQLIitem)List[i]; tmpSQL += item.Field + "=" + "@" + item.Field.TrimStart('[').TrimEnd(']') + ","; } for (int i = 0; i < List_calcparam.Count; i++) { SQLIitem item = (SQLIitem)List_calcparam[i]; tmpSQL += item.Field + "=" + item.value + ","; } for (int i = 0; i < List_Nullparam.Count; i++) { tmpSQL += List_Nullparam[i] + "=" + "null,"; } return tmpSQL.Substring(0, tmpSQL.Length - 1); } /// /// 添加参数 /// /// /// public void AddParameter(string name,object value) { List_param.Add(new SQLIitem(name.TrimStart('@'), value)); } /// /// 获取Ole参数 /// /// public System.Data.OleDb.OleDbParameter[] GetOleParameters() { System.Data.OleDb.OleDbParameter[] defPar = new System.Data.OleDb.OleDbParameter[List.Count+ List_param.Count]; for (int i = 0; i < List.Count; i++) { SQLIitem item = (SQLIitem)List[i]; defPar[i] =new System.Data.OleDb.OleDbParameter("@"+item.Field, item.value); } for (int i = List.Count; i < List.Count + List_param.Count; i++) { SQLIitem item = (SQLIitem)List_param[i- List.Count]; defPar[i] = new System.Data.OleDb.OleDbParameter("@" + item.Field.TrimStart('@'), item.value); } return defPar; } /// /// 获取SQL参数 /// /// public System.Data.SqlClient.SqlParameter[] GetSqlParameter() { System.Data.SqlClient.SqlParameter[] defPar = new System.Data.SqlClient.SqlParameter[List.Count + List_param.Count]; for (int i = 0; i < List.Count; i++) { SQLIitem item = (SQLIitem)List[i]; defPar[i] = new System.Data.SqlClient.SqlParameter("@" + item.Field, item.value); } for (int i = List.Count; i < List.Count + List_param.Count; i++) { SQLIitem item = (SQLIitem)List_param[i - List.Count]; defPar[i] = new System.Data.SqlClient.SqlParameter("@" + item.Field.TrimStart('@'), item.value); } return defPar; } } }