Exemplo n.º 1
0
        /// <summary>
        /// 读取最多一行数据。当无数据,返回null。当数据超过一行将只读取第一行
        /// </summary>
        /// <param name="converter"></param>
        /// <returns></returns>
        public object FirstLine(ConvertReaderToEntity converter)
        {
            using (SqlConnection conn = Connection()) {
                using (SqlCommand cmd = conn.CreateCommand()) {
                    cmd.CommandText = SQLScript;
                    if (Parameters != null && Parameters.Count != 0)                       //Append pairs
                    {
                        foreach (KeyValuePair <string, object> p in Parameters)
                        {
                            cmd.Parameters.AddWithValue(p.Key, p.Value);
                        }
                    }
                    if (conn.State == System.Data.ConnectionState.Closed)
                    {
                        conn.Open();
                    }

                    object re = null;
                    using (SqlDataReader reader = cmd.ExecuteReader()) {
                        if (reader.Read())
                        {
                            re = converter(reader);
                        }
                    }
                    return(re);
                }
            }
        }
Exemplo n.º 2
0
        public List <T> LinesStruct <T>(ConvertReaderToEntity converter) where T : struct
        {
            using (SqlConnection conn = Connection()) {
                using (SqlCommand cmd = conn.CreateCommand()) {
                    cmd.CommandText = SQLScript;
                    if (Parameters != null && Parameters.Count != 0)                       //Append pairs
                    {
                        foreach (KeyValuePair <string, object> p in Parameters)
                        {
                            cmd.Parameters.AddWithValue(p.Key, p.Value);
                        }
                    }
                    if (conn.State == System.Data.ConnectionState.Closed)
                    {
                        conn.Open();
                    }

                    List <T> re = new List <T>();
                    T        obj;
                    using (SqlDataReader reader = cmd.ExecuteReader()) {
                        if (reader.Read())
                        {
                            obj = (T)converter(reader);
                            re.Add(obj);
                        }
                    }
                    return(re);
                }
            }
        }