惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Last Week in AI
Last Week in AI
H
Help Net Security
博客园 - 叶小钗
V
Visual Studio Blog
阮一峰的网络日志
阮一峰的网络日志
博客园 - 三生石上(FineUI控件)
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Microsoft Azure Blog
Microsoft Azure Blog
G
Google Developers Blog
腾讯CDC
MongoDB | Blog
MongoDB | Blog
宝玉的分享
宝玉的分享
P
Proofpoint News Feed
GbyAI
GbyAI
Microsoft Security Blog
Microsoft Security Blog
A
About on SuperTechFans
博客园 - 司徒正美
人人都是产品经理
人人都是产品经理
T
The Blog of Author Tim Ferriss
Martin Fowler
Martin Fowler
月光博客
月光博客
博客园 - 聂微东
酷 壳 – CoolShell
酷 壳 – CoolShell
Vercel News
Vercel News

博客园 - Chris Chen

行业网站运作与赢利模式分析 草根传播引发营销新变革 面对淘宝 易趣在无奈中颓落 中国移动电子商务的三大问题 一种很干净的安装方式(一) 几个值得关注的PHP MVC Framework PHP FRAMEWORK——PRADO 介绍 - Chris Chen Cake PHP framework [转] 我的软件推广成功之路(转) MySQL使用和管理 HtmlControl与WebControl的区别与用途 尽量减少表单回送 使用trace方法和trace属性记录Page目录中网页的执行情况 使用StringBuilder类 掌握ADO.NET的十个热门技巧(一) 使用ADO.NET解锁 Microsoft Access数据(一) 掌握ADO.NET的十个热门技巧(二) 使用ADO.NET解锁 Microsoft Access数据(二) 用Visual C#来修改和删除数据库记录 Google宣布了中文名 - 谷歌
ADO.NET下各种DB的连接方法
Chris Chen · 2006-04-21 · via 博客园 - Chris Chen

1、连接Excel
    

      //后面的 Excel与8.0之间有一个空格
        string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("./test.xls") + ";Extended Properties=Excel 8.0;";
       
        OleDbConnection conn = new OleDbConnection(connectionString);
        conn.Open();
       
        OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", conn);   //[Shee1$]中的Sheet1是分页的名称
        OleDbDataAdapter myAdapter = new OleDbDataAdapter(cmd);
        myAdapter.SelectCommand = cmd;
        DataSet myDataSet = new DataSet();
        myAdapter.Fill(myDataSet,"test");

        grExcel.Caption = "Excel表格内容";

        //"test"是Excel文件名

        //grExcel是一个 Gridview组件
        grExcel.DataSource = myDataSet.Tables["test"].DefaultView;
        grExcel.DataBind();
        conn.Close();

2、连接Access

     string connectString = "Microsoft.Jet.OLEDB.4.0;Data Source=C:\My.mdb;User ID=;Password=;";

    //Use a string variable to hold the ConnectionString.
     string connectString = "Provider=Microsoft.Jet.OLEDB.4.0;"Data Source=C:\My.mdb;User ID=;Password=";
 
    //Create an OleDbConnection object, 
    //and then pass in the ConnectionString to the constructor.
    OleDbConnection cn = new OleDbConnection(connectString);
 
    //Open the connection.
    cn.Open();
 
    //Use a variable to hold the SQL statement.
     string selectString = "SELECT CustomerID, ContactName, Phone FROM Customers";

     //Create an OleDbCommand object.
     //Notice that this line passes in the SQL statement and the OleDbConnection object


     OleDbCommand cmd = new OleDbCommand(selectString,cn);
     //Send the CommandText to the connection, and then build an OleDbDataReader.
     //Note: The OleDbDataReader is forward-only.
     OleDbDataReader reader = cmd.ExecuteReader();

3、SQL SERVER链接

     You next create a connection string that points to a SQL Server database. A connection string has a set of semi-colon-separated attributes. Each .Net Data Provider connection string looks different, depending on the type of .NET Data Provider you need to use and which attributes are set for each different type of database system. For example, the connection string below is an example of what you use to connect to a local SQL Server.

    Data Source=(local);Initial Catalog=Northwind;User ID=sa;Password=;