using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using System.Reflection;
using System.Collections;
using System.Data.Odbc;
using System.Data.OleDb;

namespace CNAS_DBSync
{
    //数据库类
    public static class SQLDB2Helper
    {
        private static log4net.ILog log = log4net.LogManager.GetLogger("SQLDB2Helper");

        private static string connectionString = "";

        public static void createConnectString(string strHost,string strName,string strUser,string strPwd)
        {
            //connectionString = string.Format("Provider=IBMDADB2;HostName={0}; Port=50000;Data Source={1};UID={2};PWD={3};",strHost,strName,strUser,strPwd);

            connectionString = string.Format("Provider = IBMDADB2; HostName = {0}; Database = {1}; uid = {2}; pwd = {3}; protocol = TCPIP; port = 50000", strHost, strName, strUser, strPwd);
        }

        /// <summary>
        /// 执行查询语句，获取表数据
        /// </summary>
        /// <param name="strsql"></param>
        /// <returns></returns>
        public static DataTable getDB2Table(string strsql)
        {
            DataTable dt = new DataTable();
            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                OleDbCommand cmd = new OleDbCommand(strsql, conn);
                try
                {
                    conn.Open();
                    OleDbDataAdapter adp = new OleDbDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    adp.Fill(ds);
                    dt = ds.Tables[0];
                }
                catch (Exception ex)
                {
                    //这里写日志
                    log.Error(ex.Message);
                }
            }

            return dt;
        }

        //执行插入/更新语句,返回受影响的行数(无参数)
        public static int ExequeryDB2Table(string strSql)
        {
            int ireturn = 0;

            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                OleDbCommand cmd = new OleDbCommand(strSql, conn);
                try
                {
                    conn.Open();

                    ireturn=cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    //这里写日志
                    log.Error(ex.Message);
                }
            }

            return ireturn;
        }


        //执行插入/更新语句,返回受影响的行数(有参数)
        public static int ExequeryDB2Table(string strSql,params OleDbParameter[] parameters)
        {
            int ireturn = 0;

            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                OleDbCommand cmd = new OleDbCommand(strSql, conn);
                try
                {
                    conn.Open();
                    cmd.CommandText = strSql;
                    if (parameters.Length != 0)
                    {
                        cmd.Parameters.AddRange(parameters);
                    }
                    ireturn = cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    //这里写日志
                    log.Error(ex.Message);
                }
            }

            return ireturn;
        }

        /// <summary>
        /// 执行插入/更新语句,返回受影响的行数(有参数)
        /// </summary>
        public static bool TestCnasLink()
        {
            bool bIfSuccess = true;

            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                try
                {
                    conn.Open();
                }
                catch (Exception ex)
                {
                    //这里写日志
                    bIfSuccess = false;
                    log.Error(ex.Message);
                }
            }

            return bIfSuccess;
        }

    }
}
