public SMDataSource CreateDataSource()
 {
     SMDataSource source = new SMDataSource();
     source.Id = StringHelper.GetNewGuid();
     source.Name = "新数据源";
     UnitOfWork currentUnitOfWork = this.CurrentUnitOfWork;
     currentUnitOfWork.RegisterNew(source);
     currentUnitOfWork.Commit();
     return source;
 }
 public DataSet RefreshDataset(SMDataSource smDS, DataSet ds)
 {
     DBConnection connection = new DBConnection(smDS);
     connection.Open();
     try
     {
         for (int i = 0; i < ds.Tables.Count; i++)
         {
             DataTable dt = ds.Tables[i];
             dt.Rows.Clear();
             string sql = (string) dt.ExtendedProperties["selectsql"];
             connection.ExecuteSql(dt, sql);
         }
     }
     finally
     {
         connection.Close();
     }
     return ds;
 }
Beispiel #3
0
 public static void Initial(SMDataSource smDS, IList<DAODataTable> ddts, bool onlyTopLevel, string[] keys, string[] fields, IList<string[]> vals)
 {
     Exception exception;
     ITraceDb traceDB = GetTraceDB(smDS);
     bool canTraceAdd = TraceUtil.CanTraceLevel(TraceLevel.Added) && (traceDB != null);
     StringBuilder builder = new StringBuilder();
     foreach (DAODataTable table in ddts)
     {
         if (!onlyTopLevel || !table.Level)
         {
             string str = CreateInsertTableSQL(keys, fields, vals, traceDB, canTraceAdd, table);
             if (!string.IsNullOrEmpty(str))
             {
                 builder.Append(str).Append("\r\n");
             }
         }
     }
     if (builder.Length > 0)
     {
         DBConnection dbcn = new DBConnection(smDS);
         dbcn.Open();
         try
         {
             Execute(dbcn, builder.ToString(), null);
         }
         catch (Exception exception1)
         {
             exception = exception1;
             throw new DataFormException("Initial data raise error", exception);
         }
         finally
         {
             dbcn.Close();
         }
     }
     if (canTraceAdd)
     {
         try
         {
             traceDB.SaveTraceToDb();
         }
         catch (Exception exception2)
         {
             exception = exception2;
             log.Error("系统对新增数据进行痕记录时出错:", exception);
         }
     }
 }
Beispiel #4
0
 public static void Initial(SMDataSource smDS, DAODataTable ddt, string key, string[] fields, string[] vals)
 {
     List<string[]> list = new List<string[]>(1) {
         vals
     };
     Initial(smDS, ddt, new string[] { key }, fields, list);
 }
Beispiel #5
0
 public static void Initial(SMDataSource smDS, DAODataTable ddt, string[] keys, string[] fields, IList<string[]> vals)
 {
     ITraceDb traceDB = GetTraceDB(smDS);
     bool canTraceAdd = TraceUtil.CanTraceLevel(TraceLevel.Added) && (traceDB != null);
     string str = CreateInsertTableSQL(keys, fields, vals, traceDB, canTraceAdd, ddt);
     if (!string.IsNullOrEmpty(str))
     {
         Exception exception;
         DBConnection dbcn = new DBConnection(smDS);
         dbcn.Open();
         try
         {
             Execute(dbcn, str, null);
             if (canTraceAdd)
             {
                 try
                 {
                     traceDB.SaveTraceToDb();
                 }
                 catch (Exception exception1)
                 {
                     exception = exception1;
                     log.Error("系统对新增数据进行痕记录时出错:", exception);
                 }
             }
         }
         catch (Exception exception2)
         {
             exception = exception2;
             throw new DataFormException("Initial data raise error", exception);
         }
         finally
         {
             dbcn.Close();
         }
     }
 }
Beispiel #6
0
 public static void Initial(SMDataSource smDS, DAODataTable ddt, string[] keys)
 {
     Initial(smDS, ddt, keys, null, null);
 }
Beispiel #7
0
 public static void Initial(SMDataSource smDS, DAODataTable ddt, string key)
 {
     Initial(smDS, ddt, new string[] { key }, null, null);
 }
Beispiel #8
0
 public static void Initial(SMDataSource smDS, DAODataTable ddt)
 {
     Initial(smDS, ddt, null, null, new string[0]);
 }
