using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Videoworks.VPServer.Service;
using Videoworks.VPServer.Config;

using MySql.Data.MySqlClient;
using MySql.Data.Types;

namespace Videoworks.VPServer.DAL
{
    public class BaseDAL
    {
        private static string mediaConnectionString = AppConfig.GetConnectionString("media");
        private static int timeOut = 3600;


        public T GetTableA<T>(string[] args) where T : Table, new()
        {
            MySqlDataReader reader = MysqlHelper.Query(string.Concat(args), mediaConnectionString, timeOut);
            if (reader == null)
                return null;
            T table = default(T);
            if (reader.Read())
            {
                table = new T();
                table.Read(reader);
            }
            reader.Close();
            return table;
        }
        public T GetTable<T>(string sql)   where T : Table, new()
        {
            MySqlDataReader reader = MysqlHelper.Query(sql, mediaConnectionString, timeOut);
            if (reader == null)
                return null;
            T table = default(T);
            if (reader.Read())
            {
                table = new T();
                table.Read(reader);
            }
            reader.Close();
            return table;
        }
        public String GetResourceString(string sql)
        {
            MySqlDataReader reader = MysqlHelper.Query(sql, mediaConnectionString, timeOut);
            if (reader == null)
                return String.Empty;
            String tmp = String.Empty;
            if (reader.Read())
            {
                tmp = reader.GetString("source_description");
            }
            reader.Close();
            return tmp;
        }

        public bool GetTable<T>(string sql, T table) where T : Table
        {
            MySqlDataReader reader = MysqlHelper.Query(sql, mediaConnectionString, timeOut);
            if (reader == null)
                return false;
            if (reader.Read())
                table.Read(reader);
            reader.Close();
            return true;
        }
        public List<T> GetTablesA<T>(string []args) where T : Table, new()
        {
            MySqlDataReader reader = MysqlHelper.Query(string.Concat(args), mediaConnectionString, timeOut);
            if (reader == null)
                return null;
            List<T> tables = new List<T>();
            while (reader.Read())
            {
                T t = new T();
                t.Read(reader);
                tables.Add(t);
            }
            reader.Close();
            return tables;
        }
        public List<T> GetTables<T>(string sql) where T : Table, new()
        {
            MySqlDataReader reader = MysqlHelper.Query(sql, mediaConnectionString, timeOut);
            if (reader == null)
                return null;
            List<T> tables = new List<T>();
            while (reader.Read())
            {
                T t = new T();
                t.Read(reader);
                tables.Add(t);
            }
            reader.Close();
            return tables;
        }
        public  void GetTables<T>(string sql, List<T> tables) where T : Table, new()
        {
            MySqlDataReader reader = MysqlHelper.Query(sql, mediaConnectionString, timeOut);
            if (reader == null)
                return;
            while (reader.Read())
            {
                T t = new T();
                t.Read(reader);
                tables.Add(t);
            }
            reader.Close();
        }
        public int Add<T>(T table) where T : Table
        {
            return MysqlHelper.Add(table.AddSql(), mediaConnectionString, timeOut);
        }
        public void Update<T>(T table) where T : Table
        {
            MysqlHelper.Execute(table.UpdateSql(), mediaConnectionString, timeOut);
        }
        public void Delete<T>(T table) where T : Table
        {
            MysqlHelper.Execute(table.DelSql(), mediaConnectionString, timeOut);
        }
        public static int AddTable<T>(T table) where T : Table
        {
            return MysqlHelper.Add(table.AddSql(), mediaConnectionString, timeOut);
        }
        public int NewUpdate<T>(T table) where T : Table
        {
            return MysqlHelper.Execute(table.GetUpdateSql(), mediaConnectionString, timeOut);
        }
        public static void Execute(string sql)
        {
            MysqlHelper.Execute(sql, mediaConnectionString, timeOut);
        }
        public static bool Execute(List<string> sqls)
        {
            return MysqlHelper.Execute(sqls, mediaConnectionString, timeOut);
        }
        public static int ExecuteAddSql(string sql)
        {
            return MysqlHelper.Add(sql, mediaConnectionString, timeOut);
        }

    }
}



BASEDAL里用到了模板


具体的DAL用法:派生后,直接Add(object) Delete(object) Update(object) 

还可以这么用List documents = base.GetTables("select * from document where rid="+rid.ToString());

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