博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
connection string for Excel/Access 2010
阅读量:4608 次
发布时间:2019-06-09

本文共 2646 字,大约阅读时间需要 8 分钟。

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);
            }
        }

 

 

转载于:https://www.cnblogs.com/qixue/archive/2012/01/13/2321674.html

你可能感兴趣的文章
应用程序设计:图书管理系统模板(链表+文件)
查看>>
遗传算法学习--多目标优化中的遗传算法
查看>>
Git的安装和使用教程详解
查看>>
lsof命令详解
查看>>
常用模块,异常处理
查看>>
父窗口与子窗口之间的传值
查看>>
eclipse 找不到 tomcat 的解决方案
查看>>
HDU 1890--Robotic Sort(Splay Tree)
查看>>
connection string for Excel/Access 2010
查看>>
【转】【Python】Python中的__init__.py与模块导入(from import 找不到模块的问题)
查看>>
学习wavenet_vocoder之环境配置
查看>>
常用Maven命令
查看>>
Docker启动mysql的坑2
查看>>
j2ee爬坑行之二 servlet
查看>>
JAVA基础入门(JDK、eclipse下载安装)
查看>>
最基础的applet运用--在applet上画线
查看>>
布局大全
查看>>
eclipse中安装tomcat插件
查看>>
常见设计模式C++代码实现
查看>>
C++线程同步的四种方式(Windows)
查看>>