Beispiel #9
0
 public static ITraceDb GetTraceDB(SMDataSource smDS)
 {
     if (!TraceUtil.CanTraceLevel(TraceLevel.None))
     {
         return AbstractTraceDb.Create(smDS);
     }
     return null;
 }
 public DataTable RefreshDataset(SMDataSource smDS, DataTable dt)
 {
     DBConnection connection = new DBConnection(smDS);
     connection.Open();
     try
     {
         DataTable table = dt;
         table.Rows.Clear();
         string sql = (string) table.ExtendedProperties["selectsql"];
         connection.ExecuteSql(table, sql);
     }
     finally
     {
         connection.Close();
     }
     return dt;
 }
 private void SaveDataToSQlDB(SMDataSource smDS, ref DataTable dt)
 {
     DBConnection connection = new DBConnection(smDS);
     connection.Open();
     try
     {
         IDbTransaction transaction = connection.BeginTrasaction();
         DataTable dataTable = dt;
         if (dataTable.GetChanges() != null)
         {
             string strSelect = (string) dataTable.ExtendedProperties["selectsql"];
             DbDataAdapter adapter = connection.GetDataAdapter(strSelect, true, true, true);
             try
             {
                 adapter.Update(dataTable);
             }
             catch (DBConcurrencyException exception)
             {
                 throw new DataFormException(exception.Message, exception);
             }
         }
         connection.CommitTransaction();
     }
     catch (Exception exception2)
     {
         connection.RollBackTransaction();
         throw exception2;
     }
     finally
     {
         connection.Close();
     }
     dt = this.RefreshDataset(smDS, dt);
 }
 private void SaveDataToSQlDB(SMDataSource smDS, ref DataSet ds)
 {
     int count = ds.Tables.Count;
     DBConnection connection = new DBConnection(smDS);
     connection.Open();
     string str = null;
     if (ds.ExtendedProperties.ContainsKey("CascadeSql"))
     {
         str = (string) ds.ExtendedProperties["CascadeSql"];
     }
     try
     {
         IDbTransaction transaction = connection.BeginTrasaction();
         for (int i = 0; i < count; i++)
         {
             DataTable dataTable = ds.Tables[i];
             if (dataTable.GetChanges() != null)
             {
                 string strSelect = (string) dataTable.ExtendedProperties["selectsql"];
                 DbDataAdapter adapter = connection.GetDataAdapter(strSelect, true, true, true);
                 try
                 {
                     adapter.Update(dataTable);
                 }
                 catch (DBConcurrencyException exception)
                 {
                     LoggingService.Error("数据可能被其他人修改了,不能保存...");
                     throw exception;
                 }
             }
         }
         connection.CommitTransaction();
         try
         {
             if (!string.IsNullOrEmpty(str))
             {
                 if (LoggingService.IsDebugEnabled)
                 {
                     LoggingService.DebugFormatted("将执行级联SQL:{0}", new object[] { str });
                 }
                 connection.ExecuteSql(str);
             }
         }
         catch (Exception exception2)
         {
             LoggingService.ErrorFormatted("执行级联更新语句:{0},发生错语:{1}\r\n{2}", new object[] { str, exception2.Message, exception2.StackTrace });
         }
     }
     catch (Exception exception3)
     {
         connection.RollBackTransaction();
         throw exception3;
     }
     finally
     {
         connection.Close();
     }
 }
 private void SaveAs(SMDataSource smDataSource, DataSet ds)
 {
     DBConnection connection = new DBConnection(smDataSource);
     using (smDataSource.CreateConnection())
     {
         foreach (DataTable table in ds.Tables)
         {
             string strSelect = table.ExtendedProperties["selectsql"].ToString().ToLower();
             string sql = "delete" + strSelect.Substring(strSelect.IndexOf(" from "));
             if (sql.IndexOf(" where ") > 0)
             {
                 LoggingService.DebugFormatted("将行删除语句:{0}", new object[] { sql });
                 int num = connection.ExecuteNonQuery(sql);
                 LoggingService.DebugFormatted("共删除了{0}行数据", new object[] { num });
             }
             DbDataAdapter adapter = connection.GetDataAdapter(strSelect, true, true, true);
             DataTable dataTable = new DataTable();
             adapter.Fill(dataTable);
             LoggingService.InfoFormatted("将异步另存{0}行数据...", new object[] { table.Rows.Count });
             if (dataTable.Rows.Count == 0)
             {
                 foreach (DataRow row in table.Rows)
                 {
                     DataRow row2 = dataTable.NewRow();
                     row2.ItemArray = row.ItemArray;
                     dataTable.Rows.Add(row2);
                 }
                 adapter.Update(dataTable);
             }
             else
             {
                 LoggingService.WarnFormatted("另存数据前没有删除原来的{0}行数据,请检查", new object[] { dataTable.Rows.Count });
             }
         }
         if (ds.ExtendedProperties.ContainsKey("CascadeSql"))
         {
             string str3 = (string) ds.ExtendedProperties["CascadeSql"];
             if (!string.IsNullOrEmpty(str3))
             {
                 try
                 {
                     connection.ExecuteNonQuery(str3);
                 }
                 catch (Exception exception)
                 {
                     LoggingService.Error(exception, "执行级联更新:{0}时出错", new object[] { str3 });
                 }
             }
         }
     }
 }
 private ITraceDb GetTraceDb(SMDataSource smDS)
 {
     return AbstractTraceDb.Create(smDS);
 }
 public void SaveData(SMDataSource smDS, DataSet dt)
 {
     this.SaveDataToSQlDB(smDS, ref dt);
 }
 public DataTable SaveData(SMDataSource smDS, DataTable dt)
 {
     DataTable table;
     try
     {
         this.SaveDataToSQlDB(smDS, ref dt);
         table = dt;
     }
     catch (Exception exception)
     {
         throw exception;
     }
     return table;
 }