
C#数据库操作的三种经典用法
发布日期:2021-05-07 10:36:27
浏览次数:21
分类:精选文章
本文共 3104 字,大约阅读时间需要 10 分钟。
C#?SQL Server 2005??????????????
???????C#?SQL Server 2005?????????????????????????????????????????????? DataSet ????????
?????????
using System;
using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient;namespace DatabaseOperate
{ class SqlOperateInfo { // ??????? "aa"?????? "bb"????? "cc"???? "dd" private string sqlConnectionCommand = "Data Source=aa;Initial Catalog=bb;User ID=cc;Pwd=dd"; private string dataTableName = "Basic_Keyword_Test"; private string storedProcedureName = "Sp_InertToBasic_Keyword_Test"; private string sqlSelectCommand = "Select KeywordID, KeywordName From Basic_Keyword_Test"; 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 = CommandType.Text; sqlCommand.Connection = sqlConnection; sqlCommand.CommandText = sqlSelectCommand; sqlConnection.Open(); SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); while (sqlDataReader.Read()) { int keywordid = (int)sqlDataReader[0]; string keywordName = (string)sqlDataReader[1]; 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(); sqlCommand.Dispose(); sqlConnection.Close(); } public void UseSqlDataSet() { SqlConnection sqlConnection = new SqlConnection(sqlConnectionCommand); SqlCommand sqlCommand = new SqlCommand(); sqlCommand.CommandType = CommandType.Text; sqlCommand.Connection = sqlConnection; sqlCommand.CommandText = sqlSelectCommand; sqlConnection.Open(); SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); sqlDataAdapter.SelectCommand = sqlCommand; DataSet dataSet = new DataSet(); SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter); sqlDataAdapter.Fill(dataSet, dataTableName); // ????????????????? 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(); } }
}
???????C#?SQL Server 2005????????????????????????????????DataSet??????????????????????????????????????
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2025年05月03日 12时42分02秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Java学习第二章——Java基本语句
2019-03-15
遇到问题之-yum update无法连接镜像问题解决
2019-03-15
pycharm如何设置(错误、警告类的标准提醒)
2019-03-15
PHP是世界上最好的语言?Phython第一个不服
2019-03-15
Bugku CTF-web6
2019-03-15
UML-配置图
2019-03-15
python入门到秃顶(10):异常
2019-03-15
考研复试英语问答
2019-03-15
百度背景换肤案例
2019-03-15
输出对象的值——踩坑
2019-03-15
在苹果Mac上如何更改AirDrop名称?
2019-03-15
1110 Complete Binary Tree (25 point(s))
2019-03-15
541【毕设课设】基于单片机电阻电感电容RLC测量仪系统
2019-03-15
基于8086交通灯系统仿真设计(微机原理设计资料)
2019-03-15
解读域名管理之:域名注册机构介绍
2019-03-15
找中位数
2019-03-15
自动化测试——UI自动化测试的痛点
2019-03-15
如何将萌推商品主图、属性图、详情图批量保存到电脑的方法
2019-03-15
2021年N1叉车司机模拟考试及N1叉车司机考试软件
2019-03-15