利用Hashtable参数添加数据
/// <summary>
/// 通用的添加
/// </summary>
/// <param name="ht">Hashtable</param>
/// <param name="tableName">表名</param>
/// <returns>bool</returns>
public bool Insert(Hashtable ht, string tableName)
{
try
{
//获得循环访问Hashtable对象
System.Collections.IDictionaryEnumerator id = ht.GetEnumerator();
string sql = "";
string sql1 = "";
string sql2 = "";
OpenConnection();
SqlCommand com = new SqlCommand(sql, strConn);
//事务
if (isTran)
{
com.Transaction = m_Tran;
}
//遍历循环构建SQL语句
while (id.MoveNext())
{
sql1 += id.Key + ",";
sql2 += "@" + id.Key + ",";
}
sql1 = sql1.Substring(0, sql1.Length - 1);
sql2 = sql2.Substring(0, sql2.Length - 1);
sql = "insert into " + tableName + " with (rowlock) (" + sql1 + ") values (" + sql2 + ")";
//
com.CommandText = sql;
//会到初始值
id.Reset();
//遍历构建SqlParameter
while (id.MoveNext())
{
SqlParameter sp = new SqlParameter("@" + id.Key, id.Value.GetType());
sp.Value = id.Value;
com.Parameters.Add(sp);
}
return Convert.ToBoolean(com.ExecuteNonQuery());
}
catch (Exception ex)
{
if (isTran)
{
//回滚
RollbackTransaction();
}
this.strMessage = ex.Message;
return false;
}
}