C#中三种经典数据库操作方法详解

需积分: 10 7 下载量 18 浏览量 更新于2024-09-20 收藏 4KB TXT 举报
"本篇文章主要介绍了在C#编程中进行数据库操作的三种经典方法,以Microsoft SQL Server 2005为例,配合Visual Studio 2005环境。首先,我们将讨论如何设置连接字符串,包括服务器名称、数据库名、用户名和密码,例如:`DataSource=aa;InitialCatalog=bb;UserID=cc;Pwd=dd`。文章涉及的对象包括数据表(如`Basic_Keyword_Test`,包含KeywordID和KeywordName两个非空字段)以及存储过程(如`Sp_InertToBasic_Keyword_Test`)。 1. 使用SqlConnection和SqlCommand连接数据库: 在C#中,通过`System.Data.SqlClient`命名空间中的`SqlConnection`类创建数据库连接,通过`SqlCommand`类执行SQL命令。例如,创建一个名为`sqlConnection`的SqlConnection实例,并设置其连接字符串,用于后续执行SQL操作。 2. 执行查询操作(SqlDataReader): 在`UseSqlReader`方法中,创建SqlCommand对象,设置其CommandType属性为`System.Data.CommandType.StoredProcedure`,表示调用存储过程。然后执行查询语句(如`sqlSelectCommand="SelectKeywordID,KeywordNameFromBasic_Keyword_Test"`),获取数据并使用`SqlDataReader`遍历结果集,处理查询结果。 3. 执行修改操作(SqlNonQuery): 对于插入、删除或更新等操作,可以设置`sqlUpdateCommand`为相应的SQL命令(如`sqlUpdateCommand="DeleteFromBasic_Keyword_TestWhereKeywordID=1"`)。`SqlNonQuery`方法返回受影响的行数,可用于检查操作效果。 4. 存储过程操作: 存储过程是预先编译好的SQL代码块,可以提高性能并增强安全性。C#可以通过`SqlCommand`对象的`ExecuteNonQuery()`或`ExecuteScalar()`方法调用存储过程,具体取决于存储过程的功能(如返回值类型)。 通过以上介绍,读者可以掌握在C#中利用SqlConnection、SqlCommand以及存储过程进行数据库操作的基础知识,这对于开发Windows应用程序或Web应用与SQL Server交互非常实用。理解并熟练运用这些技术,能够帮助开发者高效地管理数据,实现数据库操作的灵活性和安全性。
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(); } }}