[C#] SQLHelper →→→→→进入此内容的聊天室

来自 , 2020-12-14, 写在 C#, 查看 132 次.
URL http://www.code666.cn/view/b5baa9c2
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data.OleDb;
  5. using System.Data;
  6.  
  7. namespace Sale_Comp_KR_Report_Mapping
  8. {
  9.     class ExcelBase
  10.     {
  11.         /// <summary>
  12.         /// 连接Excel数据库
  13.         /// </summary>
  14.         /// <param name="Path">文件路径</param>
  15.         /// <returns>返回连接对象</returns>
  16.         public static OleDbConnection ConnExcel(string Path)
  17.         {
  18.             try
  19.             {
  20.                 //HDR=Yes,这代表第一行是标题,不做为数据使用
  21.                                 //HDR=NO,则表示第一行不是标题,做为数据来使用。系统默认的是YES。
  22.                                
  23.                                 //当 IMEX=0 时为“汇出模式”,这个模式开启的 Excel 档案只能用来做“写入”用途。
  24.                                 //当 IMEX=1 时为“汇入模式”,这个模式开启的 Excel 档案只能用来做“读取”用途。
  25.                                 //当 IMEX=2 时为“连結模式”,这个模式开启的 Excel 档案可同时支援“读取”与“写入”
  26.            
  27.                 //string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Path + ";Extended Properties =\"Excel 12.0;\""; //IMEX=1;  
  28.                 // string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Path + ";Extended Properties =\"Excel 12.0;HDR=YES;IMEX=2;\""; //IMEX=1;                
  29.                 string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Path + ";Extended Properties =\"Excel 12.0;\""; //IMEX=1;  
  30.                 OleDbConnection conn = new OleDbConnection(strConn);                
  31.                 conn.Open();
  32.                 return conn;
  33.             }
  34.             catch (Exception ex)
  35.             {
  36.                 return null;
  37.             }
  38.         }
  39.  
  40.         /// <summary>
  41.         /// 在Excel中查找数据
  42.         /// </summary>
  43.         /// <param name="strConn">连接字符串</param>
  44.         /// <param name="Sql">查询语句</param>
  45.         /// <returns>返回查询到的数据集</returns>
  46.         public static DataSet SelectExcel(OleDbConnection conn, string Sql)
  47.         {
  48.             try
  49.             {
  50.                 string strExcel = "";
  51.                 OleDbDataAdapter myCommand = null;
  52.                 DataSet ds = null;
  53.                 strExcel = Sql;
  54.                 myCommand = new OleDbDataAdapter(strExcel, conn);
  55.                 ds = new DataSet();
  56.                 myCommand.Fill(ds);
  57.                 return ds;
  58.             }
  59.             catch (Exception ex)
  60.             {
  61.                 return null;
  62.             }
  63.  
  64.         }
  65.  
  66.         /// <summary>
  67.         /// 更新Excel文档
  68.         /// </summary>
  69.         /// <param name="Path">文件名称</param>
  70.         /// <returns>
  71.         /// -1:发生错误
  72.         /// other:更新的条数
  73.         /// </returns>
  74.         public static int UpdateExcel(OleDbConnection conn, string Sql)
  75.         {
  76.             try
  77.             {
  78.                 string strExcel = "";
  79.                 OleDbCommand myCommand = null;
  80.                 strExcel = Sql;
  81.                 myCommand = new OleDbCommand(strExcel, conn);
  82.  
  83.  
  84.                 int resultnum = myCommand.ExecuteNonQuery();
  85.                 return resultnum;
  86.             }
  87.             catch (Exception ex)
  88.             {
  89.                 return -1;
  90.             }
  91.         }
  92.  
  93.         /// <summary>
  94.         /// 关闭Excel数据库
  95.         /// </summary>
  96.         /// <param name="conn">数据库连接对象</param>
  97.         public static void CloseExcel(OleDbConnection conn)
  98.         {
  99.             conn.Close();
  100.         }
  101.  
  102.         /// <summary>
  103.         /// 读取Excel文档
  104.         /// </summary>
  105.         /// <param name="Path">文件名称</param>
  106.         /// <returns>返回一个数据集</returns>
  107.         public static DataSet ExcelToDS(string Path, string Sql)
  108.         {
  109.             try
  110.             {
  111.                 string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Path + ";Extended Properties =\"Excel 12.0;HDR=YES;IMEX=1;\""; //IMEX=1;                
  112.                 OleDbConnection conn = new OleDbConnection(strConn);
  113.                 conn.Open();
  114.                 string strExcel = "";
  115.                 OleDbDataAdapter myCommand = null;
  116.                 DataSet ds = null;
  117.                 strExcel = Sql;
  118.                 myCommand = new OleDbDataAdapter(strExcel, strConn);
  119.                 ds = new DataSet();
  120.                 myCommand.Fill(ds);
  121.                 conn.Close();
  122.                 return ds;
  123.             }
  124.             catch (Exception ex)
  125.             {
  126.                 return null;
  127.             }
  128.         }
  129.  
  130.         /// <summary>
  131.         /// 更新Excel文档
  132.         /// </summary>
  133.         /// <param name="Path">文件名称</param>
  134.         /// <returns>
  135.         /// -1:发生错误
  136.         /// other:更新的条数
  137.         /// </returns>
  138.         public static int UpdateToExcel(string Path, string Sql)
  139.         {
  140.             try
  141.             {
  142.                 string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Path + ";Extended Properties =\"Excel 12.0;HDR=FALSE;IMEX=1;\""; //IMEX=1;
  143.                 int resultnum = 0;
  144.                 OleDbConnection conn = new OleDbConnection(strConn);
  145.                 conn.Open();
  146.                 string strExcel = "";
  147.                 OleDbCommand myCommand = null;
  148.                 strExcel = Sql;
  149.                 myCommand = new OleDbCommand(strExcel, conn);
  150.                 resultnum = myCommand.ExecuteNonQuery();
  151.                 conn.Close();
  152.                 return resultnum;
  153.             }
  154.             catch (Exception ex)
  155.             {
  156.                 return -1;
  157.             }
  158.         }
  159.     }
  160. }
  161.  

回复 "SQLHelper"

这儿你可以回复上面这条便签

captcha