using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient; namespace WindowsFormsApplication1.DAL
{
/**
* 实现对SqlServer的基本操作
* 1.SqlConnection对象连接数据库;
* 2.SqlCommand对象,负责SQL语句的执行和存储过程的调用,并获取三种返回模式
* 3.执行存储过程,和带参数的存储过程
* 4.执行带参数的sql语句
*/
public class SqlServerHelp
{
//写在配置中:string ConnectionString = ConfigurationManager.ConnectionStrings["MyConnection"].ToString();
//配置中的写法
//<connectionStrings>
// <add name="SqlConnection" providerName="System.Data.SqlClient" connectionString="server=.;uid=sa;pwd=accp;database=pubs"/>
//</connectionStrings>
//或
//<appSettings>
// <add name="SqlConnection" providerName="System.Data.SqlClient" connectionString="server=.;uid=sa;pwd=accp;database=pubs"/>
//</appSettings> //Sql 验证:string ConnectionString = "user id=sa;password=;initial catalog=northwind;datasource=localhost;connect Timeout=20";
//Windows 身份验证:连接字符串:Data Source =数据库程序实例名,Initial Catalog=数据库名, Integrated Security=True 表示系统模式登录,不需要用户名密码,有最高权限
static string ConnectionString = @"Data Source=DESKTOP-IF0RC4M\SQL2012;Initial Catalog=mySqlServer;Integrated Security=True";
//执行sql语句,返回一个值
public static string GetStr()
{
string val = "";
//传入连接字符串连接
SqlConnection con = new SqlConnection(ConnectionString);
try
{
con.Open();
//传入sql语句和连接类
SqlCommand com = new SqlCommand("SELECT * FROM dbo.Table_1", con);
//返回一个值(第一行第一列)
val = com.ExecuteScalar().ToString();
con.Close(); //必须关闭,可以用USING方式写
}
catch
{
con.Close();
}
return val;
}
//执行sql语句,返回多行值
public static DataTable GetRows()
{
DataTable dt = new DataTable();
//传入连接字符串连接
SqlConnection con = new SqlConnection(ConnectionString);
try
{
con.Open();
//传入sql语句和连接类
SqlCommand com = new SqlCommand("SELECT * FROM dbo.Table_1", con);
//返回一个值(第一行第一列)
SqlDataReader dr = com.ExecuteReader();
dt.Load(dr);//直接把结果放进TB表中
//while(dr.Read()) //返回ture表示能读取一行,表示读取全部行
//{
// val = dr.GetString(0);//GetString(0)获取第一个字段的值
//} //dr.Read();//执行读取一行,返回值是bool
//val = dr.GetString(0);
con.Close();
}
catch
{
con.Close();
}
return dt;
}
//执行sql语句,返回返回一张表
public static DataTable GetTable()
{
DataTable dt = new DataTable();
//传入连接字符串连接
SqlConnection con = new SqlConnection(ConnectionString);
try
{
con.Open();
//传入sql语句和连接类
//SqlCommand com = new SqlCommand("SELECT * FROM dbo.Table_1", con);
//SqlDataAdapter sda = new SqlDataAdapter(com);
//另一种写法
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM dbo.Table_1", con);
sda.Fill(dt);
con.Close();
}
catch
{
con.Close();
}
return dt;
}
//带参数的sql语句
public static string SqlStr_parameters()
{
string val = "";
//传入连接字符串连接
SqlConnection con = new SqlConnection(ConnectionString);
try
{
con.Open();
//传入sql语句和连接类
SqlCommand com = new SqlCommand("SELECT * FROM dbo.Table_1 WHERE a=@a", con); //特别注意的是,如果参数是字符串,在这里不能加'',
//写法一
//SqlParameter sp = new SqlParameter("@a",SqlDbType.Char,10); //创建参数实例
//sp.Value = "1";
//com.Parameters.Add(sp); //写法一
//SqlParameter sp = new SqlParameter("@a",SqlDbType.Char,10){Value = "1"}; //创建参数实例
//com.Parameters.Add(sp); //写法三(推荐)
SqlParameter sp = new SqlParameter("@a", "");
sp.Value = "";
com.Parameters.Add(sp); //如果参数多的时候可以写成数组的形式
//SqlParameter[] sp1 =new SqlParameter[]{ new SqlParameter("@a", "1") };
//com.Parameters.AddRange(sp1); //返回一个值(第一行第一列)
val = com.ExecuteScalar().ToString();
con.Close(); //必须关闭,可以用USING方式写
}
catch(Exception ea)
{
con.Close();
}
return val;
}
//带参数的存储过程
public static void Sql_procedure()
{
string val = "";
//传入连接字符串连接
SqlConnection con = new SqlConnection(ConnectionString);
try
{
/*存储过程
* ALTER PROC usp_execSqlStr
@a CHAR(10)
AS
BEGIN
SELECT * FROM dbo.Table_1 WHERE a=@a
END
*/
con.Open();
//传入sql语句和连接类
SqlCommand com = new SqlCommand("usp_execSqlStr", con); //特别注意的是,如果参数是字符串,在这里不能加'',
//com.CommandType = CommandType.Text; //表示sql语句
com.CommandType = CommandType.StoredProcedure ; //表示存储过程
SqlParameter sp = new SqlParameter("@a", "");
com.Parameters.Add(sp);
val = com.ExecuteScalar().ToString(); //存储过程的执行和一般的sql语句的执行是一样的
con.Close(); //必须关闭,可以用USING方式写
}
catch (Exception ea)
{
con.Close();
}
}
}
}

