private DataBinderLoadingEventArgs LoadBinding() { DisposeTables(); Materia.RefreshAndManageCurrentProcess(); string _query = _basecommandtext + (_basecommandtext.RLTrim().EndsWith(";") ? "" : ";"); if (!String.IsNullOrEmpty(_viewcommandtext.RLTrim())) _query += _viewcommandtext + (_viewcommandtext.RLTrim().EndsWith(";") ? "" : ";"); string _error = ""; if (!String.IsNullOrEmpty(_query.RLTrim())) { QueResult _result = Que.Execute(_binder.Connection, _query, CommandExecution.ExecuteReader); _error = _result.Error; if (String.IsNullOrEmpty(_error.RLTrim())) { if (_result.ResultSet != null) { if (_result.ResultSet.Tables.Count > 0) { _basetable = _result.ResultSet.Tables[0].Replicate(); if (!String.IsNullOrEmpty(_basetable.TableName.RLTrim()) && String.IsNullOrEmpty(_name.RLTrim())) _name = _basetable.TableName; if (_result.ResultSet.Tables.Count >= 2) _table = _result.ResultSet.Tables[1].Replicate(); else _table = _basetable.Replicate(); _basetable = InitializedTable(_basetable, true); _table = InitializedTable(_table); AttachTableHandlers(); } else _error = "No resultset has been returned by the executed command."; } else _error = "No resultset has been returned by the executed command."; } } else _error = "No database command statement is specified."; return new DataBinderLoadingEventArgs(this, _error); }
private DataTable InitializedTable(DataTable table, bool createcolkey) { if (table != null) { DataTable dt = null; string _uniquekey = ""; string _pk = ""; foreach (DataColumn _column in table.Columns) { if (_column.Unique) { _uniquekey = _column.ColumnName; break; } } if (!String.IsNullOrEmpty(_uniquekey.RLTrim())) { if (!String.IsNullOrEmpty(_primarykey.RLTrim())) { if (table.Columns.Contains(_primarykey)) _pk = _primarykey; else _pk = _uniquekey; } else _pk = _uniquekey; } else { if (!String.IsNullOrEmpty(_primarykey.RLTrim())) { if (table.Columns.Contains(_primarykey)) _pk = _primarykey; else _pk = table.Columns[0].ColumnName; } else _pk = table.Columns[0].ColumnName; } table.Constraints.Clear(); bool _withuint = false; foreach (DataColumn _column in table.Columns) { _withuint = _withuint || VisualBasic.CBool(_column.DataType.Name.ToLower() == typeof(uint).Name); _withuint = _withuint || VisualBasic.CBool(_column.DataType.Name.ToLower() == typeof(UInt16).Name); _withuint = _withuint || VisualBasic.CBool(_column.DataType.Name.ToLower() == typeof(UInt32).Name); _withuint = _withuint || VisualBasic.CBool(_column.DataType.Name.ToLower() == typeof(UInt64).Name); _withuint = _withuint || VisualBasic.CBool(_column.DataType.Name.ToLower() == typeof(ulong).Name); if (_withuint) break; } DataTable _temptable = null; if (_withuint) { _temptable = table.Clone(); _temptable.TableName = table.TableName; foreach (DataColumn _column in _temptable.Columns) { _withuint = _withuint || VisualBasic.CBool(_column.DataType.Name.ToLower() == typeof(uint).Name); _withuint = _withuint || VisualBasic.CBool(_column.DataType.Name.ToLower() == typeof(UInt16).Name); _withuint = _withuint || VisualBasic.CBool(_column.DataType.Name.ToLower() == typeof(UInt32).Name); _withuint = _withuint || VisualBasic.CBool(_column.DataType.Name.ToLower() == typeof(UInt64).Name); _withuint = _withuint || VisualBasic.CBool(_column.DataType.Name.ToLower() == typeof(ulong).Name); if (_withuint)_temptable.Columns[_column.ColumnName].DataType = typeof(long); } foreach (DataRow rw in table.Rows) { if (rw.RowState != DataRowState.Deleted && rw.RowState != DataRowState.Detached) _temptable.Rows.Add(rw.ItemArray); } _temptable.AcceptChanges(); } else _temptable = table.Replicate(); if (createcolkey && !String.IsNullOrEmpty(_pk.RLTrim())) { dt = new DataTable(); dt.TableName = _temptable.TableName; dt.Columns.Add(_pkfieldname, _temptable.Columns[_pk].DataType); foreach (DataColumn _column in _temptable.Columns) dt.Columns.Add(_column.ColumnName, _column.DataType); foreach (DataRow rw in _temptable.Rows) { if (rw.RowState != DataRowState.Deleted && rw.RowState != DataRowState.Detached) { object[] _values = new object[dt.Columns.Count]; _values[0] = rw[_pk]; foreach (DataColumn _column in _temptable.Columns) _values[_column.Ordinal + 1] = rw[_column.ColumnName]; dt.Rows.Add(_values); } } } else dt = _temptable.Replicate(); if (dt != null && !String.IsNullOrEmpty(_pk.RLTrim())) { DataColumn _col = dt.Columns[_pk]; dt.Constraints.Add("PK", _col, true); if (_col.DataType.Name == typeof(int).Name || _col.DataType.Name == typeof(Int16).Name || _col.DataType.Name == typeof(Int32).Name || _col.DataType.Name == typeof(Int64).Name || _col.DataType.Name == typeof(long).Name || _col.DataType.Name == typeof(byte).Name || _col.DataType.Name == typeof(Byte).Name || _col.DataType.Name == typeof(sbyte).Name || _col.DataType.Name == typeof(SByte).Name) { if (!_col.AutoIncrement) { if (_col.DefaultValue != null) { try { _col.DefaultValue = null; } catch { } } _col.AutoIncrement = true; } _col.AutoIncrementStep = 1; if (dt.Rows.Count > 0) { object _max = dt.Compute("MAX([" + _pk + "])", ""); if (VisualBasic.IsNumeric(_max)) _col.AutoIncrementSeed = VisualBasic.CLng(_max); else _col.AutoIncrementSeed = 1; } else _col.AutoIncrementSeed = 1; } foreach (DataColumn _column in dt.Columns) { if (!_column.AutoIncrement && !_column.Unique) { if (_column.DataType.Name == typeof(string).Name || _column.DataType.Name == typeof(String).Name) _column.DefaultValue = ""; else if (_column.DataType.Name == typeof(DateTime).Name) _column.DefaultValue = DateTime.Now; else if (_column.DataType.Name == typeof(byte).Name || _column.DataType.Name == typeof(Byte).Name || _column.DataType.Name == typeof(decimal).Name || _column.DataType.Name == typeof(Decimal).Name || _column.DataType.Name == typeof(double).Name || _column.DataType.Name == typeof(Double).Name || _column.DataType.Name == typeof(float).Name || _column.DataType.Name == typeof(int).Name || _column.DataType.Name == typeof(Int16).Name || _column.DataType.Name == typeof(Int32).Name || _column.DataType.Name == typeof(Int64).Name || _column.DataType.Name == typeof(long).Name || _column.DataType.Name == typeof(sbyte).Name || _column.DataType.Name == typeof(SByte).Name || _column.DataType.Name == typeof(short).Name || _column.DataType.Name == typeof(Single).Name) _column.DefaultValue = 0; else if (_column.DataType.Name == typeof(bool).Name || _column.DataType.Name == typeof(Boolean).Name) _column.DefaultValue = false; else { } } } } if (dt != null) dt.AcceptChanges(); return dt; } else return null; }