一个简单的SQL工具-BuildQuery
2010-01-08 00:00:00 来源:WEB开发网BuildQuery类是能快速,容易地构建一个复杂的INSERT或者UPDATE 的SQL查询语句。这个类将接收的一些参数,输出有效的SQL语句。它有一个方法能刷新内部数据,因此这个类可以无数次使用,而无需反复创建和销毁实例对象。Buid,IDSettings,和FlushAllData 方法用于设置数据。 Insert, Update, 和Delete 方法用于处理数据。只有 Build 和IDSettings带参数。
代码
public class BuildQuery
{
#region Class Variables
int numFieldsCount, dicpos;
Dictionary<string, string> dicFields = new Dictionary<string, string>();
Dictionary<string, string> dicData = new Dictionary<string, string>();
Dictionary<string, bool> dicQuotes = new Dictionary<string, bool>();
List<string> listIDFields = new List<string>();
List<string> listIDValues = new List<string>();
List<bool> listIDQuotes = new List<bool>();
List<string> listIDOperators = new List<string>();
Boolean IdentityCheck;
string strTable;
#endregion
#region Constructor
/// <summary>
/// Creates an instance of this class.
/// </summary>
public BuildQuery()
{
}
#endregion
#region Properties
/// <summary>
/// Gets or sets the table that the resulting query will work with.
/// </summary>
public string Table
{
get
{
return strTable;
}
set
{
strTable = value;
}
}
/// <summary>
/// Gets or sets whether the query is set to return a new identity value.
/// </summary>
public bool GetIdentity
{
get
{
return IdentityCheck;
}
set
{
IdentityCheck = value;
}
}
#endregion
#region Methods
/// <summary>
/// Clears all of the internal dictionaries so that a new query can be created.
/// </summary>
public void FlushAllData()
{
numFieldsCount = 0;
dicpos = 0;
dicFields.Clear();
dicData.Clear();
dicQuotes.Clear();
listIDFields.Clear();
listIDValues.Clear();
listIDQuotes.Clear();
listIDOperators.Clear();
}
/// <summary>
/// Adds data to the query.
/// </summary>
/// <param name="InputField">String value containing the column in which the data will appear.</param>
/// <param name="InputData">String value containing the data that is to be used.</param>
/// <param name="InputQuotes">Boolean value indicating whether the data should be surrounded by quotes.</param>
public void Build(string InputField, string InputData, bool InputQuotes)
{
if (!(dicFields.ContainsKey(InputField)))
{
dicFields.Add(InputField, InputField);
dicData.Add(InputField, InputData);
dicQuotes.Add(InputField, InputQuotes);
}
else
{
dicData[InputField] = InputData;
dicQuotes[InputField] = InputQuotes;
}
}
/// <summary>
/// Sets the WHERE portion of the query (if applicable) using the specified operator (default is "=".)
/// </summary>
/// <param name="IDField">String containing the column to be used.</param>
/// <param name="IDValue">String containing the value to be used.</param>
/// <param name="IDQuotes">Boolean indicating whether the value should have quotes around it.</param>
public void IDSettings(string IDField, string IDValue, bool IDQuotes)
{
listIDFields.Add(IDField);
listIDValues.Add(IDValue);
listIDQuotes.Add(IDQuotes);
listIDOperators.Add("=");
}
/// <summary>
/// Sets the WHERE portion of the query (if applicable) using the specified operator (default is "=".)
/// </summary>
/// <param name="IDField">String containing the column to be used.</param>
/// <param name="IDValue">String containing the value to be used.</param>
/// <param name="IDQuotes">Boolean indicating whether the value should have quotes around it.</param>
/// <param name="IDOperator">String containing the logic operator to be used in place of the default.</param>
public void IDSettings(string IDField, string IDValue, bool IDQuotes, string IDOperator)
{
listIDFields.Add(IDField);
listIDValues.Add(IDValue);
listIDQuotes.Add(IDQuotes);
listIDOperators.Add(IDOperator);
}
/// <summary>
/// Returns an Input query using the data provided.
/// </summary>
public string Insert()
{
StringBuilder InsertString = new StringBuilder();
dicpos = 0;
numFieldsCount = dicData.Count;
InsertString.AppendFormat("INSERT INTO {0} (", strTable);
//Put all of the fields into the query
foreach (KeyValuePair<string, string> I in dicFields)
{
InsertString.Append(I.Value);
dicpos++;
if (dicpos + 1 <= numFieldsCount)
{
InsertString.Append(", ");
}
}
dicpos = 0;
InsertString.Append(") VALUES (");
//Put all of the data into the query
foreach (KeyValuePair<string, string> K in dicData)
{
if (dicQuotes[K.Key])
{
InsertString.Append("'");
}
InsertString.Append(dicData[K.Key]);
if (dicQuotes[K.Key])
{
InsertString.Append("'");
}
dicpos++;
if (dicpos + 1 <= numFieldsCount)
{
InsertString.Append(", ");
}
}
InsertString.Append(")");
if (IdentityCheck)
{
InsertString.AppendFormat("SET NOCOUNT ON;{0};SELECT @@Identity As LastID", InsertString.ToString());
}
return InsertString.ToString();
}
/// <summary>
/// Returns an Update query using the data provided.
/// </summary>
public string Update()
{
StringBuilder UpdateString = new StringBuilder();
dicpos = 0;
numFieldsCount = dicData.Count;
UpdateString.AppendFormat("UPDATE {0} SET ", strTable);
//Match up fields and data
foreach (KeyValuePair<string, string> I in dicFields)
{
UpdateString.AppendFormat("{0} = ", I.Value);
if (dicQuotes[I.Key])
{
UpdateString.Append("'");
}
UpdateString.Append(dicData[I.Key]);
if (dicQuotes[I.Key])
{
UpdateString.Append("'");
}
dicpos++;
if (dicpos + 1 <= numFieldsCount)
{
UpdateString.Append(", ");
}
}
UpdateString.Append(" WHERE ");
int Conditions = 0;
for (int IDCount = 0; IDCount < listIDFields.Count; IDCount++)
{
if (Conditions > 0)
{
UpdateString.Append(" AND ");
}
UpdateString.AppendFormat("{0} {1}", listIDFields[IDCount], listIDOperators[IDCount]);
if (listIDQuotes[IDCount])
{
UpdateString.Append("'");
}
UpdateString.Append(listIDValues[IDCount]);
if (listIDQuotes[IDCount])
{
UpdateString.Append("'");
}
Conditions++;
}
return UpdateString.ToString();
}
/// <summary>
/// Returns a Delete query using the data provided.
/// </summary>
public string Delete()
{
StringBuilder DeleteString = new StringBuilder();
DeleteString.AppendFormat("DELETE FROM {0} WHERE ", strTable);
int Conditions = 0;
for (int IDCount = 0; IDCount < listIDFields.Count; IDCount++)
{
if (Conditions > 0)
{
DeleteString.Append(" AND ");
}
DeleteString.AppendFormat("{0} {1}", listIDFields[IDCount], listIDOperators[IDCount]);
if (listIDQuotes[IDCount])
{
DeleteString.Append("'");
}
DeleteString.Append(listIDValues[IDCount]);
if (listIDQuotes[IDCount])
{
DeleteString.Append("'");
}
Conditions++;
}
return DeleteString.ToString();
}
#endregion
}
- ››SQL Server 2008 R2 下如何清理数据库日志文件
- ››sqlite 存取中文的解决方法
- ››SQL2005、2008、2000 清空删除日志
- ››SQL Server 2005和SQL Server 2000数据的相互导入...
- ››sql server 2008 在安装了活动目录以后无法启动服...
- ››简单直观易用 Win 8触屏操作更高效
- ››sqlserver 每30分自动生成一次
- ››简单的Windows 8账户切换(无需重启)
- ››sqlite 数据库 对 BOOL型 数据的插入处理正确用法...
- ››sql server自动生成批量执行SQL脚本的批处理
- ››sql server 2008亿万数据性能优化
- ››SQL Server 2008清空数据库日志方法
更多精彩
赞助商链接