ADO.NET实现对SQL Server数据库的增删改查示例
作者:天青涩,再等你~ 时间:2024-01-12 17:37:54
了解了上一篇的ADO.NET简介,我们就可以来对数据库进行增删改查等基本操作了!下面是每种操作的具体实现。
先在自定义类的头部定义好数据库连接对象和连接字符串:
string connectionString = "Data Source=SC-201607131829;Initial Catalog=Animal;Integrated Security=True";
SqlConnection conn;
1.数据库的查询操作,返回一个DataTable
public DataTable doSelect()
{
string sql = "select * from detial";
using (conn = new SqlConnection(connectionString))
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds); //填充DataSet
return ds.Tables[0];
}
}
2.数据库插入操作,返回布尔值
public bool doInsert(string name, string skin, string weight)
{
string sql = "insert into detial(name,skin,weight)values(@name,@skin,@weight)";
SqlParameter[] newAnimal = {
new SqlParameter("name",name),
new SqlParameter("skin",skin),
new SqlParameter("weight",skin)
};
using (conn = new SqlConnection(connectionString))
{
SqlCommand com = new SqlCommand(sql, conn);
try
{
if (newAnimal != null)
{
foreach (SqlParameter parameter in newAnimal)
{
com.Parameters.Add(parameter);
}
}
conn.Open();
int influence = com.ExecuteNonQuery();
if (influence > 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception exception)
{
return false;
}
}
}
3.数据库删除操作,返回布尔值
public bool doDelete(string name)
{
string sql = "delete from detial where name = @name";
SqlParameter[] deleteParameter = { new SqlParameter("name", name) };
using (conn = new SqlConnection(connectionString))
{
SqlCommand com = new SqlCommand(sql, conn);
try
{
if (deleteParameter != null)
{
foreach (SqlParameter parameter in deleteParameter)
{
com.Parameters.Add(parameter);
}
}
conn.Open();
int influence = com.ExecuteNonQuery();
if (influence > 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception exception)
{
return false;
}
}
}
4.数据库更新操作,返回布尔值
public bool doUpdate(string name , string skin) {
string sql = "update detial set skin = @skin where name = @name";
SqlParameter[] updateParameter = {
new SqlParameter("name",name),
new SqlParameter("skin",skin)
};
using (conn = new SqlConnection(connectionString)) {
SqlCommand com = new SqlCommand(sql,conn);
try {
if (updateParameter != null) {
foreach(SqlParameter parameter in updateParameter){
com.Parameters.Add(parameter);
}
}
conn.Open();
int influence = com.ExecuteNonQuery();
if (influence > 0)
{
return true;
}
else
{
return false;
}
}catch(Exception exception){
return false;
}
}
}
其中为了防止sql注入,用到了SqlParameter类。
来源:http://www.cnblogs.com/SunshineAgain/p/5721632.html
标签:ado.net,增删改查
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
javascript设计模式 – 简单工厂模式原理与应用实例分析
2023-08-27 19:53:20
Python虚拟环境的创建和使用详解
2022-09-14 18:05:26
![](https://img.aspxhome.com/file/2023/5/67395_0s.jpg)
Python 的矩阵传播机制Broadcasting和矩阵运算
2021-07-28 01:03:34
![](https://img.aspxhome.com/file/2023/6/97186_0s.png)
laravel 实现阿里云oss文件上传功能的示例
2023-06-13 20:39:26
![](https://img.aspxhome.com/file/2023/2/55532_0s.jpg)
Python字符串的全排列算法实例详解
2023-04-30 17:01:05
Oracle 日期的一些简单使用
2009-08-05 20:42:00
MySQL中超级有用的14个小知识总结
2024-01-26 09:01:10
![](https://img.aspxhome.com/file/2023/3/99343_0s.png)
你需要知道的CSS3 动画技术[译]
2009-12-30 17:02:00
![](https://img.aspxhome.com/file/UploadPic/20101/11/transform_rotate-78s.png)
django框架使用orm实现批量更新数据的方法
2023-08-11 04:48:25
关于torch中tensor数据类型的转换
2022-07-16 20:59:59
![](https://img.aspxhome.com/file/2023/0/127150_0s.png)
js中的replace方法使用介绍
2024-04-10 13:54:58
asp如何检测HTTP表头信息?
2010-06-28 18:20:00
Python运行不显示DOS窗口的解决方法
2021-01-18 08:06:16
利用python汇总统计多张Excel
2023-12-31 14:22:11
![](https://img.aspxhome.com/file/2023/0/77690_0s.jpg)
Python全局变量用法实例分析
2023-08-03 13:39:18
js 数值项目的格式化函数代码
2023-07-14 16:51:31
用Python编写简单的微博爬虫
2022-10-07 04:58:34
![](https://img.aspxhome.com/file/2023/8/133608_0s.png)
php 图片上添加透明度渐变的效果
2023-10-23 09:00:02
PyCharm最新激活码(2020/10/27全网最新)
2023-10-01 02:38:19
![](https://img.aspxhome.com/file/2023/9/120909_0s.png)
谈谈Python中的while循环语句
2023-08-02 19:07:56
![](https://img.aspxhome.com/file/2023/8/107518_0s.jpg)