您的位置:首页 > 数据库

C#数据库操作的3种典型用法

2007-05-28 18:30 295 查看
由于最近和数据库打交道,需要用C#和SQL Server 2005进行操作,就把近段时间内的最常用的操作做个总结.本人也是第一次用C#操作数据库,所以这三种典型用法对初学者还是挺有帮助的.
[align=left] 以下是我在visual studio 2005上写的一个类(连的是SQL Server 2005),已经过测试通过.里面有3个方法比较典型,在此把源码贴出:[/align]
[align=left]using System;[/align]
[align=left]using System.Collections.Generic;[/align]
[align=left]using System.Text;[/align]
[align=left]using System.Data;[/align]
[align=left]using System.Data.SqlClient;[/align]
[align=left] [/align]
[align=left]namespace DatabaseOperate[/align]
[align=left]{[/align]
[align=left] class SqlOperateInfo[/align]
[align=left] {[/align]
[align=left] //Suppose your ServerName is "aa",DatabaseName is "bb",UserName is "cc", Password is "dd"[/align]
[align=left] private string sqlConnectionCommand = "Data Source=aa;Initial Catalog=bb;User ID=cc;Pwd=dd";[/align]
[align=left] //This table contains two columns:KeywordID int not null,KeywordName varchar(100) not null[/align]
[align=left] private string dataTableName = "Basic_Keyword_Test";[/align]
[align=left] [/align]
[align=left] private string storedProcedureName = "Sp_InertToBasic_Keyword_Test";[/align]
[align=left] private string sqlSelectCommand = "Select KeywordID, KeywordName From Basic_Keyword_Test";[/align]
[align=left] //sqlUpdateCommand could contain "insert" , "delete" , "update" operate[/align]
[align=left] private string sqlUpdateCommand = "Delete From Basic_Keyword_Test Where KeywordID = 1";[/align]
[align=left] [/align]
[align=left] public void UseSqlReader()[/align]
[align=left] {[/align]
[align=left] SqlConnection sqlConnection = new SqlConnection(sqlConnectionCommand);[/align]
[align=left] SqlCommand sqlCommand = new SqlCommand();[/align]
[align=left] sqlCommand.CommandType = System.Data.CommandType.Text;[/align]
[align=left] sqlCommand.Connection = sqlConnection;[/align]
[align=left] sqlCommand.CommandText = sqlSelectCommand;[/align]
[align=left] [/align]
[align=left] sqlConnection.Open();[/align]
[align=left] SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();[/align]
[align=left] while(sqlDataReader.Read())[/align]
[align=left] {[/align]
[align=left] //Get KeywordID and KeywordName , You can do anything you like. Here I just output them.[/align]
[align=left] int keywordid = (int)sqlDataReader[0]; [/align]
[align=left] //the same as: int keywordid = (int)sqlDataReader["KeywordID"][/align]
[align=left] string keywordName = (string)sqlDataReader[1]; [/align]
[align=left] //the same as: string keywordName = (int)sqlDataReader["KeywordName"][/align]
[align=left] Console.WriteLine("KeywordID = " + keywordid + " , KeywordName = " + keywordName);[/align]
[align=left] }[/align]
[align=left] [/align]
[align=left] sqlDataReader.Close();[/align]
[align=left] sqlCommand.Dispose();[/align]
[align=left] sqlConnection.Close();[/align]
[align=left] }[/align]
[align=left] public void UseSqlStoredProcedure()[/align]
[align=left] {[/align]
[align=left] SqlConnection sqlConnection = new SqlConnection(sqlConnectionCommand);[/align]
[align=left] SqlCommand sqlCommand = new SqlCommand();[/align]
[align=left] sqlCommand.CommandType = CommandType.StoredProcedure;[/align]
[align=left] sqlCommand.Connection = sqlConnection;[/align]
[align=left] sqlCommand.CommandText = storedProcedureName;[/align]
[align=left] [/align]
[align=left] sqlConnection.Open();[/align]
[align=left] sqlCommand.ExecuteNonQuery();[/align]
[align=left] //you can use reader here,too.as long as you modify the sp and let it like select * from ....[/align]
[align=left] [/align]
[align=left] sqlCommand.Dispose();[/align]
[align=left] sqlConnection.Close();[/align]
[align=left] }[/align]
[align=left] public void UseSqlDataSet()[/align]
[align=left] {[/align]
[align=left] SqlConnection sqlConnection = new SqlConnection(sqlConnectionCommand);[/align]
[align=left] SqlCommand sqlCommand = new SqlCommand();[/align]
[align=left] sqlCommand.CommandType = System.Data.CommandType.Text;[/align]
[align=left] sqlCommand.Connection = sqlConnection;[/align]
[align=left] sqlCommand.CommandText = sqlSelectCommand;[/align]
[align=left] [/align]
[align=left] sqlConnection.Open();[/align]
[align=left] SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();[/align]
[align=left] sqlDataAdapter.SelectCommand = sqlCommand;[/align]
[align=left] DataSet dataSet = new DataSet();[/align]
[align=left] //sqlCommandBuilder is for update the dataset to database[/align]
[align=left] SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);[/align]
[align=left] sqlDataAdapter.Fill(dataSet, dataTableName);[/align]
[align=left] [/align]
[align=left] //Do something to dataset then you can update it to Database.Here I just add a row[/align]
[align=left] DataRow row = dataSet.Tables[0].NewRow();[/align]
[align=left] row[0] = 10000;[/align]
[align=left] row[1] = "new row";[/align]
[align=left] dataSet.Tables[0].Rows.Add(row);[/align]
[align=left] [/align]
[align=left] sqlDataAdapter.Update(dataSet, dataTableName);[/align]
[align=left] [/align]
[align=left] sqlCommand.Dispose();[/align]
[align=left] sqlDataAdapter.Dispose();[/align]
[align=left] sqlConnection.Close();[/align]
[align=left] }[/align]
[align=left] }[/align]
[align=left]}[/align]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: