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

推荐订阅源

Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
Google Developers Blog
S
SegmentFault 最新的问题
Microsoft Security Blog
Microsoft Security Blog
J
Java Code Geeks
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
P
Proofpoint News Feed
博客园 - 【当耐特】
MongoDB | Blog
MongoDB | Blog
L
LangChain Blog
F
Fortinet All Blogs
C
Check Point Blog
博客园_首页
I
InfoQ
Jina AI
Jina AI
Blog — PlanetScale
Blog — PlanetScale
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
酷 壳 – CoolShell
酷 壳 – CoolShell
Engineering at Meta
Engineering at Meta
美团技术团队
Vercel News
Vercel News
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - 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=;