FCL 系列 - 2. FCL.Database.dll

 

基于 oledb 的数据库访问,可用于 Oracle、MS SqlServer、Access、Excel ... 的数据库访问。

 

 

    [DeveloperInfo("Wagwei")]
    public class DataCenterOp : IWagDataCenter
    {
        string _sConn = string.Empty;
        Dictionary<string, ConnAndTrans> _dicConnAndTrans;
        DBType _DBType = DBType.ACCESS;

        public DBType DatabaseType;
        public void Init(string conn, DBType dbType);

        public void TryToConnect();
        
        #region 构造(多态)

        public DataCenterOp()
        {
        }

        public DataCenterOp(string conn, DBType dbType);
        public void InitSqlServerOp(string dataSource, string password, string initialCatalog);
        public void InitSqlServerOp(string dataSource, string initialCatalog);
        public void InitAccessOp(string dataSource, string password);
        public void InitAccessOpNew(string dataSource, string password);
        public void InitText(string file);
        public void InitExcelOp(string dataSource, string pwd, string hdr, string imex);
        public void InitOracleOp(string userId, string dataSource, string pwd, string distibtx);
        #endregion

        #region 事务: 创建, 提交, 回滚, 获取

        public string CreateTran();
        public void CommitTran(string transGuid);
        public void RollbackTran(string transGuid);

        #endregion

        #region 执行sql语句

        public object ExecScalar(string sql, string transGuid = "");
        public object ExecScalar(SqlBuilder sqlB, string transGuid);
        public object ExecScalar(SqlBuilder[] arraySqlB, string transGuid);
        public int ExecNonQuery(string sql, string transGuid);
        public int ExecNonQuery(string sql);
        public int ExecNonQuery(SqlBuilder sqlB, string transGuid);
        public int ExecNonQuery(SqlBuilder sqlB);
        public int[] ExecNonQuery(SqlBuilder[] arraySqlB, string transGuid);
        #endregion

        #region 获取dt, ds

        public DataTable GetDt(string sql, string tableName, bool isSchema, string transGuid);
        public DataTable GetDt(SqlBuilder sqlB, bool isSchema, string transGuid);
        public DataSet GetDs(string sqls, string tableNames, bool isSchema, string transGuid);
        public DataSet GetDs(SqlBuilder[] arraySqlB, bool schema, string transGuid);
        #endregion

        #region 更新dt, ds

        public void UpdateTable(DataTable dt, string tableName, string keyFields, string updateFields, string transGuid);
        public void UpdateTableWithoutConflict(DataTable dt, string tableName, string keyFields, string updateFields, string transGuid);
        #endregion

        #region 更新数据(根据DataInfo)

        public void UpdateDataInfo(List<DataInfo> listDataInfo, string transGuid);

        #endregion

        #region 更新数据(插入, 删除, 更新), 根据FieldAndValue

        //插入
        public int InsertData(string tableName,
                                FieldAndValue[] keyFieldsAndValue,
                                FieldAndValue[] UpdateFieldsAndValue,
                                string transGuid);

        //删除
        public int DeleteData(string tableName,
                                FieldAndValue[] FieldsAndValues,
                                string transGuid);
                                
        //更新
        public int UpdateData(string tableName,
                                FieldAndValue[] keyFieldsAndValue,
                                FieldAndValue[] UpdateFieldsAndValue,
                                string transGuid);

        #endregion

        #region 常用数据库操作

        public DataTable GetDmExecSessions(int type = 0);
        
        public void InitializeDatabase();
        
        public string GetGenCode(string id);
        
        #region 将T_Sys_Variables(var_key, var_value, remark)表作为配置信息存储媒介

        public string CfgGetValue(string key);
        public bool CfgSetValue(string key, string value);

        #endregion

        #endregion


    }//end class

 

 

 

简单示例代码:

DataCenterOp mDCO = new DataCenterOp();
DataTable mdt;

mDCO.InitSqlServerOp(@"JOHN-PC\DAVID", "DB_WagTest");
mDCO.InitSqlServerOp(@"JOHN-PC\DAVID", "1122", "DB_WagTest");

//使用sql语句查询
this.mdt = mDCO.GetDt("select * from t_products", "", false, "");

//直接将 DataTable 数据更新到数据库(根据行的状态、Cell 的状态)
mDCO.UpdateTable(mdt, "t_products", "*pid", "", "");
mdt.AcceptChanges();

//插入数据
SqlBuilder sb = new SqlBuilder("insert into t_log(filename) values(?)");
//sb = new SqlBuilder("insert into t_log(filename) values(?)", "值");
sb.AppendParams(DateTime.Now.ToString("yyyyMMddHHmmssffff"));
mDCO.ExecNonQuery(sb);

 

 

 

 

 

FCL 系列 - 2. FCL.Database.dll_第1张图片

 

FCL 系列 - 2. FCL.Database.dll_第2张图片

 

 

using System;
using System.Data;
using System.Windows.Forms;

using FCL.Database;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        DataCenterOp mDCO = new DataCenterOp();

        private void Form2_Load(object sender, EventArgs e)
        {
            string file = System.IO.Path.Combine(Application.StartupPath, "Db.xls");
            //@"B:\Users\Administrator\Desktop\Db.xls"
            mDCO.InitExcelOp(file, "", "Yes", "10");//1,2; 4,10

            //imex  中文  读取  写入
            //1      ok   ok     ng
            //2      ng   ok     ng
            //4      ng   ok     ok
            //10     ng   ok     ok
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataTable dt = mDCO.GetDt("select * from [Sheet1$]", "kk", false, "");
            this.dataGridView1.DataSource = dt;
        }

        //插入方式1
        private void button2_Click(object sender, EventArgs e)
        {
            string guid = mDCO.CreateTran();
            SqlBuilder sb = new SqlBuilder();
            sb.m_sSql = "insert into [Sheet1$](col1,col2,col3,备注) values(?,?,?,?)";
            sb.AppendParams("8", "88", DateTime.Now.ToString("yyyyMMddHHmmssffff"), "呵呵");
            mDCO.ExecNonQuery(sb, guid);
            mDCO.CommitTran(guid);
        }

        //插入方式2
        private void button3_Click(object sender, EventArgs e)
        {
            string guid = "";
            try
            {
                guid = mDCO.CreateTran();
                SqlBuilder sb = new SqlBuilder();
                sb.m_sSql = "insert into [Sheet1$](col1,col2,col3) values(?,?,?)";
                sb.AppendParams("8", "88", "888");
                mDCO.ExecNonQuery(sb, guid);
                mDCO.CommitTran(guid);
            }
            catch (Exception ex)
            {
                try
                {
                    if (!string.IsNullOrEmpty(guid))
                    {
                        mDCO.RollbackTran(guid);
                    }
                }
                catch { }

                MessageBox.Show(ex.ToString());
            }
        }



    }
}

 

你可能感兴趣的:(FCL 系列 - 2. FCL.Database.dll)