模板: windows->类库
引用: System.Configuration、System.Data、System.Data.SqlClient
分析: 增、删、改、查功能及对应的SQL语句
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
class SqlHelper
{
private static string strConn = ConfigurationManager.ConnectionStrings["DigitalProductShopConnectionString"].ConnectionString;
public static DataTable Query(string sql)
{
DataTable table = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(sql, strConn);
adapter.Fill(table);
return table;
}
public static int NonQuery(string sql)
{
int num = 0;
SqlConnection sqlConn = new SqlConnection(strConn);
SqlCommand sqlCmd = new SqlCommand(sql, sqlConn);
try
{
sqlConn.Open();
num = sqlCmd.ExecuteNonQuery();
}
finally
{
if (sqlConn.State == ConnectionState.Open)
sqlConn.Close();
}
return num;
}
}
}
查询功能: 调用Query方法,返回List或DataTable
删除功能: 调用NonQuery方法,返回int或bool
添加功能: 调用NonQuery方法,返回int或bool
修改功能:
1、查找对象→Select(主键):调用Query,返回对象或null。
2、更新属性→Update,调用NonQuery方法,返回int或bool
本文地址:https://blog.csdn.net/YuYuLingYan/article/details/107389387
如对本文有疑问, 点击进行留言回复!!
SQL注入--利用floor(rand(0)*2)报错返回信息
Oracle SQL nvl(),listagg() within group(),TO_CHAR(),CASE WHEN THEN ELSE END
SqlServer2008实例32全文索引和全文目录之从全文索引中去掉普通字符串
第1章 数据库系统与SQL基础知识!什么是SQL数据库系统?SQL类型?
sql学习-JOIN(INNER JOIN、LEFT JOIN、RIGHT JOIN、FULL OUTER JOIN)
网友评论