예제 #1
0
        GetExist <T>(T ob)
        {
            if (String.IsNullOrEmpty(ConnectionString))
            {
                ConnectionString = GlobalSqlContext.GetConStr(typeof(T), "");
            }
            using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(ConnectionString))
            {
                con.Open();
                var gernerater = new MSSQL.MSSSqlCommandGenerater();

                var command = gernerater.GenerateExitsSql <T>(ob);
                command.Connection = con;
                //   int insertRowCount = command.ExecuteNonQuery();


                command.Connection = con;
                SqlDataAdapter adapter = new SqlDataAdapter(command as SqlCommand);
                DataTable      table   = new DataTable();
                T newob = (T)typeof(T).Assembly.CreateInstance(typeof(T).FullName);
                adapter.Fill(table);
                if (table.Rows.Count > 0)
                {
                    SetObject(newob, table.Rows[0]);
                    return(newob);
                }
                else
                {
                    return(null);
                }
            }
        }
예제 #2
0
        public object GetNextKey <T>(T ob)
        {
            if (String.IsNullOrEmpty(ConnectionString))
            {
                ConnectionString = GlobalSqlContext.GetConStr(typeof(T), "");
            }
            using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(ConnectionString))
            {
                con.Open();
                var gernerater = new MSSQL.MSSSqlCommandGenerater();

                var command = gernerater.GenerateGetNextKey <T>(ob);
                command.Connection = con;
                SqlDataAdapter adapter = new SqlDataAdapter(command as SqlCommand);
                DataTable      table   = new DataTable();
                adapter.Fill(table);
                if (table.Rows.Count > 0)
                {
                    return(table.Rows[0][0]);
                }
                else
                {
                    return(null);
                }
            }
        }
예제 #3
0
        public List <I> Get <T, I>(System.Data.SqlClient.SqlCommand selectCommand) where T : class, I, new()
        {
            var sqlCon = ConnectionString;

            if (string.IsNullOrEmpty(sqlCon))
            {
                sqlCon = GlobalSqlContext.GetConStr(typeof(T), "");
            }
            ConnectionString = sqlCon;
            using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(sqlCon))
            {
                con.Open();
                selectCommand.Connection = con;
                var       adapter = new System.Data.SqlClient.SqlDataAdapter(selectCommand as SqlCommand);
                DataTable table   = new DataTable();
                adapter.Fill(table);
                List <I> result     = new List <I>();
                var      Type       = typeof(T);
                var      Properties = Type.GetProperties();
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    I item = new T();
                    SetObject(item, table.Rows[i], Properties);
                    result.Add((T)item);
                }
                return(result);
            }
        }
예제 #4
0
        public List <T> GetItems <T>(object ob, string ParentProperty = null)
        {
            if (String.IsNullOrEmpty(ConnectionString))
            {
                ConnectionString = GlobalSqlContext.GetConStr(typeof(T), "");
            }

            using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(ConnectionString))
            {
                var          Type       = typeof(T);
                var          Properties = Type.GetProperties();
                PropertyInfo parentInfo = null;

                if (ParentProperty == null)
                {
                    foreach (var property in Properties)
                    {
                        var attrib = property.GetCustomAttributes(typeof(ParentRelationAttribute), true);
                        if (attrib.Length > 0)
                        {
                            parentInfo = property;
                            break;
                        }
                        else if (property.PropertyType == ob.GetType())
                        {
                            parentInfo = property;
                        }
                    }
                }
                else
                {
                    parentInfo = Type.GetProperty(ParentProperty);
                }
                con.Open();
                var selectCommand = new MSSQL.MSSSqlCommandGenerater().GenerateSelectItemsSql <T>(ob, parentInfo);
                selectCommand.Connection = con;
                var       adapter = new System.Data.SqlClient.SqlDataAdapter(selectCommand as SqlCommand);
                DataTable table   = new DataTable();
                adapter.Fill(table);
                List <T> result = new List <T>();
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    var item = Type.Assembly.CreateInstance(Type.FullName);
                    SetObject(item, table.Rows[i], Properties, true);
                    parentInfo.SetValue(item, ob, null);
                    result.Add((T)item);
                }
                return(result);
            }
        }
예제 #5
0
        private void Delete <T>(T ob, bool Submit)
        {
            if (String.IsNullOrEmpty(ConnectionString))
            {
                ConnectionString = GlobalSqlContext.GetConStr(typeof(T), "");
            }

            var gernerater = new MSSQL.MSSSqlCommandGenerater();
            // var command = gernerater.GenerateDeleteSql<T>(ob);
            var UpdateSqlCommand = gernerater.GenerateDeleteSql <T>(ob);

            this.TransmitCommands.Enqueue(new SqlTransAutoMic()
            {
                ob = ob, command = UpdateSqlCommand, Operation = Soway.DB.SqlOperation.delete
            });

            if (Submit)
            {
                this.SubmitChanges();
            }
        }
예제 #6
0
        /// <summary>
        /// 新建
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ob"></param>
        private void Create <T>(T ob, bool Submit)
        {
            var sqlCon = ConnectionString;

            if (string.IsNullOrEmpty(sqlCon))
            {
                sqlCon = GlobalSqlContext.GetConStr(typeof(T), "");
            }


            ConnectionString
                = sqlCon;
            var gernerater = new MSSQL.MSSSqlCommandGenerater();
            var command    = gernerater.GenerateCreateSql <T>(ob, this);

            //    var UpdateSqlCommand = gernerater.GenerateSeletedAutoGernerateSql<T>(ob);
            this.TransmitCommands.Enqueue(new SqlTransAutoMic()
            {
                ob = ob, command = command, Operation = SqlOperation.insert
            });
            var UpdateSqlCommand = gernerater.GenerateSeletedAutoGernerateSql <T>(ob);

            if (UpdateSqlCommand != null)
            {
                this.TransmitCommands.Enqueue(new SqlTransAutoMic()
                {
                    ob        = ob,
                    command   = UpdateSqlCommand,
                    Operation = SqlOperation.updateafterinsert
                });
            }

            if (Submit)
            {
                this.SubmitChanges();
            }
        }
예제 #7
0
        private I InternalGetDetail <I, T>(object key) where T : class, I, new()
        {
            if (String.IsNullOrEmpty(ConnectionString))
            {
                ConnectionString = GlobalSqlContext.GetConStr(typeof(T), "");
            }


            if (key == null)
            {
                return(default(I));
            }
            using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(ConnectionString))
            {
                con.Open();
                var selectCommand = new MSSQL.MSSSqlCommandGenerater().GenerateGetItem <T>(key);

                selectCommand.Connection = con;
                var       adapter = new System.Data.SqlClient.SqlDataAdapter(selectCommand as SqlCommand);
                DataTable table   = new DataTable();
                adapter.Fill(table);

                var Type       = typeof(T);
                var Properties = Type.GetProperties();


                if (table.Rows.Count > 0)
                {
                    var item = new T();
                    SetObject(item, table.Rows[0], Properties, true);

                    return(item);
                }
                return(default(I));
            }
        }