/*----------------------------------------------------------------
* Copyright (C) 2010 RootSoft
* 版权所有。
*
* 文件功能描述:Web服务全局类
*
* 作者:欧阳光
*
* 创建标识: Sunlight 2010-01-15
*
* 修改标识:
* 修改描述:
//----------------------------------------------------------------*/
using System;
using System.Web;
using System.Collections;
using System.Data;
using System.IO;
///
/// GolbalService 的摘要说明
///
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);
}
///
/// 新增、修改、删除的公用方法
///
///
///
///
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);
}
}
///
/// 用DataAdapter更新数据的公用方法
///
///
///
public int DataAdapterSave(DataSet dt)
{
try
{
DBOperator db = GetDBOperator(DBName);
return db.DataAdapterSave(dt);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
///
/// 用DataAdapter更新数据的公用方法
///
///
///
///
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);
}
}
///
/// 根据条件查询,当没有结果集但有输出参数不会把参数返回
///
///
///
///
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);
}
}
///
/// 根据条件查询,当没有结果集但有输出参数不会把参数返回
///
///
///
///
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);
}
}
///
/// 根据条件查询,当没有结果集但有输出参数会把参数返回
///
///
///
///
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);
}
}
///
/// 根据条件查询
///
///
///
public DataSet QueryAll(string sPName)
{
try
{
DBOperator db = GetDBOperator(DBName);
return db.QueryAll(sPName);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
///
/// 根据条件查询
///
///
///
///
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);
}
}
///
/// 根据表名称取表的结构
///
///
///
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);
}
}
///
/// 根据传入SQL语句取数据
///
///
///
public DataSet GetDataBySql(string sCommand)
{
try
{
DBOperator db = GetDBOperator(DBName);
return db.ExecuteQuery(sCommand);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
///
/// 根据传入Key和TableName删除数据
///
///
///
///
///
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);
}
}
///
/// 根据传入Sql执行Command
///
///
///
public bool ExecuteCmd(string sCommand)
{
try
{
DBOperator db = GetDBOperator(DBName);
return db.ExecuteCmd(sCommand) > 0;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
///
/// 根据传入Sql执行Command
///
///
///
///
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);
}
}
///
/// 执行存储过程并返回查询结果的第一列
///
///
///
///
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);
}
}
///
/// 根据传入SQL语句取数据,以字符数组返回
///
///
///
///
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;
}
}
///
/// 返回数据库连接字符窜
///
///
public string GetConnectionString()
{
return System.Configuration.ConfigurationManager.ConnectionStrings["MESDB"].ConnectionString;
}
///
/// 返回设置人数
///
///
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;
//// }
/////
/////
/////
/////
/////
//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;
// // }
//}
/////
/////
/////
/////
/////
/////
/////
//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);
//}
/////
/////
/////
/////
//public void AbandantUpload(UploadInstance2 i_instance)
//{
// i_instance.AbandantUpload();
//}
//#endregion
//#region 上传
/////
///// 返回是否文件上载成功与否
/////
/////
/////
/////
/////
//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;
// }
//}
/////
///// 上传
/////
/////
/////
/////
/////
//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
}
}