.NET 追寻足迹

C# 之路

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
下面有详细方法:
http://www.connectionstrings.com/default.aspx
导入的文件下必须有一个命名为"Schema.ini"的配置文件,如何配置,上面的网址里有详细说明.
如下示例:
string dir=Path.GetDirectoryName(filePath);
        
string filename=Path.GetFileName(filePath);
        
string conStr = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}\\;Extended Properties=\"text;HDR=Yes;FMT=Delimited(,)\";", dir);
        
using (OleDbConnection oledbCon = new OleDbConnection(conStr))
        
{
            OleDbDataReader reader
=null;
            OleDbCommand oledbCom 
= new OleDbCommand();
            oledbCom.Connection 
= oledbCon;
            oledbCom.CommandText 
= string.Format("select top 1 * from {0}", filename);

            
try
            
{
                oledbCon.Open();
                reader 
= oledbCom.ExecuteReader();            }

            
catch (SqlException e)
            
{
                
throw new Exception(e.Message);
            }

            
catch (Exception ex)
            
{
                
throw new Exception(ex.Message);
            }

            
finally
            
{
                
if (reader != null)
                
{
                    reader.Close();
                }

            }

        }
posted on 2008-03-01 09:40  绿水青山  阅读(282)  评论(0编辑  收藏  举报