C#数据库操作的三种经典用法

由于最近和数据库打交道,需要用C#和SQL Server 2005进行操作,就把近段时间内的最常用的操作做个总结。本人也是第一次用C#操作数据库,所以这三种典型用法对初学者还是挺有帮助的。

以下是我在visual studio 2005上写的一个类(连的是SQL Server 2005),已经过测试通过。里面有3个方法比较典型,源码如下:

 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Data;  
  5. using System.Data.SqlClient;  
  6. namespace DatabaseOperate  
  7. {  
  8. class SqlOperateInfo  
  9. {  
  10. //Suppose your ServerName is "aa",DatabaseName is "bb",UserName is "cc", Password is "dd"  
  11. private string sqlConnectionCommand = "Data Source=aa;Initial Catalog=bb;User ID=cc;Pwd=dd";  
  12. //This table contains two columns:KeywordID int not null,KeywordName varchar(100) not null  
  13. private string dataTableName = "Basic_Keyword_Test";  
  14. private string storedProcedureName = "Sp_InertToBasic_Keyword_Test";  
  15. private string sqlSelectCommand = "Select KeywordID, KeywordName From Basic_Keyword_Test";  
  16. //sqlUpdateCommand could contain "insert" , "delete" , "update" operate  
  17. private string sqlUpdateCommand = "Delete From Basic_Keyword_Test Where KeywordID = 1";  
  18. public void UseSqlReader()  
  19. {  
  20. SqlConnection sqlConnection = new SqlConnection(sqlConnectionCommand);  
  21. SqlCommand sqlCommand = new SqlCommand();  
  22. sqlCommand.CommandType = System.Data.CommandType.Text;  
  23. sqlCommand.Connection = sqlConnection;  
  24. sqlCommand.CommandText = sqlSelectCommand;  
  25. sqlConnection.Open();  
  26. SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();  
  27. while(sqlDataReader.Read())  
  28. {  
  29. //Get KeywordID and KeywordName , You can do anything you like. Here I just output them.  
  30. int keywordid = (int)sqlDataReader[0];  
  31. //the same as: int keywordid = (int)sqlDataReader["KeywordID"]  
  32. string keywordName = (string)sqlDataReader[1];  
  33. //the same as: string keywordName = (int)sqlDataReader["KeywordName"]  
  34. Console.WriteLine("KeywordID = " + keywordid + " , KeywordName = " + keywordName);  
  35. }  
  36. sqlDataReader.Close();  
  37. sqlCommand.Dispose();  
  38. sqlConnection.Close();  
  39. }  
  40. public void UseSqlStoredProcedure()  
  41. {  
  42. SqlConnection sqlConnection = new SqlConnection(sqlConnectionCommand);  
  43. SqlCommand sqlCommand = new SqlCommand();  
  44. sqlCommand.CommandType = CommandType.StoredProcedure;  
  45. sqlCommand.Connection = sqlConnection;  
  46. sqlCommand.CommandText = storedProcedureName;  
  47. sqlConnection.Open();  
  48. sqlCommand.ExecuteNonQuery();  
  49. //you can use reader here,too.as long as you modify the sp and let it like select * from ....  
  50. sqlCommand.Dispose();  
  51. sqlConnection.Close();  
  52. }  
  53. public void UseSqlDataSet()  
  54. {  
  55. SqlConnection sqlConnection = new SqlConnection(sqlConnectionCommand);  
  56. SqlCommand sqlCommand = new SqlCommand();  
  57. sqlCommand.CommandType = System.Data.CommandType.Text;  
  58. sqlCommand.Connection = sqlConnection;  
  59. sqlCommand.CommandText = sqlSelectCommand;  
  60. sqlConnection.Open();  
  61. SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();  
  62. sqlDataAdapter.SelectCommand = sqlCommand;  
  63. DataSet dataSet = new DataSet();  
  64. //sqlCommandBuilder is for update the dataset to database  
  65. SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);  
  66. sqlDataAdapter.Fill(dataSet, dataTableName);  
  67. //Do something to dataset then you can update it to  Database.Here I just add a row  
  68. DataRow row = dataSet.Tables[0].NewRow();  
  69. row[0] = 10000;  
  70. row[1] = "new row";  
  71. dataSet.Tables[0].Rows.Add(row);  
  72. sqlDataAdapter.Update(dataSet, dataTableName);  
  73. sqlCommand.Dispose();  
  74. sqlDataAdapter.Dispose();  
  75. sqlConnection.Close();  
  76. }  
  77. }  

以上的程序概括了最典型的用法,也是最基本的用法。

希望通过本文的介绍,能给你带来帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值