C# 2005 访问数据库(一)

来源:互联网 发布:天正电气软件 编辑:程序博客网 时间:2024/05/20 22:31
      这几天在网上找C# 2005访问数据库的资料,可是没有找到特别实用、详细的。所以自己就只能一边看MSDN,一边问同事,还好顺利完成了。现在闲下来把这些东西整理了一下,以供大家参考。

       Microsoft. NET FrameWork数据库访问采用ADO.NET技术。ADO.NET提供两种内置的.NET数据提供者。一种用于OLE DB数据源,一种用于Microsoft SQL Server。通过OLE DB访问数据格式(Microsoft Access)、第三方数据库、非关系数据。(还有一种是ODBC .NET)

      ADO.NET中的基本类:

在C#中使用ADO.NET第一步要引用System.Data名称空间。将下面using指令放置在程序的开始:

using System.Data;

接下来要放置数据提供者,根据不同的数据源放置不同的数据提供者如下:

SQL Server.NET           -->     using System.Data.SqlClient;

OLE DB .NET                -->     using System.Data.OleDb;

ODBC .NET                   -->     using System.Data.ODBC;

下面使用Console Application模板分别以SQL Server、Access数据库为例进行数据库访问举例。

一、使用SQL Server 中的Norithwind数据库举例

using System;

using System.Data;                      //Use ADO.NET namespace

using System.Data.SqlClient;       //Use SQL Sever data provider namespace

class DataReaderSqlExample{

    public static void Main(){

        //Specify SQL Server-specific connection string

        SqlConnection thisConnection = new SqlConnection( @"Data Source=(local);"+

        "Integrated Security=SSPI;User ID=sa;Password=sa;Initial Catalog=northwind");

        // Open connection

        thisConnection.Open();

        // Create command for this connection

        SqlCommand thisCommand = thisConnection.CreateCommand();

        // Specify SQL query for this command

        thisCommand.CommandText = "SELECT CustomerID,CompanyName from Customers";

        // Execute DataReader for specified command

        SqlDataReader thisReader = thisCommand.ExecuteReader();

        // while there are rows to read

        While(thisReader.Read()){

        // Output ID and name columns;

        Console.WriteLine("/t{0}/t{1}",thisReader["CurtomerID"],thisReader["CompanyName"]);

        }

        // Close reader

        thisReader.Close();

        // Close connection

        thisConnection.Close();

    }

}

二、使用Access的nwind.mdb数据库举例

using System;

using System.Data;                      //Use ADO.NET namespace

using System.Data.OleDB;           //Use namespace for OLE DB .NET data provider

class DataReaderAccessExample{

    public static void Main(){

        // Create connection object for Microsoft Access OLEDB Provider;

        // note @ sign prefacing string literal so backslashes in path name;

        OleDbConnection thisConnection = new OleDbConnection(

        @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=C:/tmp/nwind.mdb;");

        // Open connection object

        thisConnection.Open();

        // Create SQL command object on this connection

        OleDbCommand thisCommand = thisConnection.CreateCommand();

        // Initialize SQL SELECT command to retrieve desired data

        thisCommand.CommandText = "SELECT CustomerID,CompanyName from Customers";

        // Execute DataReader for specified command

        OleDbDataReader thisReader = thisCommand.ExecuteReader();

        // while there are rows to read

        While(thisReader.Read()){

        // Output ID and name columns;

        Console.WriteLine("/t{0}/t{1}",thisReader["CurtomerID"],thisReader["CompanyName"]);

        }

        // Close reader

        thisReader.Close();

        // Close connection

        thisConnection.Close();

    }

}

本文仅在此处以Access为例,以后不在使用Access举例。

<注:>本文参考文献

《C#入门经典》清华大学出版社

原创粉丝点击