Beispiel #1
0
        public static bool UpdateMethod_SP(object entity, string sStoredProceedure, string Connection)
        {
            bool result = false;
            int  numRes = 0;

            using (SqlConnection con = new SqlConnection(Connection))
            {
                using (SqlCommand cmd = new SqlCommand(sStoredProceedure, con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    try
                    {
                        foreach (var item in entity.GetType().GetProperties())
                        {
                            if (item.GetValue(entity, null) != null)
                            {
                                if (item.PropertyType.Name == "Nullable`1" && item.GetValue(entity, null).ToString() == "0")
                                {
                                    continue;
                                }
                                if (item.PropertyType.Name == "Byte[]")
                                {
                                    cmd.Parameters.AddWithValue(item.Name, (byte[])(item.GetValue(entity, null)));
                                }
                                else if (item.PropertyType.Name == "DateTime")
                                {
                                    cmd.Parameters.AddWithValue("@" + item.Name, AniHelper.GetDate((DateTime)(item.GetValue(entity, null))));
                                }
                                else
                                {
                                    cmd.Parameters.AddWithValue(item.Name, item.GetValue(entity, null).ToString());
                                }
                            }
                        }
                        con.Open();
                        numRes = cmd.ExecuteNonQuery();
                    }
                    catch (Exception)
                    {
                        result = false;
                        throw;
                    }
                }
            }
            if (numRes > 0)
            {
                result = true;
            }
            else
            {
                result = false;
            }
            return(result);
        }
Beispiel #2
0
        public static bool UpdateToDatabaseObj(object data, string table, string column, int iValue, string Connection)
        {
            try
            {
                List <KeyValuePair <string, string> > values = new List <KeyValuePair <string, string> >();
                using (SqlConnection con = new SqlConnection(Connection))
                {
                    try
                    {
                        foreach (var item in data.GetType().GetProperties())
                        {
                            if (item.GetValue(data, null) != null && item.Name != column)
                            {
                                if (item.PropertyType.Name == "Nullable`1" && item.GetValue(data, null).ToString() == "0")
                                {
                                    continue;
                                }
                                values.Add(new KeyValuePair <string, string>(item.Name, "@" + item.Name));
                            }
                        }
                        string Query = getUpdateCommand(table, values, column, "@" + column);
                        using (SqlCommand cmd = new SqlCommand(Query, con))
                        {
                            cmd.Parameters.Clear();
                            cmd.Parameters.AddWithValue("@" + column, iValue);
                            foreach (var item in data.GetType().GetProperties())
                            {
                                if (item.GetValue(data, null) != null && item.Name != column)
                                {
                                    if (item.PropertyType.Name == "Nullable`1" && item.GetValue(data, null).ToString() == "0")
                                    {
                                        continue;
                                    }
                                    if (item.PropertyType.Name == "Byte[]")
                                    {
                                        cmd.Parameters.AddWithValue("@" + item.Name, (byte[])(item.GetValue(data, null)));
                                    }
                                    else if (item.PropertyType.Name == "DateTime")
                                    {
                                        cmd.Parameters.AddWithValue("@" + item.Name, AniHelper.GetDate((DateTime)(item.GetValue(data, null))));
                                    }
                                    else
                                    {
                                        cmd.Parameters.AddWithValue("@" + item.Name, item.GetValue(data, null).ToString());
                                    }
                                }
                            }
                            con.Open();
                            cmd.ExecuteNonQuery();
                        }
                    }
                    catch (Exception ex)
                    {
                        Messages.ErrorMessage(ex.Message);
                        return(false);

                        throw;
                    }
                }
                return(true);
            }
            catch (Exception)
            {
                throw;
            }
        }