sqlserver数据库查询
帮助类
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApp2
{
public static class SqlServer
{
#region 样本
private static readonly string defaultConnectString = "Server=.;DataBase=zjJunHaoCarChain;uid=sa;pwd=123456";
//"data source=.;initial catalog=zjJunHaoCarChain;user id=sa;pwd=123456"; #endregion
#region 变量
private static SqlConnection _con = null;
public static string _constr = "Server=.;DataBase=zjJunHaoCarChain;uid=sa;pwd=123456";
//"data source=.;initial catalog=zjJunHaoCarChain;user id=sa;pwd=123456"; #endregion
#region 属性 public static string constr
{
get
{
if (_constr == null || _constr.Equals(String.Empty))
{
_constr = defaultConnectString;
}
return _constr;
}
set
{
_constr = value;
}
} /// <summary>
/// 获取或设置数据库连接对象
/// </summary>
public static SqlConnection Con
{
get
{ if (SqlServer._con == null)
{
SqlServer._con = new SqlConnection();
}
if (SqlServer._con.ConnectionString == null || SqlServer._con.ConnectionString.Equals(string.Empty))
{
SqlServer._con.ConnectionString = SqlServer.constr;
}
return SqlServer._con;
}
set
{
SqlServer._con = value;
}
}
#endregion /// <summary>
/// 获取数据表
/// </summary>
/// <param name="commandText">select命令</param>
/// <param name="param">参数表</param>
/// <returns></returns>
#region MyRegion
public static DataTable GetDataTable(string commandText, params SqlParameter[] param)
{
DataTable result = new DataTable();
try
{
using (SqlCommand cmd = new SqlCommand(commandText, SqlServer.Con))
{
if (param != null)
cmd.Parameters.AddRange(param);
try
{
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(result);
}
catch (Exception ex)
{
result = null;
}
}
}
finally
{
if (SqlServer.Con.State != ConnectionState.Closed)
{
SqlServer.Con.Close();
}
}
return result;
}
#endregion /// <summary>
/// 执行不查询的数据库操作
/// </summary>
/// <param name="commandText">Oracle语句或存储过程名</param>
/// <param name="commandType">Oracle命令类型</param>
/// <param name="param">Oracle命令参数数组</param>
/// <returns>受影响的行数</returns>
#region MyRegion
public static int ExecuteNonQuery(string commandText, CommandType commandType, params SqlParameter[] param)
{
int result = ;
try
{
using (SqlCommand cmd = new SqlCommand(commandText, SqlServer.Con))
{
try
{
//cmd.CommandType = commandType;
if (param != null)
{
cmd.Parameters.AddRange(param);
}
SqlServer.Con.Open();
result = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
result = -;
}
}
}
finally
{
if (SqlServer.Con.State != ConnectionState.Closed)
{
SqlServer.Con.Close();
}
}
return result;
}
#endregion }
}
调用
/// <summary>
/// SqlServer数据库增删改查
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();//DataTable接收传回的数据
dt = SqlServer.GetDataTable("select * from Base_UserInfo;", null);//查询
// SqlServer.ExecuteNonQuery(@"INSERT INTO ZLCB01A (ZLCB01A005) VALUES('912')", CommandType.Text, null);//增加
//SqlServer.ExecuteNonQuery("delete from ZLCB01A WHERE ZLCB01A005='955'", CommandType.Text, null);//删除
//SqlServer.ExecuteNonQuery(@"update ZLCB01A SET ZLCB01A005='955' where ZLCB01A005='912'", CommandType.Text, null);//修改
}
sqlserver数据库查询的更多相关文章
- SQLServer数据库查询语法
SQLServer数据库查询语法 前言: SQLServer数据库介绍: SQLServer数据库是微软公司推出的一款关系型数据库系统,SQL Server是一个可扩展的.高性能的.为分布式客户机/服 ...
- 基于Winform框架DataGridView控件的SqlServer数据库查询展示功能的实现
关键词:Winform.DataGridView.SqlServer 一个基于winform框架的C/S软件,主要实现对SqlServer数据库数据表的实时查询. 一.为DataGridView添加数 ...
- sqlserver数据库查询,在数据类型不一致时容易出错
1. 如此句sql: select SysNo from User_MainInfo where Ouid=@Ouid 在 User_MainInfo表中Ouid是nvarchar类型,但当我们传入的 ...
- sqlserver数据库查询语句
--数据库所有表select * from sysobjects where type='u'; --指定表的所有列select name from syscolumns where id=(sele ...
- Sqlserver数据库分页查询
Sqlserver数据库分页查询一直是Sqlserver的短板,闲来无事,想出几种方法,假设有表ARTICLE,字段ID.YEAR...(其他省略),数据53210条(客户真实数据,量不大),分页查询 ...
- 查询Sqlserver数据库死锁的一个存储过程(转)
使用sqlserver作为数据库的应用系统,都避免不了有时候会产生死锁, 死锁出现以后,维护人员或者开发人员大多只会通过sp_who来查找死锁的进程,然后用sp_kill杀掉.利用sp_who ...
- SQL多表连接查询以及mysql数据库、sqlserver数据库常见不同点
mysql数据库表及数据准备语句: USE test; DROP TABLE IF EXISTS `teacher_table`; DROP TABLE IF EXISTS `student_tabl ...
- Python查询SQLserver数据库备份(抛砖引玉)
通过python pymssql直接访问SQLserver数据库,查找其数据库mode,这个脚本具有很强的抛砖引玉特性: 1.可以巡检多台多数据库服务器 2.query内容可以多样化,譬如查询死锁.连 ...
- C#通过SqlConnection连接查询更新等操作Sqlserver数据库
Sqlserver数据库连接方式有多种,这里只介绍最常用的通过SqlConnection和Sqlserver数据库用户名和密码验证来进行操作数据库. 数据库连接字符串: string connStri ...
随机推荐
- JavaScript 闭包&基于闭包实现柯里化和bind
闭包: 1 函数内声明了一个函数,并且将这个函数内部的函数返回到全局 2 将这个返回到全局的函数内中的函数存储到全局变量中 3 内部的函数调用了外部函数中的局部变量 闭包简述: 有权访问另一个函数局部 ...
- Vue——解决移动端键盘弹起导致的页面fixed定位元素布局错乱
最近做了一个移动端项目,页面主体是由form表单和底部fixed定位的按钮组成,当用户进行表单输入时,手机软键盘弹起,此时页面的尺寸发生变化,底部fixed定位的元素自然也会上移,可能就会覆盖页面中的 ...
- PB specified database is invalid
拷贝资料库到其他机器,可以重新配置ODBC ,如果还是报错,建议删除log .
- Educational Codeforces Round 81 + Gym 102267
UPD:变色了!!!历史最高1618~ Educational Codeforces Round 81 (Rated for Div. 2) The 2019 University of Jordan ...
- Ubuntu将Python3软连接到Python
sudo ln -s /usr/bin/python3 /usr/bin/python
- kali安装vm tools正确操作
参考博文:https://blog.csdn.net/qq_39536876/article/details/79501471 前言:每次在执行完 ./vmware-install.pl 重启后,总是 ...
- 【剑指Offer面试编程题】题目1355:扑克牌顺子--九度OJ
题目描述: LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他 ...
- Elasticsearch 如何使用RESTful API
所有其他语言可以使用 RESTful API 通过端口 9200 和 Elasticsearch 进行通信,你可以用你最喜爱的 web 客户端访问 Elasticsearch .事实上,正如你所看到的 ...
- java记录3--抽象
1.由来 利用抽象类是i为了更好的对类加以分类,例如各种植物有具体名字,也有“植物”这个抽象的词对所有具体植物进行归类. 2.抽象类通常用来作为一个类族的最顶层的父类(表示该类族所有事物的共性), 用 ...
- Linux centosVMware shell中的函数、shell中的数组、
一.shell中的函数 函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这个小单元的名字即可. 格式: function _name() { command ...