示例#1
0
        private bool Exist <T>(T t, string key)
        {
            string tableName = typeof(T).Name;

            PropertyInfo[] propertyInfos = t.GetType().GetProperties();
            List <string>  keyList       = new List <string>();

            if (!string.IsNullOrEmpty(key))
            {
                keyList.Add(key);
            }
            foreach (var keyItem in propertyInfos)
            {
                KeyFieldAttribute keyFieldAttribute = keyItem.GetCustomAttributes(typeof(KeyFieldAttribute), false).FirstOrDefault() as KeyFieldAttribute;
                if (keyFieldAttribute != null)
                {
                    keyList.Add(keyItem.Name);
                }
            }
            string sql = $"SELECT * FROM {tableName} WHERE 1 = 1";

            foreach (string keyStr in keyList)
            {
                sql += $" AND {keyStr} = @{keyStr} ";
            }
            bool succ       = false;
            var  parameters = typeof(T).GetProperties().Select(p => new SQLiteParameter($"@{p.Name}", p.GetValue(t, null) ?? DBNull.Value));

            using (SQLiteConnection sqlConnection = new SQLiteConnection(_conStr))
            {
                sqlConnection.Open();
                using (SQLiteCommand sqlCommand = new SQLiteCommand(sql, sqlConnection))
                {
                    sqlCommand.Parameters.AddRange(parameters.ToArray());
                    SQLiteDataReader dr = sqlCommand.ExecuteReader();
                    succ = dr.Read();
                    dr.Close();

                    sqlCommand.Dispose();
                    sqlConnection.Close();
                    sqlConnection.Dispose();
                }
            }
            return(succ);
        }
示例#2
0
        private bool Update <T>(T t, string key)
        {
            bool   succ      = false;
            string tableName = typeof(T).Name;
            string filed     = "";

            PropertyInfo[] propertyInfos = t.GetType().GetProperties();
            List <string>  keyList       = new List <string>();

            if (!string.IsNullOrEmpty(key))
            {
                keyList.Add(key);
            }
            foreach (var keyItem in propertyInfos)
            {
                KeyFieldAttribute keyFieldAttribute = keyItem.GetCustomAttributes(typeof(KeyFieldAttribute), false).FirstOrDefault() as KeyFieldAttribute;
                if (keyFieldAttribute != null)
                {
                    keyList.Add(keyItem.Name);
                }
            }

            filed = string.Join(",", typeof(T).GetProperties().Where(p => !keyList.Contains(p.Name) && p.Name.ToUpper() != "ID").Select(p => $"[{p.Name}] = @{p.Name}"));
            string sql = $"UPDATE [{tableName}] SET {filed} WHERE 1 = 1 ";

            foreach (string keyStr in keyList)
            {
                sql += $" AND [{keyStr}] = @{keyStr} ";
            }
            var parameters = typeof(T).GetProperties().Select(p => new SQLiteParameter($"@{p.Name}", p.GetValue(t, null) ?? DBNull.Value));

            using (SQLiteConnection sqlConnection = new SQLiteConnection(_conStr))
            {
                sqlConnection.Open();
                using (SQLiteCommand sqlCommand = new SQLiteCommand(sql, sqlConnection))
                {
                    sqlCommand.Parameters.AddRange(parameters.ToArray());
                    succ = sqlCommand.ExecuteNonQuery() > 0;
                }
                sqlConnection.Close();
                sqlConnection.Dispose();
            }
            return(succ);
        }