コード例 #1
0
        public int Insert <T>(T value)
        {
            StringBuilder sb  = new StringBuilder(string.Format(" insert into {0} (", typeof(T).Name));
            StringBuilder sb2 = new StringBuilder(" values (");

            PropertyInfo[] pinfos =
                typeof(T).GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance);
            List <SqlParameter> parameters = new List <SqlParameter>();
            string primarykey = DbComm.GetPrimarykey <T>();
            bool   isauto     = false;

            foreach (PropertyInfo item in pinfos)
            {
                if (primarykey == item.Name)
                {
                    IsAutoId isautoid = Attribute.GetCustomAttribute(item, typeof(IsAutoId)) as IsAutoId;
                    if (isautoid != null)
                    {
                        isauto = isautoid.SetIsAutoId;
                    }
                    continue;
                }
                sb.AppendFormat(" [{0}],", item.Name);
                sb2.AppendFormat("@{0},", item.Name);
                parameters.Add(new SqlParameter(item.Name, item.GetValue(value, null)));
            }
            sb  = sb.Replace(',', ')', sb.Length - 1, 1);
            sb2 = sb2.Replace(',', ')', sb2.Length - 1, 1);
            sb.Append(sb2);
            if (isauto)
            {
                sb.AppendFormat(" SELECT IDENT_CURRENT('{0}') ", typeof(T).Name);
                object obj = ExecuteScalar(sb.ToString(), parameters.ToArray());
                if (obj != null && obj != DBNull.Value)
                {
                    return(Convert.ToInt32(obj));
                }
            }
            return(ExecuteNonQuery(sb.ToString(), parameters.ToArray()));
        }
コード例 #2
0
        public int Insert <T>(List <T> values)
        {
            PropertyInfo[] pinfos     = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance);
            StringBuilder  sb         = new StringBuilder(string.Format(" insert into {0} ( ", typeof(T).Name));
            StringBuilder  sb1        = new StringBuilder(" values ( ");
            string         primarykey = DbComm.GetPrimarykey <T>();
            bool           isauto     = false;

            foreach (PropertyInfo item in pinfos)
            {
                if (primarykey == item.Name)
                {
                    IsAutoId isautoid = Attribute.GetCustomAttribute(item, typeof(IsAutoId)) as IsAutoId;
                    if (isautoid != null)
                    {
                        isauto = isautoid.SetIsAutoId;
                    }
                    continue;
                }
                sb.AppendFormat("{0},", item.Name);
                sb1.Append("?,");
            }
            sb  = sb.Replace(',', ')', sb.Length - 1, 1);
            sb1 = sb1.Replace(',', ')', sb1.Length - 1, 1);
            sb.Append(sb1);
            if (isauto)
            {
                sb.Append(" ; select last_insert_rowid(); ");
            }
            int count             = 0;
            SQLiteConnection conn = new SQLiteConnection(_connectionstr);
            SQLiteCommand    comm = new SQLiteCommand(sb.ToString(), conn);

            conn.Open();
            SQLiteTransaction tra = conn.BeginTransaction();

            foreach (T item in values)
            {
                List <SQLiteParameter> parameters = new List <SQLiteParameter>();
                foreach (PropertyInfo pinfo in pinfos)
                {
                    if (primarykey == pinfo.Name)
                    {
                        continue;
                    }
                    parameters.Add(new SQLiteParameter(pinfo.Name, pinfo.GetValue(item, null)));
                }
                comm.Parameters.Clear();
                comm.Parameters.AddRange(parameters.ToArray());
                if (isauto)
                {
                    object obj = comm.ExecuteScalar();
                    if (obj != null && obj != DBNull.Value)
                    {
                        if (!string.IsNullOrEmpty(primarykey))
                        {
                            PropertyInfo info = item.GetType().GetProperty(primarykey);
                            info.SetValue(item, obj, null);
                        }
                    }
                }
                else
                {
                    comm.ExecuteNonQuery();
                }
                count++;
            }
            tra.Commit();
            tra.Dispose();
            return(count);
        }