C#入门:ADO.NET操作数据库详解与Access实例

需积分: 9 13 下载量 79 浏览量 更新于2024-07-31 收藏 1021KB PPT 举报
本篇文章详细介绍了如何使用C#语言操作数据库,特别是针对ADO.NET技术进行深入探讨。ADO.NET是Microsoft提供的一套用于.NET平台上的数据库访问组件,它为程序员提供了强大的数据访问能力,支持在编程模式下编写复杂的数据库访问程序,同时也支持通过数据访问向导进行可视化设计。 章节9.1主要聚焦于Access数据库的使用。Access是一种关系型数据库管理系统(DBMS),用于存储和管理结构化的数据。数据库以表格形式存在,由行和列构成,例如提到的信息中心专门人才基本情况表,包含了诸如编号、姓名、性别、出生日期、工资、是否党员等字段。 建立Access数据库的步骤包括: 1. 启动Microsoft Access,选择创建一个新的空Access数据库,指定保存位置和文件名。 2. 在新建的数据库中,可以创建新的表,如“基本信息表”,并设置字段如“bj10001”的信息,包括姓名、性别等个人基本信息。 文中列举了部分员工的基本信息,这些数据以表格形式存储在Access数据库中,每个员工的记录都对应数据库中的一个行,而字段则表示列。这些数据可以通过C#中的ADO.NET类库,如SqlConnection、SqlCommand、SqlDataReader等进行读取、插入、更新和删除操作。 在实际开发中,C#开发者可以使用连接字符串(Connection String)来连接数据库,执行SQL语句来执行各种数据库操作。例如,查询数据、执行存储过程、处理事务等。同时,C#也支持数据对象(Data Objects)模型,如DataSet和DataTable,它们可以方便地在内存中操作数据,便于数据的处理和展示。 总结来说,C#操作数据库的关键在于掌握ADO.NET API,理解数据库连接、数据访问和操作的基本原理,以及如何结合具体的数据库(如Access)特性进行高效编程。通过本文的学习,开发者能够熟练地在C#项目中集成和管理数据库,实现数据的持久化和业务逻辑的执行。
2008-05-30 上传
using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Data.SqlClient; namespace DatabaseOperate{ class SqlOperateInfo { //Suppose your ServerName is "aa",DatabaseName is "bb",UserName is "cc", Password is "dd" private string sqlConnectionCommand = "Data Source=aa;Initial Catalog=bb;User ID=cc;Pwd=dd"; //This table contains two columns:KeywordID int not null,KeywordName varchar(100) not null private string dataTableName = "Basic_Keyword_Test"; private string storedProcedureName = "Sp_InertToBasic_Keyword_Test"; private string sqlSelectCommand = "Select KeywordID, KeywordName From Basic_Keyword_Test"; //sqlUpdateCommand could contain "insert" , "delete" , "update" operate private string sqlUpdateCommand = "Delete From Basic_Keyword_Test Where KeywordID = 1"; public void UseSqlReader() { SqlConnection sqlConnection = new SqlConnection(sqlConnectionCommand); SqlCommand sqlCommand = new SqlCommand(); sqlCommand.CommandType = System.Data.CommandType.Text; sqlCommand.Connection = sqlConnection; sqlCommand.CommandText = sqlSelectCommand; sqlConnection.Open(); SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); while(sqlDataReader.Read()) { //Get KeywordID and KeywordName , You can do anything you like. Here I just output them. int keywordid = (int)sqlDataReader[0]; //the same as: int keywordid = (int)sqlDataReader["KeywordID"] string keywordName = (string)sqlDataReader[1]; //the same as: string keywordName = (int)sqlDataReader["KeywordName"] Console.WriteLine("KeywordID = " + keywordid + " , KeywordName = " + keywordName); } sqlDataReader.Close(); sqlCommand.Dispose(); sqlConnection.Close(); } public void UseSqlStoredProcedure() { SqlConnection sqlConnection = new SqlConnection(sqlConnectionCommand); SqlCommand sqlCommand = new SqlCommand(); sqlCommand.CommandType = CommandType.StoredProcedure; sqlCommand.Connection = sqlConnection; sqlCommand.CommandText = storedProcedureName; sqlConnection.Open(); sqlCommand.ExecuteNonQuery(); //you can use reader here,too.as long as you modify the sp and let it like select * from .... sqlCommand.Dispose(); sqlConnection.Close(); } public void UseSqlDataSet() { SqlConnection sqlConnection = new SqlConnection(sqlConnectionCommand); SqlCommand sqlCommand = new SqlCommand(); sqlCommand.CommandType = System.Data.CommandType.Text; sqlCommand.Connection = sqlConnection; sqlCommand.CommandText = sqlSelectCommand; sqlConnection.Open(); SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); sqlDataAdapter.SelectCommand = sqlCommand; DataSet dataSet = new DataSet(); //sqlCommandBuilder is for update the dataset to database SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter); sqlDataAdapter.Fill(dataSet, dataTableName); //Do something to dataset then you can update it to Database.Here I just add a row DataRow row = dataSet.Tables[0].NewRow(); row[0] = 10000; row[1] = "new row"; dataSet.Tables[0].Rows.Add(row); sqlDataAdapter.Update(dataSet, dataTableName); sqlCommand.Dispose(); sqlDataAdapter.Dispose(); sqlConnection.Close(); } }}