在WPF中调用的增删改查方法

来源:转载

/*----------------------------------------------------------------

 * Copyright (C) 2010 RootSoft * 版权所有。 
 * 
 * 文件功能描述:Web服务全局类
 * 
 * 作者:欧阳光
 *  
 * 创建标识: Sunlight 2010-01-15
 * 
 * 修改标识:   
 * 修改描述: 
//----------------------------------------------------------------*/

using System;
using System.Web;
using System.Collections;
using System.Data;
using System.IO;
/// <summary>
/// GolbalService 的摘要说明
/// </summary>
namespace eHelper.Database
{
    public class GlobalService
    {

        public GlobalService()
        {

        }

        private DBOperatorFactory.DBName _dbName = DBOperatorFactory.DBName.MESDB;

        public DBOperatorFactory.DBName DBName
        {
            get { return _dbName; }
            set { _dbName = value; }
        }


        public void SetDB(DBOperatorFactory.DBName dbName)
        {
            DBName = dbName;
        }

        public DBOperator GetDBOperator(DBOperatorFactory.DBName DBName)
        {
            return new DBOperator(DBName);
        }

        /// <summary>
        /// 新增、修改、删除的公用方法
        /// </summary>
        /// <param name="sPName"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public bool Save(string sPName, DataSet dt)
        {

            try
            {
                DBOperator db = GetDBOperator(DBName);
               
                return db.Save(sPName, dt.Tables[0].Rows[0]) > 0;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        /// <summary>
        /// 用DataAdapter更新数据的公用方法
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public int DataAdapterSave(DataSet dt)
        {
            try
            {
                DBOperator db = GetDBOperator(DBName);
                return db.DataAdapterSave(dt);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 用DataAdapter更新数据的公用方法
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="sSelect"></param>
        /// <returns></returns>
        public int DataAdapterSave(DataSet dt,string sSelect)
        {
            try
            {
                DBOperator db = GetDBOperator(DBName);
                return db.DataAdapterSave(dt,sSelect);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 根据条件查询,当没有结果集但有输出参数不会把参数返回
        /// </summary>
        /// <param name="sPName"></param>
        /// <param name="ds"></param>
        /// <returns></returns>
        public DataSet QueryBySP(string sPName, DataSet ds)
        {
            try
            {
                DBOperator db = GetDBOperator(DBName);
                return db.QuerySP(sPName, ds.Tables[0].Rows[0]);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 根据条件查询,当没有结果集但有输出参数不会把参数返回
        /// </summary>
        /// <param name="sPName"></param>
        /// <param name="ds"></param>
        /// <returns></returns>
        public DataSet QueryBy(string sPName, DataSet ds)
        {
            try
            {
                DBOperator db = GetDBOperator(DBName);
                return db.Query(sPName, ds.Tables[0].Rows[0]);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 根据条件查询,当没有结果集但有输出参数会把参数返回
        /// </summary>
        /// <param name="sPName"></param>
        /// <param name="ds"></param>
        /// <returns></returns>
        public DataSet QueryByOutPrm(string sPName, DataSet ds)
        {
            try
            {
                DBOperator db = GetDBOperator(DBName);
                return db.QueryByOutPrm(sPName, ds.Tables[0].Rows[0]);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 根据条件查询
        /// </summary>
        /// <param name="sPName"></param>
        /// <returns></returns>
        public DataSet QueryAll(string sPName)
        {
            try
            {
                DBOperator db = GetDBOperator(DBName);
                return db.QueryAll(sPName);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 根据条件查询
        /// </summary>
        /// <param name="sPName"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public object Get(string sPName, DataTable dt)
        {
            try
            {
                DBOperator db = GetDBOperator(DBName);
                return db.Get(sPName, dt.Rows[0]);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }


        /// <summary>
        /// 根据表名称取表的结构
        /// </summary>
        /// <param name="sTableName"></param>
        /// <returns></returns>
        public DataSet GetSchema(string sTableName)
        {
            try
            {
                DBOperator db = GetDBOperator(DBName);
                return db.ExecuteQuery("SELECT * FROM " + sTableName + " WHERE 1=2");
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 根据传入SQL语句取数据
        /// </summary>
        /// <param name="sCommand"></param>
        /// <returns></returns>
        public DataSet GetDataBySql(string sCommand)
        {
            try
            {
                DBOperator db = GetDBOperator(DBName);
                return db.ExecuteQuery(sCommand);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 根据传入Key和TableName删除数据
        /// </summary>
        /// <param name="sKey"></param>
        /// <param name="sKeyValue"></param>
        /// <param name="sTable"></param>
        /// <returns></returns>
        public bool DeleteByKey(string sKey, string sKeyValue, string sTable)
        {
            try
            {
                DBOperator db = GetDBOperator(DBName);
                return db.ExecuteDeleteByKey(sKey, sKeyValue, sTable);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 根据传入Sql执行Command
        /// </summary>
        /// <param name="sCommand"></param>
        /// <returns></returns>
        public bool ExecuteCmd(string sCommand)
        {
            try
            {
                DBOperator db = GetDBOperator(DBName);
                return db.ExecuteCmd(sCommand) > 0;

            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 根据传入Sql执行Command
        /// </summary>
        /// <param name="sPName"></param>
        /// <param name="ds"></param>
        /// <returns></returns>
        public bool ExecuteCommandSP(string sPName, DataSet ds)
        {
            try
            {
                DBOperator db = GetDBOperator(DBName);
                return db.ExecuteCommandSP(sPName, ds.Tables[0].Rows[0]);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        /// <summary>
        /// 执行存储过程并返回查询结果的第一列
        /// </summary>
        /// <param name="sPName"></param>
        /// <param name="ds"></param>
        /// <returns></returns>
        public object ExecuteScalar(string sPName, DataSet ds)
        {
            try
            {
                DBOperator db = GetDBOperator(DBName);
                return db.ExecuteScalar(sPName, ds);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }

        }

        /// <summary>
        /// 根据传入SQL语句取数据,以字符数组返回
        /// </summary>
        /// <param name="prefixText"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public String[] GetDataBySqlString(string prefixText, int count)
        {
            DataSet ds = GetDataBySql("select modlcode from RSADMODLM0");
            String[] str = new string[ds.Tables[0].Rows.Count];
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                str[i] = ds.Tables[0].Rows[i]["modlcode"].ToString();
            }
            Array.Sort(str, new CaseInsensitiveComparer());
            int index = Array.BinarySearch(str, prefixText,

              new CaseInsensitiveComparer());

            if (index < 0)
            {
                index = ~index;
            }

            int matchingCount;

            for (matchingCount = 0; matchingCount < count && index + matchingCount < str.Length; matchingCount++)
            {

                if (!str[index + matchingCount].StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase))
                {
                    break;
                }
            }

            String[] returnValue = new string[matchingCount];

            if (matchingCount > 0)
            {
                Array.Copy(str, index, returnValue, 0, matchingCount);
            }

            return returnValue;


        }

        public string ConnectDB()
        {
            try
            {
                DBOperator db = GetDBOperator(DBName);
                return Convert.ToString(db.ExecuteScalarBySP("GetSystemTime_sp"));

            }
            catch (Exception e)
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 返回数据库连接字符窜
        /// </summary>
        /// <returns></returns>
        public string GetConnectionString()
        {
            return System.Configuration.ConfigurationManager.ConnectionStrings["MESDB"].ConnectionString;
        }

        /// <summary>
        /// 返回设置人数
        /// </summary>
        /// <returns></returns>
        public string GetCountstring()
        {
            return System.Configuration.ConfigurationManager.AppSettings["count"];
        }

        #region 上传文件

        ////  [WebMethod(EnableSession=true)]
        ////  public DataSet LoadUserFolderInfo(long i_clientID)
        ////  {
        ////   DataSet m_dataSet = new DataSet();
        ////   DataTable m_table = new DataTable("FolderData");
        ////   WaveFolderManager.GetClientFolderData(m_table,i_clientID);
        ////   m_dataSet.Tables.Add(m_table);
        ////   return m_dataSet;
        ////  }

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="sFileName"></param>
        ///// <returns></returns>
        //public UploadInstance2 CreateUploadInstance(string sFileName)
        //{
        //    //   WebbUsers m_user = new WebbUsers();
        //    //   if(m_user.UserLogin(i_loginName,i_password))
        //    //   {
        //    UploadInstance2 m_inistance = new UploadInstance2(sFileName);
        //    //    m_user.LoadUserData(m_user.UserID);
        //    //    m_inistance.UserID   = m_user.UserID;
        //    //    m_inistance.PathOnServer = Path.Combine(this.Server.MapPath("."),m_user.FolderPath.Trim());
        //    m_inistance.PathOnServer = Path.Combine(this.Server.MapPath("."), "ClientFiles");
        //    m_inistance.CreateFile();
        //    //Session.Add(m_inistance.GUID,m_inistance);
        //    Session[m_inistance.GUID] = m_inistance;
        //    return m_inistance;
        //    //   }
        //    //   else
        //    //   {
        //    //    return null;
        //    //   }
        //}

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="m_instance"></param>
        ///// <param name="i_currentPoint"></param>
        ///// <param name="i_dataLength"></param>
        ///// <returns></returns>
        //public bool UploadFileData(UploadInstance2 m_instance, long i_currentPoint, long i_dataLength)
        //{
        //    // UploadInstance2 m_instance = Session[i_instanceID] as UploadInstance2;
        //    if (m_instance == null) return false;
        //    Stream m_stream = RequestSoapContext.Current.Attachments[0].Stream;
        //    byte[] m_data = new byte[m_stream.Length];
        //    m_stream.Read(m_data, 0, m_data.Length);
        //    m_stream.Close();
        //    return m_instance.UploadData(m_data, i_currentPoint, m_data.Length);
        //}

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="i_instance"></param>
        //public void AbandantUpload(UploadInstance2 i_instance)
        //{
        //    i_instance.AbandantUpload();
        //}
        //#endregion

        //#region 上传
        ///// <summary>
        ///// 返回是否文件上载成功与否
        ///// </summary>
        ///// <param name="fs"></param>
        ///// <param name="florder"></param>
        ///// <param name="FileName"></param>
        ///// <returns></returns>
        //public bool UploadFile(byte[] fs, ref string florder, ref string FileName)
        //{
        //    try
        //    {
        //        string path = Server.MapPath(".") + florder;
        //        //创建文件夹
        //        if (!Directory.Exists(path))
        //        {
        //            Directory.CreateDirectory(path);

        //        }
        //        ///定义并实例化一个内存流,以存放提交上来的字节数组。
        //        MemoryStream m = new MemoryStream(fs);
        //        ///定义实际文件对象,保存上载的文件。
        //        FileStream f = new FileStream(path + "//" + FileName, FileMode.Create);
        //        ///把内内存里的数据写入物理文件
        //        m.WriteTo(f);
        //        m.Close();
        //        f.Close();
        //        f = null;
        //        m = null;

        //        florder = path;
        //        FileName = FileName;
        //        return true;
        //    }
        //    catch
        //    {
        //        return false;
        //    }
        //}

        ///// <summary>
        ///// 上传
        ///// </summary>
        ///// <param name="FileName"></param>
        ///// <param name="StartPosition"></param>
        ///// <param name="bData"></param>
        ///// <returns></returns>
        //public bool UploadFileData2(string FileName, int StartPosition, byte[] bData)
        //{
        //    string strFullName = Server.MapPath("Uploads") + @"/" + FileName;
        //    FileStream fs = null;
        //    try
        //    {
        //        fs = new FileStream(strFullName, FileMode.OpenOrCreate,
        //            FileAccess.Write, FileShare.Write);
        //    }
        //    catch (IOException err)
        //    {
        //        Session["ErrorMessage"] = err.Message;
        //        return false;
        //    }

        //    using (fs)
        //    {
        //        fs.Position = StartPosition;
        //        fs.Write(bData, 0, bData.Length);
        //    }
        //    return true;
        //}
        #endregion

    }

}

本文来自miyabaobeii的博客,原文地址:http://blog.csdn.net/miyabaobeii/archive/2011/06/13/6540882.aspx


分享给朋友:
您可能感兴趣的文章:
随机阅读: