Example #1
1
        public string Proc(string nameSpace, string className, string sa, string password)
        {
            string SQL = @"
            USE [{0}]
            DECLARE @objid INT
            SELECT  @objid = object_id
            FROM sys.all_objects
            WHERE object_id = OBJECT_ID('{1}')

            SELECT  name,
                    'type' = TYPE_NAME(user_type_id),
                    max_length,
                    precision,
                    scale,
                    is_output
            FROM    sys.all_parameters
            WHERE   object_id = @objid
            ORDER BY parameter_id
            ";

            SqlCmd cmd = new SqlCmd(databaseName.Provider, string.Format(SQL, databaseName.Name, spName));
               // cmd.ChangeConnection(sa,password);
            DataTable dt = cmd.FillDataTable();
            DPCollection<SpParamDpo> parameters = new DPCollection<SpParamDpo>(dt);

            string comment = string.Format("//Machine Generated Code by {0} at {1}", ActiveAccount.Account.UserName, DateTime.Today);
            string usingString = @"{0}
            using System;
            using System.Data;
            using Sys.Data;
            ";
            comment = string.Format(usingString, comment);
            string clss = @"{0}
            namespace {1}
            {{
            public partial class {2}
            {{
            {3}
            }}
            }}
            ";

            string func = @"
            private static object ___{0}(int __xtype{6}{1})
            {{
            SqlCmd cmd = new SqlCmd(""{2}..[{0}]"");
            {3}
            object __result = null;
            if(__xtype == 1)
              cmd.ExecuteNonQuery();
            else if(__xtype == 2)
              __result = cmd.FillDataTable();
            else if(__xtype == 3)
              __result = cmd.FillDataSet();
            {4}
            return __result;
            }}

            public static void {0}({1})
            {{
            ___{0}(1{5});
            }}

            public static DataTable dt_{0}({1})
            {{
            return (DataTable)___{0}(2{5});
            }}

            public static DataSet ds_{0}({1})
            {{
            return (DataSet)___{0}(3{5});
            }}

            public const string {7} = @""{8}"";
            ";

            string signuture1 = "";
            string code1 = "";
            string code2 = "";
            string signuture2 = "";
            string tab = "            ";
            foreach (SpParamDpo param in parameters)
            {
                SpParam p = new SpParam(param);
                if (signuture1 != "")
                    signuture1 += ", ";

                if (signuture2 != "")
                    signuture2 += ", ";

                signuture1 += p.signuture1();
                signuture2 += p.signuture2();

                code1 += tab + p.param1();
                code2 += tab + p.param2();

            }

            string method = string.Format(func,
                spName,
                signuture1,
                databaseName.Name,
                code1,
                code2,
                signuture2 == "" ? "" : ", " + signuture2,
                signuture1 == "" ? "" : ", ",
                this.spDefVariable,
                escapeSpDef()
                );

            return string.Format(clss, comment, nameSpace, className, method);
        }
Example #2
0
        protected bool RefreshRow()
        {
            SqlCmd sqlCmd = new SqlCmd(this.tableName.Provider, selectQuery());

            foreach (ColumnAdapter column in columns)
            {
                column.AddParameter(sqlCmd);
            }

            DataTable dt = sqlCmd.FillDataTable();

            if (dt.Rows.Count == 0)
            {
                this.loadedRow = dt.NewRow();
                this.exists    = false;
                return(false);
            }

            if (dt.Rows.Count > 1 && this.locator.Unique)
            {
                throw new ApplicationException("ERROR: Row is not unique.");
            }

            this.loadedRow = dt.Rows[0];
            this.exists    = true;
            return(true);
        }
Example #3
0
        private DataTable LoadData()
        {
            DataTable dt = cmd.FillDataTable();

            dt.CaseSensitive = CaseSensitive;
            var schema = new TableSchema(tableName);

            string[] keys = schema.PrimaryKeys.Keys;
            dt.PrimaryKey = dt.Columns.OfType <DataColumn>().Where(column => keys.Contains(column.ColumnName)).ToArray();
            foreach (IColumn column in schema.Columns)
            {
                DataColumn _column = dt.Columns[column.ColumnName];
                _column.AllowDBNull   = column.Nullable;
                _column.AutoIncrement = column.IsIdentity;

                //because string supports Unicode
                if (column.CType == CType.NVarChar || column.CType == CType.NChar)
                {
                    if (column.Length > 0)
                    {
                        _column.MaxLength = column.Length / 2;
                    }
                }
            }

            return(dt);
        }
Example #4
0
        private void Generate(SqlCmd cmd, DbDataReader reader, TextWriter writer, IProgress <int> progress)
        {
            if (reader != null)
            {
                GenerateByDbReader(reader, writer, progress);
                return;
            }

            var dt = cmd.FillDataTable();

            GenerateByDbTable(dt, writer);
        }