数据库帮助类 SqlServerHelp的更多相关文章

  1. 【知识必备】ezSQL,最好用的数据库操作类,让php操作sql更简单~

    最近用php做了点小东东,用上了ezSQL,感觉真的很ez,所以拿来跟大家分享一下~ ezSQL是一个非常好用的PHP数据库操作类.著名的开源博客WordPress的数据库操作就使用了ezSQL的My ...

  2. DataAccess通用数据库访问类,简单易用,功能强悍

    以下是我编写的DataAccess通用数据库访问类,简单易用,支持:内联式创建多个参数.支持多事务提交.支持参数复用.支持更换数据库类型,希望能帮到大家,若需支持查出来后转换成实体,可以自行扩展dat ...

  3. Android打造属于自己的数据库操作类。

    1.概述 开发Android的同学都知道sdk已经为我们提供了一个SQLiteOpenHelper类来创建和管理SQLite数据库,通过写一个子类去继承它,就可以方便的创建.管理数据库.但是当我们需要 ...

  4. 我也来写:数据库访问类DBHelper

    一.前言 相信许多人都百度过:“.net 数据库访问类”.然后就出来一大堆SqlHelper.我也用过这些SqlHelper,也自己写过,一堆静态方法,开始使用起来感觉很不错,它们也确实在很多时候可以 ...

  5. PHP 数据库操作类:ezSQL

    EZSQL类介绍: 下载地址:http://www.jb51.net/codes/26393.htmlezsql是一个小型的快速的数据库操作类,可以让你很容易地用PHP操作各种数据库( MySQL.o ...

  6. 通用数据库操作类,前端easyui-datagrid,form

    实现功能:     左端datagrid显示简略信息,右侧显示选中行详细信息,数据库增删改 (1)点击选中行,右侧显示详细信息,其中[新增].[修改].[删除]按钮可用,[保存]按钮禁用 (2)点击[ ...

  7. MySQL数据库工具类之——DataTable批量加入MySQL数据库(Net版)

    MySQL数据库工具类之——DataTable批量加入数据库(Net版),MySqlDbHelper通用类希望能对大家有用,代码如下: using MySql.Data.MySqlClient; us ...

  8. 我也来写:数据库访问类DBHelper(转)

    一.前言 相信许多人都百度过:“.net 数据库访问类”.然后就出来一大堆SqlHelper.我也用过这些SqlHelper,也自己写过,一堆静态方法,开始使用起来感觉很不错,它们也确实在很多时候可以 ...

  9. php : mysql数据库操作类演示

    设计目标: 1,该类一实例化,就可以自动连接上mysql数据库: 2,该类可以单独去设定要使用的连接编码(set names XXX) 3,该类可以单独去设定要使用的数据库(use XXX): 4,可 ...

随机推荐

  1. Beta阶段第1周/共2周 Scrum立会报告+燃尽图 03

    作业要求与 [https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284] 相同 版本控制:https://git.coding.net/li ...

  2. 【python】利用scipy进行层次聚类

    参考博客: https://joernhees.de/blog/2015/08/26/scipy-hierarchical-clustering-and-dendrogram-tutorial/ 层次 ...

  3. 每天一个linux命令(网络):【转载】route命令

    Linux系统的route命令用于显示和操作IP路由表(show / manipulate the IP routing table).要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或 ...

  4. HDU4261 Estimation

    题意 Estimation Time Limit: 40000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  5. streamsets redis destinations 使用

    测试集成了directory(excel) 以及redis && field splitter 组件 pipeline flow docker-compose 配置 redis 服务& ...

  6. Yarn import now uses package-lock.json

    转发自: https://yarnpkg.com/blog/2018/06/04/yarn-import-package-lock/?utm_source=tuicool&utm_medium ...

  7. ACdream区域赛指导赛之手速赛系列(2)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/DaiHaoC83E15/article/details/26187183        回到作案现场 ...

  8. const_cast

    函数原型: const_cast < type-id > ( expression ) 去掉const属性:const_cast<int*> (&num),常用,因为不 ...

  9. RK3288 红外遥控器增加自定义按键

    转载请注明出处:https://www.cnblogs.com/lialong1st/p/10071557.html CPU:RK3288 系统:Android 5.1 1.在 dts 中增加红外遥控 ...

  10. CentOS 7 安装Memcached服务

    Memcached 简介 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站 ...