private DataTable GetTable(string TableName, DBConnection dbcn) { DataTable dataTable = new DataTable(TableName); string strSelect = "select * from " + TableName + " where 1<>1"; DbDataAdapter adapter = dbcn.GetDataAdapter(strSelect, true, false, false); adapter.Fill(dataTable); dataTable.ExtendedProperties.Add("da", adapter); return dataTable; }
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 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 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 }); } } } } }