using System.Data;
using System.Data.SqlClient;
using System.Configuration; namespace WindowsFormsApp3
{
public class DbHelper
{
private static string connStr = ConfigurationManager.ConnectionStrings["Demo"].ToString(); /// <summary>
/// 根据Sql语句返回Table类型数据
/// </summary>
/// <param name="sql">查询Sql语句</param>
/// <returns>Table</returns>
/// <SqlParameter>SqlParameter</SqlParameter>
public static DataTable GetDataTable(string sql, SqlParameter[] sqlParameter = null)
{
DataSet ds = new DataSet();
using (SqlConnection sqlConn = new SqlConnection(connStr))
{
sqlConn.Open();
if (sqlConn.State == ConnectionState.Open)
{
using (SqlCommand cmd = new SqlCommand(sql, sqlConn))
{
if (sqlParameter != null && sqlParameter.Length > )
cmd.Parameters.AddRange(sqlParameter);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
}
}
}
return ds.Tables[];
} /// <summary>
/// 查询数据库是否存在数据
/// </summary>
/// <param name="sql">查询Sql语句</param>
/// <param name="sqlParameter">查询参数</param>
/// <returns>存在返回True,不存在返回False</returns>
public static bool DataIsHasRows(string sql, SqlParameter[] sqlParameter = null)
{
bool HasRows = false;
using (SqlConnection sqlConn = new SqlConnection(connStr))
{
sqlConn.Open();
if (sqlConn.State == ConnectionState.Open)
{
using (SqlCommand cmd = new SqlCommand(sql, sqlConn))
{
if (sqlParameter != null && sqlParameter.Length > )
cmd.Parameters.AddRange(sqlParameter);
using (SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.HasRows)
{
HasRows = true;
}
}
}
}
}
return HasRows;
}
/// <summary>
/// 执行Sql语句返回受影响行数
/// </summary>
/// <param name="sql">Sql语句</param>
/// <param name="sqlParameter">Sql参数</param>
/// <returns>返回受影响行数</returns>
public static int ExecNonQuery(string sql, SqlParameter[] sqlParameter =null)
{
int num = ;
try
{
using (SqlConnection sqlConn = new SqlConnection(connStr))
{
sqlConn.Open();
if (sqlConn.State == ConnectionState.Open)
{
using (SqlCommand cmd = new SqlCommand(sql, sqlConn))
{
if (sqlParameter != null && sqlParameter.Length > )
cmd.Parameters.AddRange(sqlParameter);
num = cmd.ExecuteNonQuery();
}
}
}
return num;
}
catch
{
return -;
} }
/// <summary>
/// 返回第一行第一列内容
/// </summary>
/// <param name="sql"></param>
/// <param name="sqlParameters"></param>
/// <returns></returns>
public static string ExecScalar(string sql, SqlParameter[] sqlParameter = null)
{
string value = "";
using (SqlConnection sqlConn = new SqlConnection(connStr))
{
sqlConn.Open();
if (sqlConn.State == ConnectionState.Open)
{
using (SqlCommand cmd = new SqlCommand(sql, sqlConn))
{
if (sqlParameter != null && sqlParameter.Length > )
cmd.Parameters.AddRange(sqlParameter);
value = cmd.ExecuteScalar().ToString();
}
}
}
return value;
}
}
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<connectionStrings>
<add name="Demo" connectionString="Data Source=127.0.0.1;Initial Catalog=AdventureWorks;Persist Security Info=True;User ID=sa;Password=Sa123"/>
</connectionStrings>
</configuration>

