标签:
/// <summary> /// 将ListItems中存储的值生成SQLCommand对象,进行修改DataBaseTableName中ID为WhereID的记录操作 /// </summary> public string UpdateToDataBaseTable(ListItemCollection ListItems, string DataBaseTableName, string WhereID) { bool tranflage = false; if (!IsHaveTran) { BeginTransaction(); tranflage = true; } strupdateID = WhereID; string strsql = "select * from " + DataBaseTableName + " where id=‘" + WhereID + "‘; "; DataTable tab = new DataTable(); strsql = this.GetAdapterTable(strsql, tab); if (strsql != string.Empty || tab == null) return "获取" + DataBaseTableName + "表结构时出现错误!"; if (tab.Rows.Count == 0) return "当前需要修改的数据已经被其他用户删除,请刷新后重试!"; SqlCommand mySqlCommand = new SqlCommand(); mySqlCommand.Connection = this.SqlConnection; if (IsHaveTran) mySqlCommand.Transaction = sqltran; string sqlstrUpdata = "UPDATE " + DataBaseTableName + " SET "; 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();**//修改之前2008-12-28 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); if (ListItems[i].Value.ToString().Trim().ToLower() != "null") sqlstrUpdata = sqlstrUpdata + "[" + ListItems[i].Text.ToString().Trim() + "]=@" + ListItems[i].Text.ToString().Trim(); else sqlstrUpdata = sqlstrUpdata + "[" + ListItems[i].Text.ToString().Trim() + "]=null"; if (i < ListItems.Count - 1) { sqlstrUpdata = sqlstrUpdata + ","; } } sqlstrUpdata = sqlstrUpdata + " where id=‘" + WhereID + "‘; "; mySqlCommand.CommandText = sqlstrUpdata; try { string strmssage = WriteUpdateLog(tranflage, DataBaseTableName, sqlstrUpdata, mySqlCommand.Parameters); if (!string.IsNullOrEmpty(strmssage)) { return strmssage; } strreturnid = string.Empty; affectrows = mySqlCommand.ExecuteNonQuery(); strreturnid = WhereID; // m_dbLog.RowID = strreturnid; if (tranflage) { return CommitTransaction(); } return string.Empty; } catch (Exception ex) { return ex.ToString(); } }
标签:
原文地址:http://www.cnblogs.com/Echo529/p/4544310.html