Example #5
0
        public void SetValue()
        {
            if (association == null)
            {
                return;
            }

            object value1 = propertyInfo1.GetValue(dpoInstance, null);
            SqlCmd cmd    = new SqlCmd(this.clause2);

            cmd.AddParameter(association.Column1.SqlParameterName(), value1);
            DataTable dataTable = cmd.FillDataTable();

            if (mappingType == MappingType.One2One)
            {
                //if association object was not instatiated
                if (propertyInfo2.GetValue(this, null) == null)
                {
                    PersistentObject dpo = (PersistentObject)Activator.CreateInstance(propertyInfo2.PropertyType, null);
                    dpo.FillObject(dataTable.Rows[0]);
                    propertyInfo2.SetValue(this, dpo, null);
                }
                else
                {
                    IDPObject dpo = (IDPObject)propertyInfo2.GetValue(this, null);
                    dpo.FillObject(dataTable.Rows[0]);
                }
            }
            else
            {
                //if association collection was not instatiated
                if (propertyInfo2.GetValue(this, null) == null)
                {
                    propertyInfo2.SetValue(this, Activator.CreateInstance(propertyInfo2.PropertyType, new object[] { dataTable }), null);
                }
                else
                {
                    IPersistentCollection collection = (IPersistentCollection)propertyInfo2.GetValue(this, null);
                    collection.Table = dataTable;
                }
            }
        }
Example #6
0
        public int Generate(string nameSpace, string sa, string password)
        {
            string SQL = @"
            USE [{0}]
            SELECT name, OBJECT_DEFINITION(OBJECT_ID) AS sp
            FROM sys.procedures
            WHERE is_ms_shipped <> 1
            ORDER BY name
            ";

            SqlCmd cmd = new SqlCmd(databaseName.Provider, string.Format(SQL, databaseName.Name));
               // cmd.ChangeConnection(sa, password);
            DataTable dt = cmd.FillDataTable();

            foreach (DataRow row in dt.Rows)
            {
                SpProc proc = new SpProc(databaseName, (string)row[SP_NAME], row[SP_DEFINITION].IsNull<string>(""));

                string sourceCode = proc.Proc(nameSpace, databaseName.Name, sa, password);
                WriteFile(proc.SpName, sourceCode, nameSpace, proc.IsSpChanged(nameSpace, databaseName.Name));
            }

            return dt.Rows.Count;
        }
Example #7
0
        public static DataTable FillDataTable(this ConnectionProvider provider, string script)
        {
            SqlCmd cmd = new SqlCmd(provider, script);

            return(cmd.FillDataTable());
        }
Example #8
0
        public static DataTable FillDataTable(this TableName tname, string script)
        {
            SqlCmd cmd = new SqlCmd(tname.Provider, script);

            return(cmd.FillDataTable());
        }
Example #9
0
        protected bool RefreshRow()
        {
            SqlCmd sqlCmd = new SqlCmd(this.tableName.Provider, selectQuery());
            foreach (ColumnAdapter column in columns)
            {
                column.AddParameter(sqlCmd);
            }

            DataTable dt = sqlCmd.FillDataTable();

            if (dt.Rows.Count == 0)
            {
                this.loadedRow = dt.NewRow();
                this.exists = false;
                return false;
            }

            if (dt.Rows.Count > 1 && this.locator.Unique)
                throw new ApplicationException("ERROR: Row is not unique.");

            this.loadedRow = dt.Rows[0];
            this.exists = true;
            return true;
        }
Example #10
0
        public void SetValue()
        {
            if (association == null)
                return ;

            object value1 = propertyInfo1.GetValue(dpoInstance, null);
            SqlCmd cmd = new SqlCmd(this.clause2);
            cmd.AddParameter(association.Column1.SqlParameterName(), value1);
            DataTable dataTable =  cmd.FillDataTable();

            if (mappingType == MappingType.One2One)
            {
                //if association object was not instatiated
                if (propertyInfo2.GetValue(this, null) == null)
                {
                    PersistentObject dpo = (PersistentObject)Activator.CreateInstance(propertyInfo2.PropertyType, null);
                    dpo.FillObject(dataTable.Rows[0]);
                    propertyInfo2.SetValue(this, dpo, null);
                }
                else
                {
                    IDPObject dpo = (IDPObject)propertyInfo2.GetValue(this, null);
                    dpo.FillObject(dataTable.Rows[0]);
                }
            }
            else
            {
                //if association collection was not instatiated
                if (propertyInfo2.GetValue(this, null) == null)
                    propertyInfo2.SetValue(this, Activator.CreateInstance(propertyInfo2.PropertyType, new object[] { dataTable }), null);
                else
                {
                    IPersistentCollection collection = (IPersistentCollection)propertyInfo2.GetValue(this, null);
                    collection.Table = dataTable;
                }
            }
        }