コード例 #1
0
        public DbDataReader ExecuteReaderP(string sql, NVCollection parameters = null)
        {
            SqlConnection connection = GetConnection();
            SqlCommand    cmd        = CreateCommand();

            cmd.Connection  = connection;
            cmd.CommandText = sql;

            Attach(cmd, parameters);

            connection.Open();
            return(cmd.ExecuteReader(CommandBehavior.CloseConnection));
        }
コード例 #2
0
 private void Attach(SqlCommand cmd, NVCollection nvc)
 {
     if (nvc != null)
     {
         if (nvc.Count > 0)
         {
             foreach (var item in nvc)
             {
                 var p = CreateParameter("@" + item.Key, item.Value);
                 cmd.Parameters.Add(p);
             }
         }
     }
 }
コード例 #3
0
        public NVCollection ConvertToNVC(object[] parameters)
        {
            NVCollection nvc = null;

            if (parameters != null)
            {
                if (parameters.Length > 0)
                {
                    var obj = parameters[0];

                    var t = obj.GetType();



                    if (obj is NVCollection)
                    {
                        nvc = obj as NVCollection;

                        return(nvc);
                    }
                    else if (obj is DbParameter)
                    {
                        var p = obj as DbParameter;

                        nvc = new NVCollection();

                        nvc[p.ParameterName] = p.Value;

                        return(nvc);
                    }

                    nvc = new NVCollection(parameters.Length);

                    for (int i = 0; i < parameters.Length; i++)
                    {
                        nvc.Add(i.ToString(), parameters[i]);
                    }
                }
            }

            return(nvc);
        }
コード例 #4
0
        public T ExecuteScalarP <T>(string sql, NVCollection parameters = null)
        {
            SqlConnection connection = GetConnection();

            SqlCommand cmd = CreateCommand();

            cmd.Connection  = connection;
            cmd.CommandText = sql;

            Attach(cmd, parameters);

            connection.Open();
            object o = cmd.ExecuteScalar();

            connection.Close();

            cmd.Parameters.Clear();
            cmd.Dispose();

            Type typ = typeof(T);

            if (typ == typeof(int) ||
                typ == typeof(double) ||
                typ == typeof(decimal) ||
                typ == typeof(long) ||
                typ == typeof(float)
                )
            {
                if (o != null && o != DBNull.Value)
                {
                    return((T)(o));
                }
                else
                {
                    return(default(T));
                }
            }
            else
            {
                return((T)(Convert.ChangeType(o, typ)));
            }
        }
コード例 #5
0
        public int ExecuteNoneQueryP(string sql, NVCollection parameters = null)
        {
            SqlConnection connection = GetConnection();
            SqlCommand    cmd        = CreateCommand();

            cmd.Connection  = connection;
            cmd.CommandText = sql;

            Attach(cmd, parameters);

            connection.Open();
            int num = cmd.ExecuteNonQuery();

            connection.Close();

            cmd.Parameters.Clear();
            cmd.Dispose();

            return(num);
        }
コード例 #6
0
        public NVCollection GetDataP(string sql, NVCollection parameters = null)
        {
            NVCollection nv = null;

            using (DbDataReader reader = ExecuteReaderP(sql, parameters))
            {
                while (reader.Read())
                {
                    nv = new NVCollection(reader.FieldCount);

                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        nv.Add(reader.GetName(i), reader.GetValue(i));
                    }
                }
                reader.Close();
                reader.Dispose();
            }

            return(nv);
        }
コード例 #7
0
        public T ExecuteScalarP <T>(string sql, NVCollection parameters = null)
        {
            SqlConnection connection = GetConnection();

            SqlCommand cmd = CreateCommand();

            cmd.Connection  = connection;
            cmd.CommandText = sql;

            Attach(cmd, parameters);

            connection.Open();
            object o = cmd.ExecuteScalar();

            connection.Close();

            cmd.Parameters.Clear();
            cmd.Dispose();

            return((T)Convert.ChangeType(o, typeof(T)));
        }
コード例 #8
0
 private void Attach(SqlCommand cmd, NVCollection nvc)
 {
     if (nvc != null)
     {
         if (nvc.Count > 0)
         {
             foreach (var item in nvc)
             {
                 SqlParameter p = null;
                 if (item.Key.IndexOf("@") == 0)
                 {
                     p = CreateParameter(item.Key, item.Value);
                 }
                 else
                 {
                     p = CreateParameter("@" + item.Key, item.Value);
                 }
                 cmd.Parameters.Add(p);
             }
         }
     }
 }
コード例 #9
0
        public List <NVCollection> GetDataListP(string sql, NVCollection parameters = null)
        {
            List <NVCollection> list = new List <NVCollection>();

            using (DbDataReader reader = ExecuteReaderP(sql, parameters))
            {
                while (reader.Read())
                {
                    NVCollection nvc = new NVCollection(reader.FieldCount);

                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        nvc.Add(reader.GetName(i), reader.GetValue(i));
                    }

                    list.Add(nvc);
                }
                reader.Close();
                reader.Dispose();
            }

            return(list);
        }