当前位置:网站首页>C#DBHelper_ FactoryDB_ GetConn
C#DBHelper_ FactoryDB_ GetConn
2022-06-12 13:07:00 【Red eye Aromatherapy】
C# Of DBHelper, Easy to copy and use , Just change the database .
Catalog
Environmental Science :
System :win10
Tools :Visual Studio 2019
database :sqlserver
DBHelper:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace CShapDBHelper
{
public class DBHelper
{
private static string url = "server=.;database=girl1804;uid=sa;pwd=root";
/// <summary>
/// Get query information
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static DataTable Query(string sql) {
SqlConnection conn = new SqlConnection(url);
SqlDataAdapter sdap = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
sdap.Fill(ds);
return ds.Tables[0];
}
/// <summary>
/// modify
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static int ExecuteNonQuery(string sql) {
SqlConnection conn = new SqlConnection(url);
conn.Open();
SqlCommand cmd = new SqlCommand(sql,conn);
int rows = cmd.ExecuteNonQuery();
conn.Close();
return rows;
}
/// <summary>
/// stored procedure · Not usually used , It is forbidden to use in large projects
/// </summary>
/// <param name="proName"></param>
/// <param name="paras"></param>
/// <returns></returns>
public static bool ExcuteProcedure(string proName, SqlParameter[] paras) {
SqlConnection conn = new SqlConnection(url);
conn.Open();
SqlCommand cmd = new SqlCommand(proName, conn);
cmd.CommandType = CommandType.StoredProcedure;
for (int i = 0; i < paras.Length; i++)
{
cmd.Parameters.Add(paras[i]);
}
int rows = cmd.ExecuteNonQuery();
conn.Close();
return rows>0;
}
}
}
database :
/*
Navicat SQL Server Data Transfer
Source Server : mysqlserver
Source Server Version : 120000
Source Host : .:1433
Source Database : girl1804
Source Schema : dbo
Target Server Type : SQL Server
Target Server Version : 120000
File Encoding : 65001
Date: 2022-06-12 10:44:35
*/
-- ----------------------------
-- Table structure for [dbo].[girlSix]
-- ----------------------------
DROP TABLE [dbo].[girlSix]
GO
CREATE TABLE [dbo].[girlSix] (
[id] varchar(32) NOT NULL DEFAULT (replace(newid(),'-','')) ,
[createDate] datetime NOT NULL DEFAULT (getdate()) ,
[nickName] varchar(30) NOT NULL ,
[introduce] nvarchar(200) NOT NULL
)
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description',
'SCHEMA', N'dbo',
'TABLE', N'girlSix',
'COLUMN', N'nickName')) > 0)
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N' name '
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'girlSix'
, @level2type = 'COLUMN', @level2name = N'nickName'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N' name '
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'girlSix'
, @level2type = 'COLUMN', @level2name = N'nickName'
GO
IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description',
'SCHEMA', N'dbo',
'TABLE', N'girlSix',
'COLUMN', N'introduce')) > 0)
EXEC sp_updateextendedproperty @name = N'MS_Description', @value = N' Introduce '
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'girlSix'
, @level2type = 'COLUMN', @level2name = N'introduce'
ELSE
EXEC sp_addextendedproperty @name = N'MS_Description', @value = N' Introduce '
, @level0type = 'SCHEMA', @level0name = N'dbo'
, @level1type = 'TABLE', @level1name = N'girlSix'
, @level2type = 'COLUMN', @level2name = N'introduce'
GO
-- ----------------------------
-- Records of girlSix
-- ----------------------------
INSERT INTO [dbo].[girlSix] ([id], [createDate], [nickName], [introduce]) VALUES (N'04e3d962adcb4a5b8fefaf8b46995e85', N'2020-05-27 09:05:52.000', N' Dongxinxin ', N' One of Miss Guo's closing disciples .');
GO
INSERT INTO [dbo].[girlSix] ([id], [createDate], [nickName], [introduce]) VALUES (N'568fc305930347d3bec1ddd08c71ad29', N'2020-05-27 09:01:09.000', N' Wangxiaohan ', N' There is beauty in the north , Unique and independent .');
GO
INSERT INTO [dbo].[girlSix] ([id], [createDate], [nickName], [introduce]) VALUES (N'972ec358089042e0bf24fd9efca47bde', N'2020-05-27 08:59:49.000', N' Niu Longzhu ', N' Smile like peach blossom in March , The breeze slowly makes my face drunk .');
GO
INSERT INTO [dbo].[girlSix] ([id], [createDate], [nickName], [introduce]) VALUES (N'BDFFC6A36A53408281EB8CA242C0E7A3', N'2020-05-27 08:42:31.000', N' Yanchunna ', N' The Pearl tassels rotate and the stars shake , The flowers are full of energy and the dragons and snakes are moving .');
GO
INSERT INTO [dbo].[girlSix] ([id], [createDate], [nickName], [introduce]) VALUES (N'd1cdd67717e549caba16503787b55877', N'2021-02-17 15:27:41.357', N' Little dragon female ', N' I want to live my life ');
GO
INSERT INTO [dbo].[girlSix] ([id], [createDate], [nickName], [introduce]) VALUES (N'efb0ca854dac456b9d8c42d4c4b1bce0', N'2020-05-27 09:03:30.000', N' Liuzijia ', N' the fond gazing of a beauty , The subsidiary party assumes the right , Beautiful and elegant , Enjoy the tranquility and leisure , Duan is a good Kao , The rich peony .');
GO
INSERT INTO [dbo].[girlSix] ([id], [createDate], [nickName], [introduce]) VALUES (N'f839343b980e45caafaa9d2c9797294b', N'2020-05-27 09:04:53.000', N' Weihuijuan ', N' Pulse eye medium wave , A place full of flowers .');
GO
-- ----------------------------
-- Indexes structure for table girlSix
-- ----------------------------
-- ----------------------------
-- Primary Key structure for table [dbo].[girlSix]
-- ----------------------------
ALTER TABLE [dbo].[girlSix] ADD PRIMARY KEY ([id])
GO
test :
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CShapDBHelper
{
class Program
{
static void Main(string[] args)
{
GetList(null);
// add to
string id = System.Guid.NewGuid().ToString("N");
string addSql = string.Format("insert into girlSix values('{0}','{1}','{2}','{3}')",id,DateTime.Now," summer "," Keep in a good mood every day .");
int rows = DBHelper.ExecuteNonQuery(addSql);
Console.WriteLine(rows>0?" The addition, deletion and modification of Ge succeeded ": "NonQuery Error");
// Inquire about
GetList(id);
}
/// <summary>
/// Inquire about
/// </summary>
public static void GetList(string id) {
string sql = "select * from girlSix";
if (!string.IsNullOrEmpty(id)) {
sql += string.Format(" where id='{0}'",id);
}
DataTable dt = DBHelper.Query(sql);
for (int i = 0; i < dt.Rows.Count; i++)
{
Console.WriteLine(dt.Rows[i][0]);
Console.WriteLine(dt.Rows[i][1]);
Console.WriteLine(dt.Rows[i][2]);
Console.WriteLine(dt.Rows[i][3]);
}
}
}
}

边栏推荐
- list和dict的应用
- It is enough to read this article. Web Chinese development
- "New continent" of mobile application going to sea
- How to balance multiple losses in deep learning?
- Robot Jacobian solution
- STM32F1与STM32CubeIDE编程实例-设备驱动-EEPROM-AT24C256驱动
- 大一女生废话编程爆火!懂不懂编程的看完都拴Q了
- Embedded system hardware composition - embedded system hardware architecture
- Image comparison function after registration itk:: checkerboardimagefilter
- Source of routing information
猜你喜欢

About paiwen

Online picture material

Embedded driver design

list和dict的应用

多源BFS问题 模板(附题)

Help you with everything from the basics to the source code. Introduce the technology in detail

Bitmap, bloom filter and hash sharding

Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading

Stm32f1 and stm32cubeide programming examples - device driver -eeprom-at24c256 driver

成功定级腾讯T3-2,万字解析
随机推荐
智能垃圾桶语音芯片应用设计方案介绍,WT588F02B-8S
Binary tree (serialization)
leetcode 47. Permutations II 全排列 II(中等)
About paiwen
Binary tree (thoughts)
Safety KNN
torch_geometric message passing network
[wechat applet development] Part 1: development tool installation and program configuration
Array -- seven array topics with double pointer technique
Software construction 03 regular expression
苹果电脑上MySQL安装完成找不到怎么办
Robot Jacobian solution
基于华为云鲲鹏弹性云服务器ECS部署openGauss数据库【这次高斯不是数学家】
Itk:: neighborhood get 6 neighborhood, 18 neighborhood, 26 neighborhood, 18/6 neighborhood, 26/18 neighborhood
Share PDF HD version, series
在 Debian 10 上独立安装MySQL数据库
itk::SymmetricForcesDemonsRegistrationFilter
【云原生 | Kubernetes篇】Ingress案例实战
When to use binary search
Hudi key generation