zoukankan      html  css  js  c++  java
  • 超简DbHelper

    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Dynamic;
    
    namespace ConsoleApplication1 {
        public class QuickDB {
            static void Main() {
                var sql = "select * from Categories";
                var connectionString = @"Data Source=(localdb)MSSQLLocalDB;Initial Catalog=Northwind;";
                var list = new QuickDB(connectionString).ExecuteReader(sql);
                for (int i = 0; i < list.Length; i++) {
                    var item = list[i];
                    Console.WriteLine(item.CategoryName);
                }
                Console.ReadLine();
            }
    
            private string connectionString;
            public QuickDB(string connectionString) {
                this.connectionString = connectionString;
            }
    
            private void command(string sql, Action<SqlCommand> act) {
                var c = new SqlConnection(connectionString);
                c.Open();
                var comm = c.CreateCommand();
                comm.CommandText = sql;
                act(comm);
                c.Close();
            }
    
            private T command<T>(string sql, Func<SqlCommand, T> func) {
                T t = default(T);
                command(sql, c => { t = func(c); });
                return t;
            }
    
            public int ExecuteNonQuery(string sql) {
                return command(sql, c => {
                    return c.ExecuteNonQuery();
                });
            }
    
            public object ExecuteScalar(string sql) {
                var r = ExecuteReader(sql);
                return r.Length == 0 ? null : r[0][0];
            }
    
            public dynamic[] ExecuteReader(string sql) {
                return command(sql, c => {
                    var q = c.ExecuteReader();
                    var r = new List<dynamic>();//will return this.
                    var ns = new List<string>();//column names
                    while (q.Read()) {
                        if (ns.Count == 0) {
                            for (int i = 0; i < q.FieldCount; i++) {
                                ns.Add(q.GetName(i));
                            }
                            ns.Sort();
                        }
                        IDictionary<string, object> obj = new ExpandoObject();//same to javascript object
                        foreach (var n in ns) {
                            obj.Add(n, q[n]);
                        }
                        r.Add(obj);
                    }
                    q.Close();
                    return r.ToArray();
                });
            }
        }
    }
  • 相关阅读:
    Codeforces Round #107 (Div. 1) D Mission Impassable
    Codeforces Round #107 (Div. 1) C Smart Cheater
    Codeforces Round #104 (Div. 1) D Lucky Pair
    Codeforces Round #104 (Div. 1) C Lucky Subsequence
    拓扑排序&&欧拉(回)路
    复习笔记之矩阵快速幂(不定时更新)
    复习笔记之母函数
    树链剖分来一发
    最短路算法略解
    题目记录
  • 原文地址:https://www.cnblogs.com/ly45/p/6279186.html
Copyright © 2011-2022 走看看