锐腾搅拌上料功能
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1084 lines
44 KiB

5 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Security.Cryptography;
  7. using System.Data;
  8. using System.Reflection;
  9. using System.Data.SqlClient;
  10. using System.Net;
  11. using System.Net.Sockets;
  12. //using System.Windows.Forms;
  13. using System.Data.OleDb;
  14. using System.CodeDom.Compiler;
  15. using System.CodeDom;
  16. //using System.Web.Services.Description;
  17. using ICSSoft.Base.Config.DBHelper;
  18. namespace ICSSoft.Frame.Data.DAL
  19. {
  20. public class APPConfigForOracl
  21. {
  22. public class AppConfig
  23. {
  24. #region 数据库连接符
  25. /// <summary>
  26. /// 框架数据库连接
  27. /// </summary>
  28. public static string FrameConnectString = "";
  29. /// <summary>
  30. /// 实际应用软件的数据库连接
  31. /// </summary>
  32. public static string AppConnectString = "";
  33. #endregion
  34. #region 用户登陆时提供的信息
  35. /// <summary>
  36. /// 用户的唯一标识
  37. /// </summary>
  38. public static string UserId = "";
  39. /// <summary>
  40. /// 当前登陆用户账户
  41. /// </summary>
  42. public static string UserCode = "";
  43. /// <summary>
  44. /// 当前登陆用户姓名
  45. /// </summary>
  46. public static string UserName = "";
  47. /// <summary>
  48. /// 当前登陆用户口令(加密)
  49. /// </summary>
  50. public static string UserPwd = "";
  51. /// <summary>
  52. /// 站点编号
  53. /// </summary>
  54. public static string WorkPointCode = "";
  55. /// <summary>
  56. /// 站点名称
  57. /// </summary>
  58. public static string WorkPointName = "";
  59. /// <summary>
  60. /// 当前用户是否管理员
  61. /// </summary>
  62. public static bool AdminFlag = false;
  63. /// <summary>
  64. /// 公司名称
  65. /// </summary>
  66. public static string CompanyName = "";
  67. /// <summary>
  68. /// 当前语言包 1代表简体 2 代表繁体 3代表英文
  69. /// </summary>
  70. public static string LangIndex = "1";
  71. ///// <summary>
  72. ///// 转换规则
  73. ///// </summary>
  74. //public static string LangConvert = "";
  75. /// <summary>
  76. /// 基础服务的路径
  77. /// </summary>
  78. public static string BaseServicePath = System.AppDomain.CurrentDomain.BaseDirectory + "\\BaseServiceConfig\\";
  79. /// <summary>
  80. /// 基础服务设置文件
  81. /// </summary>
  82. public static string BaseServiceFile = BaseServicePath + "baseserviceurl.ini";
  83. /// <summary>
  84. /// 登录时保存路径
  85. /// </summary>
  86. public static string LoginConfigPath = System.AppDomain.CurrentDomain.BaseDirectory + "\\LoginConfig\\";
  87. /// <summary>
  88. /// 登录时保存文件
  89. /// </summary>
  90. public static string LoginConfigFile = LoginConfigPath + "loginconfig.ini";
  91. /// <summary>
  92. /// 语言库设置路径
  93. /// </summary>
  94. public static string LanguageConfigPath = System.AppDomain.CurrentDomain.BaseDirectory + "\\Language\\";
  95. /// <summary>
  96. /// 语言包XML
  97. /// </summary>
  98. public static string LanguageConfigFile = LanguageConfigPath + "lang.xml";
  99. /// <summary>
  100. /// 默认语言文件
  101. /// </summary>
  102. public static string LanguageConfigDefaultStyle = LanguageConfigPath + "defaultlang.ini";
  103. /// <summary>
  104. /// 图片资源路径
  105. /// </summary>
  106. public static string ResourcePath = System.AppDomain.CurrentDomain.BaseDirectory + "\\Resource\\";
  107. /// <summary>
  108. /// 新版图片资源
  109. /// </summary>
  110. public static string ImagesPath = System.AppDomain.CurrentDomain.BaseDirectory + "\\Images\\";
  111. /// <summary>
  112. /// 系统图片
  113. /// </summary>
  114. //public static ImageList SysImageList = new ImageList();
  115. /// <summary>
  116. /// 新版系统图片
  117. /// </summary>
  118. //public static ImageList NewSysImageList = new ImageList();
  119. /// <summary>
  120. /// 新版系统图片
  121. /// </summary>
  122. //public static TabControl tc;
  123. /// <summary>
  124. /// 当前登陆设备
  125. /// </summary>
  126. public static string EQPCode = "";
  127. /// <summary>
  128. /// 当前登陆工序
  129. /// </summary>
  130. public static string OPCode = "";
  131. /// <summary>
  132. /// 当前登陆资源
  133. /// </summary>
  134. public static string RESCode = "";
  135. #endregion
  136. #region 字符串加解密
  137. /// <summary>
  138. /// MD5加密
  139. /// </summary>
  140. /// <param name="str"></param>
  141. /// <returns></returns>
  142. public static string ToMd5(string str)
  143. {
  144. return Encrypt(str, "&%#@?,:*_");
  145. }
  146. /// <summary>
  147. /// MD5解密
  148. /// </summary>
  149. /// <param name="str"></param>
  150. /// <returns></returns>
  151. public static string FromMd5(string str)
  152. {
  153. return Decrypt(str, "&%#@?,:*_");
  154. }
  155. /// <summary>
  156. /// 加密
  157. /// </summary>
  158. /// <param name="strText"></param>
  159. /// <param name="strEncrKey"></param>
  160. /// <returns></returns>
  161. private static String Encrypt(String strText, String strEncrKey)
  162. {
  163. Byte[] byKey = { };
  164. Byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  165. try
  166. {
  167. byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
  168. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  169. Byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
  170. MemoryStream ms = new MemoryStream();
  171. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV),
  172. CryptoStreamMode.Write);
  173. cs.Write(inputByteArray, 0, inputByteArray.Length);
  174. cs.FlushFinalBlock();
  175. return Convert.ToBase64String(ms.ToArray());
  176. }
  177. catch (Exception ex)
  178. {
  179. return ex.Message;
  180. }
  181. }
  182. /// <summary>
  183. /// 解密
  184. /// </summary>
  185. /// <param name="strText"></param>
  186. /// <param name="sDecrKey"></param>
  187. /// <returns></returns>
  188. private static String Decrypt(String strText, String sDecrKey)
  189. {
  190. Byte[] byKey = { };
  191. Byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  192. Byte[] inputByteArray = new byte[strText.Length];
  193. try
  194. {
  195. byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
  196. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  197. inputByteArray = Convert.FromBase64String(strText);
  198. MemoryStream ms = new MemoryStream();
  199. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV),
  200. CryptoStreamMode.Write);
  201. cs.Write(inputByteArray, 0, inputByteArray.Length);
  202. cs.FlushFinalBlock();
  203. System.Text.Encoding encoding = System.Text.Encoding.UTF8;
  204. return encoding.GetString(ms.ToArray());
  205. }
  206. catch (Exception ex)
  207. {
  208. return ex.Message;
  209. }
  210. }
  211. #endregion
  212. #region 获取服务器时间
  213. public static DateTime GetSeverDateTime(string formatString)
  214. {
  215. string dateSql = "SELECT getdate()";
  216. return Convert.ToDateTime(Convert.ToDateTime(DBHelper.ExecuteScalar(FrameConnectString, CommandType.Text, dateSql).ToString()).ToString(formatString));
  217. }
  218. public static double GetDateDiffForDays(string sDate, string dDate)
  219. {
  220. string sql = "SELECT DATEDIFF(DAY,'" + sDate + "','" + dDate + "')";
  221. object obj = DBHelper.ExecuteScalar(AppConfig.FrameConnectString, CommandType.Text, sql);
  222. return Convert.ToDouble(obj);
  223. }
  224. #endregion
  225. #region 获取唯一GUID
  226. public static string GetGuid()
  227. {
  228. return Guid.NewGuid().ToString();
  229. }
  230. #endregion
  231. #region WCF基本参数
  232. /// <summary>
  233. /// 基础服务WCF路径
  234. /// </summary>
  235. public static string BaseServiceUri = "";
  236. #endregion
  237. #region 获取本机器信息
  238. /// <summary>
  239. /// 获取本地IP地址
  240. /// </summary>
  241. /// <returns>IP地址</returns>
  242. public static string GetIpAddress()
  243. {
  244. string ipstr = "";
  245. IPAddress[] arrIPAddresses = Dns.GetHostAddresses(Dns.GetHostName());
  246. foreach (IPAddress ip in arrIPAddresses)
  247. {
  248. if (ip.AddressFamily.Equals(AddressFamily.InterNetwork))
  249. {
  250. ipstr = ip.ToString();
  251. }
  252. }
  253. return ipstr;
  254. }
  255. public static string GetComputerName()
  256. {
  257. string ip = GetIpAddress();
  258. IPHostEntry hostInfo = Dns.GetHostByAddress(ip);
  259. return hostInfo.HostName;
  260. }
  261. public static string GetHostName()
  262. {
  263. return Dns.GetHostName();
  264. }
  265. #endregion
  266. #region 判断是否是端口格式
  267. /// <summary>
  268. /// 判断是否是端口号
  269. /// </summary>
  270. /// <param name="port"></param>
  271. /// <returns></returns>
  272. public static bool CheckIsPort(string port)
  273. {
  274. bool isPort = false;
  275. int portNum;
  276. isPort = Int32.TryParse(port, out portNum);
  277. if (isPort && portNum >= 0 && portNum <= 65535)
  278. {
  279. isPort = true;
  280. }
  281. else
  282. {
  283. isPort = false;
  284. }
  285. return isPort;
  286. }
  287. #endregion
  288. #region 测试连接符是否能够连接
  289. /// <summary>
  290. /// 测试连接符是否成功
  291. /// </summary>
  292. /// <param name="str"></param>
  293. /// <returns></returns>
  294. public static bool CheckDbConnectionState(string str)
  295. {
  296. bool flag = false;
  297. try
  298. {
  299. using (SqlConnection conn = new SqlConnection(str))
  300. {
  301. conn.Open();
  302. if (conn.State == ConnectionState.Open)
  303. {
  304. flag = true;
  305. }
  306. else
  307. {
  308. flag = false;
  309. }
  310. }
  311. }
  312. catch
  313. {
  314. flag = false;
  315. }
  316. return flag;
  317. }
  318. #endregion
  319. #region 反射窗体
  320. //通用窗体入口
  321. public struct ChildWinManagement
  322. {
  323. public static string WinCaption(string r) { return r; }
  324. //public static bool ExistWin(Form MDIwin, string caption) { bool R = false; foreach (Form f in MDIwin.MdiChildren) { if (f.Name + f.Text == caption) { R = true; f.Show(); f.Activate(); break; } } return R; }
  325. }
  326. //系统入口方法
  327. //public static void CreateFormShow(string MDll, string MCode, string MName, string MForm, bool IsShowDialog, Form MainForm, string FormKeyValue, bool maxFlag)
  328. //{
  329. // try
  330. // {
  331. // bool canEnterFlag = GetUserExcuteRight(MCode, "see");
  332. // if (canEnterFlag == false)
  333. // {
  334. // MessageBox.Show("对不起,您无权限使用该功能!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  335. // return;
  336. // }
  337. // Form f = new Form();
  338. // string typeName = MDll + "." + MForm;
  339. // Assembly assembly = Assembly.LoadFrom(Application.StartupPath + @"\" + MDll + ".dll");
  340. // // 实例化窗体
  341. // Type t = assembly.GetType(typeName);
  342. // if (!string.IsNullOrEmpty(FormKeyValue))
  343. // {
  344. // FormKeyValue = FormKeyValue.Substring(0, FormKeyValue.Length - 1);
  345. // object[] strs = FormKeyValue.Split(',');
  346. // f = Activator.CreateInstance(t, strs) as Form;
  347. // }
  348. // else
  349. // {
  350. // f = Activator.CreateInstance(t) as Form;
  351. // }
  352. // f.Name = MForm;
  353. // f.Tag = MCode;
  354. // if (MainForm == null)
  355. // {
  356. // f.Text = MName;
  357. // f.WindowState = (maxFlag == true ? FormWindowState.Maximized : FormWindowState.Normal);
  358. // f.Text = MName;
  359. // f.Tag = MCode;
  360. // f.ShowDialog();
  361. // }
  362. // else
  363. // {
  364. // if (!ChildWinManagement.ExistWin(MainForm, f.Name + MName))
  365. // {
  366. // if (IsShowDialog == false)
  367. // {
  368. // f.MdiParent = MainForm;
  369. // f.WindowState = FormWindowState.Maximized;
  370. // f.Text = MName;
  371. // f.Tag = MCode;
  372. // f.Show();
  373. // }
  374. // else
  375. // {
  376. // f.WindowState = (maxFlag == true ? FormWindowState.Maximized : FormWindowState.Normal);
  377. // f.Text = MName;
  378. // f.Tag = MCode;
  379. // f.ShowDialog();
  380. // }
  381. // }
  382. // }
  383. // }
  384. // catch (Exception ex)
  385. // {
  386. // throw ex;
  387. // }
  388. //}
  389. //系统入口方法
  390. //public static void CloseFormShow(string MName)
  391. //{
  392. // try
  393. // {
  394. // if (tc != null)
  395. // {
  396. // foreach (TabPage tab in tc.TabPages)
  397. // {
  398. // if (tab.Text == MName)
  399. // {
  400. // tc.TabPages.Remove(tab);
  401. // tc.Refresh();
  402. // }
  403. // }
  404. // }
  405. // }
  406. // catch (Exception ex)
  407. // {
  408. // throw ex;
  409. // }
  410. //}
  411. #endregion
  412. #region DataTable分页
  413. /**/
  414. /// <summary>
  415. /// 根据索引和pagesize返回记录
  416. /// </summary>
  417. /// <param name="dt">记录集 DataTable</param>
  418. /// <param name="PageIndex">当前页</param>
  419. /// <param name="pagesize">一页的记录数</param>
  420. /// <returns></returns>
  421. public static DataTable GetPageData(DataTable dt, int PageIndex, int PageSize)
  422. {
  423. if (PageIndex == 0)
  424. return dt;
  425. DataTable newdt = dt.Clone();
  426. //newdt.Clear();
  427. int rowbegin = (PageIndex - 1) * PageSize;
  428. int rowend = PageIndex * PageSize;
  429. if (rowbegin >= dt.Rows.Count)
  430. return newdt;
  431. if (rowend > dt.Rows.Count)
  432. rowend = dt.Rows.Count;
  433. for (int i = rowbegin; i <= rowend - 1; i++)
  434. {
  435. DataRow newdr = newdt.NewRow();
  436. DataRow dr = dt.Rows[i];
  437. foreach (DataColumn column in dt.Columns)
  438. {
  439. newdr[column.ColumnName] = dr[column.ColumnName];
  440. }
  441. newdt.Rows.Add(newdr);
  442. }
  443. return newdt;
  444. }
  445. #endregion
  446. #region 获取数据源连接符
  447. /// <summary>
  448. /// 数据源ID(GUID)
  449. /// </summary>
  450. /// <param name="dbId"></param>
  451. /// <returns></returns>
  452. public static string GetDataBaseConnectString(string dbId)
  453. {
  454. string retString = "";
  455. string sql = @"SELECT DBSourceName ,
  456. DBIpAddress ,
  457. DBName ,
  458. DBUser ,
  459. DBPwd
  460. FROM dbo.Sys_DataBase
  461. WHERE WorkCode = '{0}'
  462. AND ID = '{1}'";
  463. sql = string.Format(sql, AppConfig.WorkPointCode, dbId);
  464. DataTable data = DBHelper.ExecuteDataset(AppConfig.FrameConnectString, CommandType.Text, sql).Tables[0];
  465. if (data.Rows.Count == 0)
  466. {
  467. retString = "";
  468. }
  469. else
  470. {
  471. string foramtString = "Data Source={0};Database={1};Uid={2};Pwd={3};";
  472. string dbIpAddress = data.Rows[0]["DBIpAddress"].ToString();
  473. string dbName = data.Rows[0]["DBName"].ToString();
  474. string dbUser = data.Rows[0]["DBUser"].ToString();
  475. string dbPwd = data.Rows[0]["DBPwd"].ToString();
  476. retString = string.Format(foramtString, new object[] { dbIpAddress, dbName, dbUser, AppConfig.FromMd5(dbPwd) });
  477. }
  478. return retString;
  479. }
  480. #endregion
  481. #region 权限判断
  482. /// <summary>
  483. /// 窗体按钮权限
  484. /// </summary>
  485. /// <param name="menuCode">窗体TAG值</param>
  486. /// <param name="btnName">按钮名称</param>
  487. /// <returns>false or true</returns>
  488. public static bool GetUserExcuteRight(string menuCode, string btnName)
  489. {
  490. try
  491. {
  492. string sql = @"DECLARE @Count INT
  493. SET @Count = 0
  494. SELECT @Count = COUNT(*)
  495. FROM dbo.Sys_UserPower a WITH ( NOLOCK )
  496. LEFT JOIN dbo.Sys_MenuAction b ON a.ActionId = b.ID
  497. WHERE UserId = '{0}'
  498. AND b.WorkPointCode = '{1}'
  499. AND b.MenuCode = '{2}'
  500. AND b.BtnName = '{3}'
  501. DECLARE @Count1 INT
  502. SET @Count1 = 0
  503. SELECT @Count1 = COUNT(*)
  504. FROM dbo.Sys_RolePower a WITH ( NOLOCK )
  505. LEFT JOIN dbo.Sys_MenuAction b WITH ( NOLOCK ) ON a.ActionId = b.ID
  506. WHERE a.RoleId IN ( SELECT ISNULL(a.RoleId, 0)
  507. FROM dbo.Sys_UserRole a WITH ( NOLOCK )
  508. WHERE a.UserId = '{0}' )
  509. AND b.WorkPointCode = '{1}'
  510. AND b.MenuCode = '{2}'
  511. AND b.BtnName = '{3}'
  512. SELECT @Count + @Count1
  513. ";
  514. sql = string.Format(sql, new object[] { AppConfig.UserId, AppConfig.WorkPointCode, menuCode, btnName });
  515. object obj = DBHelper.ExecuteScalar(AppConfig.FrameConnectString, CommandType.Text, sql);
  516. bool flag = false;
  517. if (AppConfig.UserCode.ToLower() == "demo")
  518. {
  519. flag = true;
  520. }
  521. else
  522. {
  523. if (AppConfig.AdminFlag == true)
  524. {
  525. flag = true;
  526. }
  527. else
  528. {
  529. flag = (Convert.ToInt32(obj) > 0 ? true : false);
  530. }
  531. }
  532. return flag;
  533. }
  534. catch
  535. {
  536. return false;
  537. }
  538. }
  539. #endregion
  540. #region 获取结构ID&&数据源Id
  541. /// <summary>
  542. /// 返回窗体结构ID
  543. /// </summary>
  544. /// <param name="menuCode">tagcode</param>
  545. /// <returns></returns>
  546. public static string GetMenuId(string menuCode)
  547. {
  548. string sql = "SELECT ID FROM dbo.Sys_Menu WHERE WorkPointCode='{0}' AND MenuCode='{1}'";
  549. sql = string.Format(sql, AppConfig.WorkPointCode, menuCode);
  550. object obj = DBHelper.ExecuteScalar(AppConfig.FrameConnectString, CommandType.Text, sql);
  551. return obj == null ? "" : obj.ToString();
  552. }
  553. /// <summary>
  554. /// 获取数据源ID
  555. /// </summary>
  556. /// <param name="menuCode"></param>
  557. /// <param name="filterbtnNmae"></param>
  558. /// <returns></returns>
  559. public static string GetSourceId(string menuCode, string filterbtnNmae)
  560. {
  561. string sql = " SELECT ID FROM dbo.Sys_FormDataSource WHERE MenuId='{0}' AND FilterButtonName='{1}'";
  562. sql = string.Format(sql, GetMenuId(menuCode), filterbtnNmae);
  563. object obj = DBHelper.ExecuteScalar(AppConfig.FrameConnectString, CommandType.Text, sql);
  564. return obj == null ? "" : obj.ToString();
  565. }
  566. public static DataSet GetDataSourceDefaultDataSet(string menuCode, string filterbtnName)
  567. {
  568. string sql = "SELECT SysDataSourceFlag,DbId,SqlTxt FROM dbo.Sys_FormDataSource WHERE MenuId='{0}' AND FilterButtonName='{1}'";
  569. sql = string.Format(sql, GetMenuId(menuCode), filterbtnName);
  570. DataSet ds = null;
  571. DataRow dr = null;
  572. try
  573. {
  574. DataTable data = DBHelper.ExecuteDataset(AppConfig.FrameConnectString, CommandType.Text, sql).Tables[0];
  575. if (data.Rows.Count > 0)
  576. {
  577. dr = data.Rows[0];
  578. }
  579. if (dr != null)
  580. {
  581. bool sysFlag = Convert.ToBoolean(dr["SysDataSourceFlag"]);
  582. string dbid = dr["DbId"].ToString();
  583. string sqltxt = dr["SqlTxt"].ToString();
  584. string conStr = "";
  585. if (sysFlag == true)
  586. {
  587. conStr = AppConfig.AppConnectString;
  588. }
  589. else
  590. {
  591. conStr = GetDataBaseConnectString(dbid);
  592. }
  593. ds = DBHelper.ExecuteDataset(conStr, CommandType.Text, sqltxt);
  594. }
  595. }
  596. catch (Exception ex)
  597. {
  598. ds = null;
  599. }
  600. return ds;
  601. }
  602. #endregion
  603. #region EXCEL
  604. public static DataTable GetExcelData(string excelPath, string sheetName)
  605. {
  606. DataTable dtGBPatient = new DataTable();
  607. string strConn;
  608. //注意:把一个excel文件看做一个数据库,一个sheet看做一张表。语法 "SELECT * FROM [sheet1$]",表单要使用"[]"和"$"
  609. // 1、HDR表示要把第一行作为数据还是作为列名,作为数据用HDR=no,作为列名用HDR=yes;
  610. // 2、通过IMEX=1来把混合型作为文本型读取,避免null值。
  611. strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source={0};Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";
  612. string strConnection = string.Format(strConn, excelPath);
  613. OleDbConnection conn = new OleDbConnection(strConnection);
  614. conn.Open();
  615. OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + sheetName + "$]", strConnection);
  616. dtGBPatient.TableName = "gbPatientInfo";
  617. oada.Fill(dtGBPatient);//获得datatable
  618. conn.Close();
  619. return dtGBPatient;
  620. }
  621. //public static string[] GetExcelSheetNames(string filePath)
  622. //{
  623. //Microsoft.Office.Interop.Excel.ApplicationClass
  624. //excelApp
  625. //=
  626. //new
  627. //Microsoft.Office.Interop.Excel.ApplicationClass();
  628. //Microsoft.Office.Interop.Excel.Workbooks
  629. //wbs
  630. //=
  631. //excelApp.Workbooks;
  632. //Microsoft.Office.Interop.Excel.Workbook
  633. //wb
  634. //=
  635. //wbs.Open(filePath,
  636. //Type.Missing, Type.Missing, Type.Missing, Type.Missing,
  637. //Type.Missing,
  638. //Type.Missing,
  639. //Type.Missing,
  640. //Type.Missing,
  641. //Type.Missing, Type.Missing,
  642. //Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  643. //int count = wb.Worksheets.Count;
  644. //string[] names = new string[count];
  645. //for (int i = 1; i <= count; i++)
  646. //{
  647. // names[i - 1]
  648. // =
  649. // ((Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[i]).Name;
  650. //}
  651. //wb.Close(null, null, null);
  652. //excelApp.Quit();
  653. //wbs.Close();
  654. //System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
  655. //System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
  656. //System.Runtime.InteropServices.Marshal.ReleaseComObject(wbs);
  657. //excelApp = null;
  658. //wbs = null;
  659. //wb = null;
  660. //return names;
  661. //}
  662. #endregion
  663. #region 获取单据流水号
  664. /// <summary>
  665. /// 获取单据号
  666. /// </summary>
  667. /// <param name="tbName"></param>
  668. /// <param name="colName"></param>
  669. /// <param name="Pre"></param>
  670. /// <param name="numLen"></param>
  671. /// <returns></returns>
  672. public static string GetBillCode(string tbName, string colName, string Pre, int numLen)
  673. {
  674. string sql = "EXEC Addins_GetBillCode '{0}','{1}','{2}',{3}";
  675. sql = string.Format(sql, new object[] { tbName, colName, Pre, numLen });
  676. return DBHelper.ExecuteScalar(AppConfig.AppConnectString, CommandType.Text, sql).ToString();
  677. }
  678. /// <summary>
  679. /// 获取单据号
  680. /// </summary>
  681. /// <param name="tbName"></param>
  682. /// <param name="colName"></param>
  683. /// <param name="Pre"></param>
  684. /// <param name="numLen"></param>
  685. /// <returns></returns>
  686. public static string GetSerialCode(string connectString, string tbName, string colName, string Pre, int numLen)
  687. {
  688. string sql = "EXEC Addins_GetSerialCode '{0}','{1}','{2}',{3}";
  689. sql = string.Format(sql, new object[] { tbName, colName, Pre, numLen });
  690. return DBHelper.ExecuteScalar(connectString, CommandType.Text, sql).ToString();
  691. }
  692. #endregion
  693. #region 获取物料批次
  694. /// <summary>
  695. ///
  696. /// </summary>
  697. /// <param name="invCode"></param>
  698. /// <returns></returns>
  699. public static string GetMaxNum(string invCode)
  700. {
  701. string sql = @"DECLARE @Num INT
  702. SET @Num = 0
  703. SELECT @Num = ISNULL(MAX(MAXNUM), 0)
  704. FROM dbo.TBLBARCODEBATCH
  705. WHERE DATEINDEX = '{0}'
  706. AND InvCode = '{1}'
  707. IF @Num = 0
  708. BEGIN
  709. INSERT INTO dbo.TBLBARCODEBATCH
  710. ( InvCode, DateIndex, MaxNum )
  711. VALUES ( '{1}', '{0}', @Num + 1 )
  712. END
  713. ELSE
  714. BEGIN
  715. UPDATE dbo.TBLBARCODEBATCH SET maxnum=@Num+1 WHERE invcode='{1}' AND dateindex='{0}'
  716. END
  717. SELECT @Num+1";
  718. string dateIndex = AppConfig.GetSeverDateTime("yyyy-MM-dd HH:mm").ToString("yyyyMMddHHmm");
  719. sql = string.Format(sql, dateIndex, invCode);
  720. object obj = DBHelper.ExecuteScalar(AppConfig.AppConnectString, CommandType.Text, sql);
  721. return dateIndex + Convert.ToInt32(obj).ToString("0000");
  722. }
  723. #endregion
  724. #region 调用WEBSERVICE
  725. /**/
  726. /// 根据指定的信息,调用远程WebService方法
  727. ///
  728. /// WebService的http形式的地址
  729. /// 欲调用的WebService的命名空间
  730. /// 欲调用的WebService的类名(不包括命名空间前缀)
  731. /// 欲调用的WebService的方法名
  732. /// 参数列表
  733. /// WebService的执行结果
  734. ///
  735. /// 如果调用失败,将会抛出Exception。请调用的时候,适当截获异常。
  736. /// 异常信息可能会发生在两个地方:
  737. /// 1、动态构造WebService的时候,CompileAssembly失败。
  738. /// 2、WebService本身执行失败。
  739. ///
  740. ///
  741. ///
  742. /// object obj = InvokeWebservice("http://localhost/GSP_WorkflowWebservice/common.asmx","Genersoft.Platform.Service.Workflow","Common","GetToolType",new object[]{"1"});
  743. ///
  744. ///
  745. //public static object InvokeWebservice(string url, string @namespace, string classname, string methodname, object[] args)
  746. //{
  747. //try
  748. //{
  749. // System.Net.WebClient wc = new System.Net.WebClient();
  750. // System.IO.Stream stream = wc.OpenRead(url + "?WSDL");
  751. // System.Web.Services.Description.ServiceDescription sd = System.Web.Services.Description.ServiceDescription.Read(stream);
  752. // System.Web.Services.Description.ServiceDescriptionImporter sdi = new System.Web.Services.Description.ServiceDescriptionImporter();
  753. // sdi.AddServiceDescription(sd, "", "");
  754. // System.CodeDom.CodeNamespace cn = new System.CodeDom.CodeNamespace(@namespace);
  755. // System.CodeDom.CodeCompileUnit ccu = new System.CodeDom.CodeCompileUnit();
  756. // ccu.Namespaces.Add(cn);
  757. // sdi.Import(cn, ccu);
  758. // Microsoft.CSharp.CSharpCodeProvider csc = new Microsoft.CSharp.CSharpCodeProvider();
  759. // System.CodeDom.Compiler.ICodeCompiler icc = csc.CreateCompiler();
  760. // System.CodeDom.Compiler.CompilerParameters cplist = new System.CodeDom.Compiler.CompilerParameters();
  761. // cplist.GenerateExecutable = false;
  762. // cplist.GenerateInMemory = true;
  763. // cplist.ReferencedAssemblies.Add("System.dll");
  764. // cplist.ReferencedAssemblies.Add("System.XML.dll");
  765. // cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
  766. // cplist.ReferencedAssemblies.Add("System.Data.dll");
  767. // System.CodeDom.Compiler.CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
  768. // if (true == cr.Errors.HasErrors)
  769. // {
  770. // System.Text.StringBuilder sb = new System.Text.StringBuilder();
  771. // foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
  772. // {
  773. // sb.Append(ce.ToString());
  774. // sb.Append(System.Environment.NewLine);
  775. // }
  776. // throw new Exception(sb.ToString());
  777. // }
  778. // System.Reflection.Assembly assembly = cr.CompiledAssembly;
  779. // Type t = assembly.GetType(@namespace + "." + classname, true, true);
  780. // object obj = Activator.CreateInstance(t);
  781. // System.Reflection.MethodInfo mi = t.GetMethod(methodname);
  782. // return mi.Invoke(obj, args);
  783. //}
  784. //catch (Exception ex)
  785. //{
  786. // throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));
  787. //}
  788. //}
  789. #endregion
  790. #region 默认站点采集点
  791. public static string DefaultLine = "";
  792. public static string DefaultRes = "";
  793. #endregion
  794. #region 获取连接符中的数据库名称
  795. public static string GetDataBaseNameByConnectString(string connectString)
  796. {
  797. Dictionary<string, string> dictionary = connectString.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToDictionary(x => x.Split('=')[0], x => x.Split('=')[1]);
  798. string erpDataBase = dictionary["Database"];
  799. return erpDataBase;
  800. }
  801. #endregion
  802. #region 获取打印次数
  803. /// <summary>
  804. /// 返回打印次数
  805. /// </summary>
  806. /// <param name="printTableName">打印的表名称</param>
  807. /// <param name="printKey">打印KEY值</param>
  808. /// <returns></returns>
  809. public static int GetPrintNum(string printTableName, string printKey)
  810. {
  811. string sql = " SELECT dbo.ZHC_GetPrintCount('{0}','{1}','{2}')";
  812. sql = string.Format(sql, AppConfig.WorkPointCode, printTableName, printKey);
  813. object obj = DBHelper.ExecuteScalar(AppConfig.FrameConnectString, CommandType.Text, sql);
  814. return Convert.ToInt32(obj);
  815. }
  816. #endregion
  817. public static Dictionary<string, string> GetDataBaseNames()
  818. {
  819. Dictionary<string, string> retDic = new Dictionary<string, string>();
  820. string sql = @"SELECT '[DB.'+DBSourceName+']' AS [Key],DBSourceName,DBIpAddress,DBName,DBUser,DBPwd
  821. FROM dbo.Sys_DataBase";
  822. DataTable data = DBHelper.ExecuteDataset(AppConfig.FrameConnectString, CommandType.Text, sql).Tables[0];
  823. foreach (DataRow dr in data.Rows)
  824. {
  825. string key = dr["Key"].ToString();
  826. if (!retDic.ContainsKey(key))
  827. {
  828. retDic.Add(key, dr["DBName"].ToString());
  829. }
  830. }
  831. return retDic;
  832. }
  833. /// <summary>
  834. /// 比如您设置设置的数据源名为ERP 那么您的key 就是[DB.ERP]
  835. /// </summary>
  836. /// <param name="key"></param>
  837. /// <returns></returns>
  838. public static string GetDataBaseConnectStringByKey(string key)
  839. {
  840. Dictionary<string, string> DIC = GetDataBaseConnectString();
  841. if (DIC.ContainsKey(key))
  842. {
  843. return DIC[key];
  844. }
  845. else
  846. {
  847. return "";
  848. }
  849. }
  850. private static Dictionary<string, string> GetDataBaseConnectString()
  851. {
  852. Dictionary<string, string> retDic = new Dictionary<string, string>();
  853. string sql = @"SELECT '[DB.'+DBSourceName+']' AS [Key],DBSourceName,DBIpAddress,DBName,DBUser,DBPwd
  854. FROM dbo.Sys_DataBase where WorkCode='{0}'";
  855. sql = string.Format(sql, AppConfig.WorkPointCode);
  856. DataTable data = DBHelper.ExecuteDataset(AppConfig.FrameConnectString, CommandType.Text, sql).Tables[0];
  857. foreach (DataRow dr in data.Rows)
  858. {
  859. string key = dr["Key"].ToString();
  860. string foramtString = "Data Source={0};Database={1};Uid={2};Pwd={3};";
  861. if (!retDic.ContainsKey(key))
  862. {
  863. string pwd = dr["DBPwd"].ToString();
  864. pwd = FromMd5(pwd);
  865. string dsql = string.Format(foramtString, new object[] { dr["DBIpAddress"], dr["DBName"], dr["DBUser"], pwd });
  866. retDic.Add(key, dsql);
  867. }
  868. }
  869. return retDic;
  870. }
  871. public static DBLinkClass GetDataBaseClass(string key)
  872. {
  873. try
  874. {
  875. Dictionary<string, string> retDic = new Dictionary<string, string>();
  876. string sql = @"SELECT '[DB.'+DBSourceName+']' AS [Key],DBSourceName,DBIpAddress,DBName,DBUser,DBPwd
  877. FROM dbo.Sys_DataBase where WorkCode='{0}'";
  878. sql = string.Format(sql, AppConfig.WorkPointCode);
  879. DataTable data = DBHelper.ExecuteDataset(AppConfig.FrameConnectString, CommandType.Text, sql).Tables[0];
  880. foreach (DataRow dr in data.Rows)
  881. {
  882. if (key == dr["Key"].ToString())
  883. {
  884. DBLinkClass item = new DBLinkClass();
  885. string pwd = dr["DBPwd"].ToString();
  886. pwd = FromMd5(pwd);
  887. item.DBIpAddress = dr["DBIpAddress"].ToString();
  888. item.DBName = dr["DBName"].ToString();
  889. item.DBUser = dr["DBUser"].ToString();
  890. item.DBPwd = pwd;
  891. return item;
  892. }
  893. }
  894. return null;
  895. }
  896. catch (Exception ex)
  897. {
  898. return null;
  899. }
  900. }
  901. #region 分页
  902. public static DataTable GetPageDataByDb(string tablename, string pkey, int pagesize, int pageindex, int rowcount)
  903. {
  904. string sql = "EXEC Proc_GetPage '{0}','{1}','',{2},{3},'{1}',0,'*',{4}";
  905. sql = string.Format(sql, tablename, pkey, pagesize, pageindex, rowcount);
  906. DataTable data = DBHelper.ExecuteDataset(AppConfig.FrameConnectString, CommandType.Text, sql).Tables[0];
  907. return data;
  908. }
  909. public static void DropTemTable(string tempTableName)
  910. {
  911. if (!string.IsNullOrEmpty(tempTableName))
  912. {
  913. string dropSql = @"IF EXISTS ( SELECT *
  914. FROM dbo.SysObjects
  915. WHERE ID = OBJECT_ID(N'[" + tempTableName + "]') ) BEGIN drop table " + tempTableName + " END";
  916. DBHelper.ExecuteNonQuery(AppConfig.FrameConnectString, CommandType.Text, dropSql);
  917. }
  918. }
  919. /// <summary>
  920. /// 创建临时表
  921. /// </summary>
  922. /// <param name="data"></param>
  923. public static string InsertTempTable(DataTable data, string _tempTableName)
  924. {
  925. try
  926. {
  927. if (_tempTableName != "")
  928. {
  929. string dropSql = @"IF EXISTS ( SELECT *
  930. FROM dbo.SysObjects
  931. WHERE ID = OBJECT_ID(N'[" + _tempTableName + "]') ) BEGIN drop table " + _tempTableName + " END";
  932. DBHelper.ExecuteNonQuery(AppConfig.FrameConnectString, CommandType.Text, dropSql);
  933. }
  934. _tempTableName = "TEMP_" + AppConfig.GetSeverDateTime("yyyy-MM-dd HH:mm:ss").ToString("yyyyMMddHHmmss") + "_" + Guid.NewGuid().ToString().Replace("-", "");
  935. string sql = @"IF not EXISTS ( SELECT * FROM dbo.SysObjects WHERE ID = OBJECT_ID(N'[" + _tempTableName + "]') ) BEGIN CREATE TABLE " + _tempTableName + "(";
  936. foreach (DataColumn dc in data.Columns)
  937. {
  938. if (dc.ColumnName.ToLower() == "pagerowindex")
  939. {
  940. string filedCol = dc.ColumnName + " int IDENTITY(1,1) PRIMARY KEY,";
  941. sql += filedCol;
  942. }
  943. else
  944. {
  945. if (dc.DataType == typeof(int))
  946. {
  947. string filedCol = dc.ColumnName + " int,";
  948. sql += filedCol;
  949. }
  950. else if (dc.DataType == typeof(decimal))
  951. {
  952. string filedCol = dc.ColumnName + " decimal(18,6),";
  953. sql += filedCol;
  954. }
  955. else if (dc.DataType == typeof(double))
  956. {
  957. string filedCol = dc.ColumnName + " float,";
  958. sql += filedCol;
  959. }
  960. else if (dc.DataType == typeof(DateTime))
  961. {
  962. string filedCol = dc.ColumnName + " datetime,";
  963. sql += filedCol;
  964. }
  965. else if (dc.DataType == typeof(string))
  966. {
  967. string filedCol = dc.ColumnName + " nvarchar(2000),";
  968. sql += filedCol;
  969. }
  970. else if (dc.DataType == typeof(bool))
  971. {
  972. string filedCol = dc.ColumnName + " bit,";
  973. sql += filedCol;
  974. }
  975. else
  976. {
  977. string filedCol = dc.ColumnName + " nvarchar(2000),";
  978. sql += filedCol;
  979. }
  980. }
  981. }
  982. sql = sql.Substring(0, sql.Length - 1) + ") END";
  983. DBHelper.ExecuteNonQuery(AppConfig.FrameConnectString, CommandType.Text, sql);
  984. return _tempTableName;
  985. }
  986. catch (Exception ex)
  987. {
  988. throw ex;
  989. }
  990. }
  991. #endregion
  992. }
  993. public class DBLinkClass
  994. {
  995. public string DBIpAddress { get; set; }
  996. public string DBName { get; set; }
  997. public string DBUser { get; set; }
  998. public string DBPwd { get; set; }
  999. }
  1000. }
  1001. }