C# 2005 访问数据库(一)
来源:互联网 发布:天正电气软件 编辑:程序博客网 时间:2024/05/20 22:31
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#入门经典》清华大学出版社
- C# 2005 访问数据库(一)
- C#与数据库访问技术总结(一)之体系结构
- C# 2005 访问数据库(二)
- C# 2005 访问数据库(三)
- C# 2005 数据库访问(四)
- C# 2005 数据库访问(五)
- C# 2005 数据库访问(六)
- C# 2005 数据库访问(七)
- C#访问数据库(SQL Server)
- C#访问数据库(SQL Server)
- C# 访问远程数据库(sql server)
- C# OleDBHelper(数据库访问公共接口)
- C#访问MySQL数据库(winform+EF)
- C#访问SQLite数据库
- C#访问SQLite数据库
- C#访问SQLite数据库
- C#访问数据库方法
- C#访问数据库类
- sqlservice2000的安装操作常见问题
- linux服务器管理相关问题(四)--proftpd配置
- PL/SQLDEVELOPER6对象比较功能说明(原创)
- 一个不错的可拖动层遮罩加ALPHA滤镜(点击弹出后背景变浅色,信息提示框效果)
- SQLselect语法
- C# 2005 访问数据库(一)
- PL/SQLDEVELOPER6会话管理(原创)
- SRM 398 DIV2 [250]
- C# 2005 访问数据库(二)
- PL/SQLDEVELOPER6.0.5发布了
- PL/SQLDEVELOPER相关资源网址
- 下月起苏州实施扩大儿童免疫规划
- MS查询优化方法
- 数据窗口中按字段的显示值排序