zoukankan      html  css  js  c++  java
  • connection string for Excel/Access 2010

    Excel 2010 连接字符串

    case 1: ConnectionString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'", ExcelPath); break;
    case 2: ConnectionString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=No;IMEX=1'", ExcelPath); break;

    Access 2010 连接字符串

    string conString = string.Format (@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0}; Persist Security Info=False;", accdbPath );

    Excel - 读取一个sheet到内存

            /// <summary>
            
    /// Read a Sheet in to memory as a Dataset
            
    /// </summary>
            
    /// <param name="sheet">Sheet Name</param>
            
    /// <param name="flagUseheader">if value is 1, the first row was read as the header; else if value is 2, the first row was read as not the header</param>
            
    /// <returns>ds</returns>
            public System.Data.DataSet GetDataSetFromExcel(string sheet, int flagUseheader)
            {
                if (flagUseheader != 1 && flagUseheader != 2)
                {
                    return null;//throw new ArgumentOutOfRangeException("HDR_INVALIDE");
                }

                if (!System.IO.File.Exists (ExcelPath ))
                {
                    return null;//throw new ArgumentNullException("EXCEL_PATH_NULL");
                }
                
                string ConnectionString = string.Empty;
                switch (flagUseheader)
                {
                    case 1: ConnectionString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'", ExcelPath); break;
                    case 2: ConnectionString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=No;IMEX=1'", ExcelPath); break;
                }

                DataSet ds = new DataSet();
                OleDbDataAdapter olDataAdapter = new OleDbDataAdapter(string.Format(@"select * from [{0}$]", sheet ), ConnectionString );
                try
                {
                    olDataAdapter.Fill(ds);
                }
                catch { ds = null; }
                finally { olDataAdapter.Dispose();}
                
                return ds;
            }

    Access - 更新Item 表里的列值

            /// <summary>
            
    /// update column
            
    /// </summary>
            
    /// <param name="item">custom class{ItemName, SectionID}</param>
            public void UpdateFunctionalGroupID(ClsItem item)
            {            
                string commandText = string.Format (@"update Item set FunctionalGroup='{0}' where ItemName='{1}'",item.SectionID ,item.ItemName );
                string conString = string.Format (@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0}; Persist Security Info=False;", accdbPath );
                OleDbConnection oledbconn=new OleDbConnection (conString );
                OleDbCommand oledbCmd = new OleDbCommand(commandText, oledbconn);
                oledbconn.Open();
                try
                {
                    int affectedCount = oledbCmd.ExecuteNonQuery();

                    oledbCmd.Dispose();
                    oledbconn.Close();
                    if (affectedCount == 0)
                        ALNotUpdated.Add(item.ItemName);
                }
                catch {
                    ALNotUpdated.Add(item.ItemName);
                }
            }
  • 相关阅读:
    MSSQL·阻止保存要求重新创建表的更改配置
    MSSQL·查询某数据库中所有表的记录数并排序
    异常处理·psftp·local unable to open
    MSSQL·Execution Timeout Expired. The timeout period elapsed prior to completion of the oper..
    MSSQL·ORDER BY 1 DESC是什么写法?
    MSSQL·大数据量历史数据清理的思路
    ubuntu清理wine卸载后的残余项目
    Learning the Vi Editor, 6th Edition O'Reilly Media
    做一粒不浮躁的好“种子”
    Qt Designer使用简易教程
  • 原文地址:https://www.cnblogs.com/qixue/p/2321674.html
Copyright © 2011-2022 走看看