自己整理的的数据操作DbHelper的更多相关文章

  1. SQL不同服务器数据库之间的数据操作整理(完整版)

    ---------------------------------------------------------------------------------- -- Author : htl25 ...

  2. 我的DbHelper数据操作类

    其实,微软的企业库中有一个非常不错的数据操作类了.但是,不少公司(起码我遇到的几个...),对一些"封装"了些什么的东西不太敢用,虽然我推荐过微软的企业库框架了...但是还是要&q ...

  3. DbHelper数据操作类

    摘要:本文介绍一下DbHelper数据操作类 微软的企业库中有一个非常不错的数据操作类.但是,不少公司(起码我遇到的几个...),对一些"封装"了些什么的东西不太敢用,虽然我推荐过 ...

  4. 转载-SQL不同服务器数据库之间的数据操作整理(完整版) .

    ---------------------------------------------------------------------------------- -- Author : htl25 ...

  5. 我的DbHelper数据操作类(转)

    其实,微软的企业库中有一个非常不错的数据操作类了.但是,不少公司(起码我遇到的几个...),对一些"封装"了些什么的东西不太敢用,虽然我推荐过微软的企业库框架了...但是还是要&q ...

  6. C#调用C++的DLL搜集整理的所有数据类型转换方式

    //C#调用C++的DLL搜集整理的所有数据类型转换方式,可能会有重复或者多种方案,自己多测试 //c++:HANDLE(void *) ---- c#:System.IntPtr //c++:Byt ...

  7. SQLServer服务器数据库之间的数据操作(完整版)

    分类: 数据库开发技术 ---------------------------------------------------------------------------------- -- Au ...

  8. 【summary】JQuery 相关css、ajax、数据操作函数或方法

    总结一下JQuery常用的函数方法,更加系统的整理一下. JQuery遍历的一些函数: 函数 描述 .add() 将元素添加到匹配元素的集合中. .andSelf() 把堆栈中之前的元素集添加到当前集 ...

  9. Android 常用数据操作封装类案例

    1.DbHelper类 继承自SQLiteOpenHelper类,实现对数据库的基本操作 package com.example.utils; import android.content.Conte ...

随机推荐

  1. Windows10 64位 安装 Postgresql 数据库

    1,下载Postgresql 10.7 版本,下载地址   https://www.enterprisedb.com/downloads/postgres-postgresql-downloads 2 ...

  2. 客户端热更新框架之UI热更框架设计(下)

    上一篇笔者介绍了关于什么是热更新,为什么需要热更新的技术文章.本篇就专门针对UI框架的热更新功能实现部分展开讨论,讨论的重点是热更新如何与UI框架进行结合? 现在笔者把设计“UI热更新框架”的整体设计 ...

  3. Leetcode 69. Sqrt(x)及其扩展(有/无精度、二分法、牛顿法)详解

    Leetcode 69. Sqrt(x) Easy https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute an ...

  4. SpringBoot:SpringBoot整合JdbcTemplate

    个人其实偏向于使用类似于JdbcTemplate这种的框架,返回数据也习惯于接受Map/List形式,而不是转化成对象,一是前后台分离转成json方便,另外是返回数据格式,数据字段可以通过SQL控制, ...

  5. 记一次 vmware ESXI 升级

    旧服务器的esxi版本为 60(6765062),计划安装成为最新版 的为ESXI 60  (14513180),中间波折遇坑多次,现记录如下: 一.开启ESXI的SSH 访问权限(可以通过按F2进入 ...

  6. kafka producer serializer序列化(六)

    生产者需要将要发送的数据转换成字节数组才能通过网络发送给kafka,对于一些简单的数据,kafka自带了一些序列化工具, 如:StringSerializer Double Long Integer ...

  7. lua table 的操作(四)

    table在前面作过介绍,它是一种关联数组,这种关联指的是可以设置各类类型的key来存储值. 1.table 间的数据传递 -- 为 table a 并设置元素,然后将 a 赋值给 b,则 a 与 b ...

  8. HanLP分词工具中的ViterbiSegment分词流程

    本篇文章将重点讲解HanLP的ViterbiSegment分词器类,而不涉及感知机和条件随机场分词器,也不涉及基于字的分词器.因为这些分词器都不是我们在实践中常用的,而且ViterbiSegment也 ...

  9. 【青橙商城-管理后台开发】3. web模块搭建

    [青橙商城-管理后台开发]3. web模块搭建 1.创建qingcheng_web_manager模块 pom.xml <?xml version="1.0" encodin ...

  10. HTTP用户认证、追加协议以及相关技术简单学习

    1. 用户身份认证 BASIC认证(基本认证): DIGEST(摘要认证): SSL客户端认证: FormBase认证(表单认证)常用: session和cookie 2. 基于HTTP的追加协议 A ...