标签:
public string InsertToDataBaseTable(ListItemCollection ListItems, string DataBaseTableName) { bool tranflage = false; if (!IsHaveTran) { BeginTransaction(); tranflage = true; } string strsql = "select * from " + DataBaseTableName + " where 1=2"; DataTable tab = new DataTable(); strsql = this.GetAdapterTable(strsql, tab); if (strsql != string.Empty || tab == null) return "获取" + DataBaseTableName + "表结构时出现错误!"; SqlCommand mySqlCommand = new SqlCommand(); mySqlCommand.Connection = this.SqlConnection; mySqlCommand.StatementCompleted += new StatementCompletedEventHandler(SQLCommand_StatementCompleted); if (IsHaveTran) mySqlCommand.Transaction = sqltran; //修改-添加2007-12-28 ListItemCollection nListItems = new ListItemCollection(); int k = 0; for (int j = 0; j < ListItems.Count; j++) { if (!ListItems[j].Value.Equals(string.Empty)) { ListItem l = new ListItem(ListItems[j].Text.ToString(), ListItems[j].Value.ToString()); nListItems.Add(l); k++; } } ListItems = nListItems; //修改-添加2007-12-28 string sqlstrInsert = "INSERT INTO " + DataBaseTableName + " ("; string sqlstrValue = ") VALUES ("; for (int i = 0; i < ListItems.Count; i++) { DataColumn DC = tab.Columns[ListItems[i].Text.ToString().Trim()]; if (DC == null) return "表" + DataBaseTableName + "不存在" + ListItems[i].Text.ToString().Trim() + "字段!"; SqlParameter s = new SqlParameter(ListItems[i].Text.ToString().Trim(), DC.DataType); if (ListItems[i].Value.ToString().Equals(string.Empty) || ListItems[i].Value.ToString().Trim().ToLower() == "null") s.Value = ListItems[i].Value.ToString(); else { switch (DC.DataType.FullName) { case "System.Boolean": if (ListItems[i].Value.ToString() != "0" && ListItems[i].Value.ToString().ToLower() != "false") s.Value = Convert.ChangeType(1, DC.DataType); else s.Value = Convert.ChangeType(0, DC.DataType); break; case "System.Byte[]": if (!System.IO.File.Exists(ListItems[i].Value.ToString())) return "在存储文件时未找到缓存的文件!"; byte[] file = FileToArray(ListItems[i].Value.ToString()); if (file == null) return "在存储文件时未能够正确的转换文件!"; s.Value = file; break; default: s.Value = Convert.ChangeType(ListItems[i].Value.ToString(), DC.DataType); break; } } mySqlCommand.Parameters.Add(s); sqlstrInsert = sqlstrInsert + "[" + ListItems[i].Text.ToString().Trim() + "]"; if (ListItems[i].Value.ToString().Trim().ToLower() != "null") sqlstrValue = sqlstrValue + "@" + ListItems[i].Text.ToString().Trim(); else sqlstrValue = sqlstrValue + "null"; if (i < ListItems.Count - 1) { sqlstrInsert += ","; sqlstrValue += ","; } else { sqlstrValue = sqlstrValue + ");select ReturnID= SCOPE_IDENTITY();"; } } mySqlCommand.CommandText = sqlstrInsert + sqlstrValue; try { strreturnid = string.Empty; strreturnid = mySqlCommand.ExecuteScalar().ToString(); m_dbLog = new DbUpdateLog(); m_dbLog.TableName = DataBaseTableName; m_dbLog.ExecuteSQL = mySqlCommand.CommandText; m_dbLog.UserInfo_ID = HttpContext.Current.Session["UserID"].ToString(); m_dbLog.Trans = sqltran; m_dbLog.Conn = SqlConnection; m_dbLog.RowID = strreturnid; m_dbLog.DBUpdateType_ID = "1"; m_dbLog.FunctionType = DbUpdateLog.EnumFunctionType.InsertToDataBaseTable; DbUpdateLogApi api = DbUpdateLogApiFacotry.GetInterfaceInfo(m_dbLog.FunctionType, m_dbLog); m_dbLog.OpertionType = DbUpdateLog.EnumOpertionType.Insert; m_dbLog.Parameters = mySqlCommand.Parameters; string strmessage = api.Execute(); if (!string.IsNullOrEmpty(strmessage)) { if (tranflage) { RollbackTransaction(); Close(); } return strmessage; } if (tranflage) { return CommitTransaction(); } return string.Empty; } catch (Exception Ex) { if (tranflage) { RollbackTransaction(); Close(); } return Ex.ToString(); } }
标签:
原文地址:http://www.cnblogs.com/Echo529/p/4544307.html