csharp: MySQL Stored Procedure using DAL
# 建表 塗聚文 20160907
drop table attendrecord; create table attendrecord
(
seq INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
emp_no varchar(20) null,
rdate datetime not null,
rtime time not null,
rdescription varchar(100),
rdes_reasnon varchar(100),
branch varchar(50)
);
#存储过程
# 添加
DELIMITER $$
DROP PROCEDURE IF EXISTS `attend`.`proc_Insert_Attendrecord` $$
CREATE PROCEDURE `attend`.`proc_Insert_Attendrecord`
(
IN param1emp_no VarChar(20),
IN param1rdate Datetime ,
IN param1rtime Time,
IN param1rdescription VarChar(100),
IN param1rdes_reasnon VarChar(100),
IN param1branch VarChar(50)
)
BEGIN
INSERT INTO attendrecord
(
emp_no ,
rdate ,
rtime ,
rdescription ,
rdes_reasnon ,
branch
)
VALUES
(
param1emp_no ,
param1rdate ,
param1rtime ,
param1rdescription ,
param1rdes_reasnon ,
param1branch
); END $$
DELIMITER ; -- 添加
DELIMITER $$
DROP PROCEDURE IF EXISTS proc_Insert_Attendrecord $$
CREATE PROCEDURE proc_Insert_Attendrecord
(
IN param1emp_no VarChar(20),
IN param1rdate Datetime ,
IN param1rtime Time,
IN param1rdescription VarChar(100),
IN param1rdes_reasnon VarChar(100),
IN param1branch VarChar(50)
)
BEGIN
INSERT INTO attendrecord
(
emp_no ,
rdate ,
rtime ,
rdescription ,
rdes_reasnon ,
branch
)
VALUES
(
param1emp_no ,
param1rdate ,
param1rtime ,
param1rdescription ,
param1rdes_reasnon ,
param1branch
); END $$
DELIMITER ; #添加
DELIMITER $$
DROP PROCEDURE IF EXISTS proc_Insert_AttendrecordOutput $$
CREATE PROCEDURE proc_Insert_AttendrecordOutput
(
IN param1emp_no VarChar(20),
IN param1rdate Datetime,
IN param1rtime Time,
IN param1rdescription VarChar(100),
IN param1rdes_reasnon VarChar(100),
IN param1branch VarChar(50),
out param1seq int
)
BEGIN
INSERT INTO attendrecord
(
emp_no ,
rdate ,
rtime ,
rdescription ,
rdes_reasnon ,
branch
)
VALUES
(
param1emp_no ,
param1rdate ,
param1rtime ,
param1rdescription ,
param1rdes_reasnon ,
param1branch
);
SELECT LAST_INSERT_ID() into param1seq;
END $$
DELIMITER ;
/// <summary>
/// Attendrecord数据访问层
///生成時間2016-9-6 17:24:08
///塗聚文(Geovin Du) 自建代码生成器生成(简单存储过程也可以生成)
///</summary>
public class AttendrecordDAL : IAttendrecord
{
///<summary>
/// 追加记录 存储过程
///</summary>
///<param name="AttendrecordInfo"></param>
///<returns></returns>
public int InsertAttendrecord(AttendrecordInfo attendrecord)
{
int ret = 0;
try
{
MySqlParameter[] par = new MySqlParameter[]{
new MySqlParameter("?param1emp_no",MySqlDbType.VarChar,20),
new MySqlParameter("?param1rdate",MySqlDbType.Datetime),
new MySqlParameter("?param1rtime",MySqlDbType.String), //涂聚文注:不能用MySqlDbType.Time否则报错:base {System.Data.Common.DbException} = {"Only TimeSpan objects can be serialized by MySqlTimeSpan"}
new MySqlParameter("?param1rdescription",MySqlDbType.VarChar,100),
new MySqlParameter("?param1rdes_reasnon",MySqlDbType.VarChar,100),
new MySqlParameter("?param1branch",MySqlDbType.VarChar,50),
};
par[0].Value = attendrecord.Emp_no;
par[1].Value = attendrecord.Rdate;
par[2].Value = attendrecord.Rtime;
par[3].Value = attendrecord.Rdescription;
par[4].Value = attendrecord.Rdes_reasnon;
par[5].Value = attendrecord.Branch;
ret = DBHelper.ExecuteSql("proc_Insert_Attendrecord", CommandType.StoredProcedure, par);
}
catch (MySqlException ex)
{
throw ex;
}
return ret;
}
///<summary>
/// 追加记录 SQL脚本
///</summary>
///<param name="AttendrecordInfo"></param>
///<returns></returns>
public int InsertSqlAttendrecord(AttendrecordInfo attendrecord)
{
int ret = 0;
try
{
StringBuilder strSql = new StringBuilder();
strSql.Append("INSERT INTO attendrecord(emp_no,rdate,rtime,rdescription,rdes_reasnon,branch");
strSql.Append(") VALUES (");
strSql.Append("?param1emp_no ,?param1rdate ,?param1rtime ,?param1rdescription ,?param1rdes_reasnon ,?param1branch)");
MySqlParameter[] par = new MySqlParameter[]{
new MySqlParameter("?param1emp_no",MySqlDbType.VarChar,20),
new MySqlParameter("?param1rdate",MySqlDbType.Datetime),
new MySqlParameter("?param1rtime",MySqlDbType.String),
new MySqlParameter("?param1rdescription",MySqlDbType.VarChar,100),
new MySqlParameter("?param1rdes_reasnon",MySqlDbType.VarChar,100),
new MySqlParameter("?param1branch",MySqlDbType.VarChar,50),
};
par[0].Value = attendrecord.Emp_no;
par[1].Value = attendrecord.Rdate;
par[2].Value = attendrecord.Rtime;
par[3].Value = attendrecord.Rdescription;
par[4].Value = attendrecord.Rdes_reasnon;
par[5].Value = attendrecord.Branch;
ret = DBHelper.ExecuteSql(strSql.ToString(), CommandType.Text, par);
}
catch (MySqlException ex)
{
throw ex;
}
return ret;
}
类似于SQL Server中的:sp_executesql
sql server script:
IF EXISTS (SELECT * FROM sysobjects WHERE [name] = 'proc_Select_DuDeptUserCount')
DROP PROCEDURE proc_Select_DuDeptUserCount
GO
CREATE PROCEDURE proc_Select_DuDeptUserCount
(
@where NVARCHAR(1000)
)
AS
DECLARE @sql NVARCHAR(4000)
SET @sql='select count(*) as H from DuDeptUser '
IF @where<>''
SET @sql=@sql+@where
EXEC(@sql)
GO
MySql script:
#表有多少条记录 Geovin Du
DELIMITER $$
DROP PROCEDURE IF EXISTS proc_Select_AttendrecordCount $$
CREATE PROCEDURE proc_Select_AttendrecordCount
(
IN wherestr varchar(1000)
)
BEGIN
declare sqlstr varchar(2000);
set sqlstr='SELECT count(1) as H FROM attendrecord';
if wherestr='' then
set sqlstr=sqlstr;
else
set sqlstr=sqlstr+wherestr;
end if;
set @sqlstr=sqlstr;
-- call(sqlstr);
PREPARE stmt FROM @sqlstr;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END $$
DELIMITER ;
# 测试
call proc_Select_AttendrecordCount('');
#视图有多少条记录 涂聚文
DELIMITER $$
DROP PROCEDURE IF EXISTS proc_Select_AttendrecordCountView $$
CREATE PROCEDURE proc_Select_AttendrecordCountView
(
IN wherestr varchar(1000)
)
BEGIN
declare sqlstr varchar(2000);
set sqlstr='SELECT count(1) as H FROM View_attendrecord';
if wherestr='' then
set sqlstr=sqlstr;
else
set sqlstr=sqlstr+wherestr;
end if;
set @sqlstr=sqlstr;
-- call(sqlstr);
PREPARE stmt FROM @sqlstr;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END $$
DELIMITER ; #查询某记录的字段艺工作者Geovin Du
DELIMITER $$
DROP PROCEDURE IF EXISTS proc_Select_AttendrecordTitle $$
CREATE PROCEDURE proc_Select_AttendrecordTitle
( IN FieldName varchar(1000),
IN param1id int
)
BEGIN
declare sqlstr varchar(2000);
declare wherestr varchar(1000);
set sqlstr=CONCAT('select ',FieldName);
set wherestr=CONCAT(' from attendrecord WHERE seq =',cast(param1id as char(20)));
set sqlstr=CONCAT(sqlstr,wherestr);
set @sqlstr=sqlstr;
-- select @sqlstr;
-- call(sqlstr);
PREPARE stmt FROM @sqlstr; -- 5.1 up
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END $$
DELIMITER ; call proc_Select_AttendrecordTitle('rdescription',1); select concat(2); select cast(2 as char(20));
#模糊查询
DELIMITER $$
DROP PROCEDURE IF EXISTS proc_Select_AttendrecordFuzzySearch $$
CREATE PROCEDURE proc_Select_AttendrecordFuzzySearch
( IN FieldList varchar(1000),
IN wherestr varchar(2000)
)
BEGIN
declare sqlstr varchar(2000);
declare ifwherestr varchar(1000);
declare iflist varchar(1000);
set ifwherestr='';
if FieldList='' then
set iflist=' * ';
else
set iflist=FieldList;
end if;
if wherestr<>'' then
set ifwherestr=CONCAT(' WHERE ',wherestr);
end if;
set sqlstr=CONCAT('select ',iflist);
set sqlstr=CONCAT(sqlstr,' from attendrecord ');
set sqlstr=CONCAT(sqlstr,ifwherestr);
set @sqlstr=sqlstr;
-- select @sqlstr;
-- call(sqlstr);
PREPARE stmt FROM @sqlstr;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END $$
DELIMITER ; -- seq =1
call proc_Select_AttendrecordFuzzySearch('rdescription','seq=1');
csharp: MySQL Stored Procedure using DAL的更多相关文章
- csharp: Oracle Stored Procedure DAL using ODP.NET
paging : http://www.codeproject.com/Articles/44858/Custom-Paging-GridView-in-ASP-NET-Oracle https:// ...
- Retrieving Out Params From a Stored Procedure With Python
http://www.rodneyoliver.com/blog/2013/08/08/retrieving-out-params-from-a-stored-procedure-with-pytho ...
- JDBC连接执行 MySQL 存储过程报权限错误:User does not have access to metadata required to determine stored procedure parameter types. If rights can not be granted,
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...
- Archive MySQL Data In Chunks Using Stored Procedure
sqladminon September 26, 2018 In a DBA’s day to day activities, we are doing Archive operation on ou ...
- Home / Python MySQL Tutorial / Calling MySQL Stored Procedures in Python Calling MySQL Stored Procedures in Python
f you are not familiar with MySQL stored procedures or want to review it as a refresher, you can fol ...
- [MySQL] Stored Procedures 【转载】
Stored routines (procedures and functions) can be particularly useful in certain situations: When mu ...
- Cursors in MySQL Stored Procedures
https://www.sitepoint.com/cursors-mysql-stored-procedures/ After my previous article on Stored Proce ...
- How To Call Stored Procedure In Hibernate
How To Call Stored Procedure In Hibernate In this tutorial, you will learn how to call a store proce ...
- 数据库之存储过程(Stored Procedure)【未完待续】
存储过程(Stored Procedure,以下简称SP)是啥?有什么用?优缺点? 一个SP是一段存储在数据库系统中的一条或多条sql语句的集合,类似一条批处理,它能被触发器,或者其他的SP以及APP ...
随机推荐
- 女生的最爱,装饰品。WPF也有,Adorner。(上海晒衣服理念)
说到装饰,不由要说到女性. 去年过年回家给我妈买了周大福项链,很明显就感觉待遇就不一样了,即使这样,还是被一个阿姨说应该买更重点的.看来钱这种东西果然是多一点才好.虽然自己无所谓,但让家里人更开心也是 ...
- 新版C#编译器关于函数闭包的一处更改
感谢@DiryBoy的补充,他提到这个问题在MSDN上是有说明的: http://msdn.microsoft.com/en-us/library/vstudio/hh678682.aspx 在Vis ...
- MySQL 指定各分区路径
200 ? "200px" : this.width)!important;} --> 介绍 可以针对分区表的每个分区指定各自的存储路径,对于innodb存储引擎的表只能指定 ...
- [转]GC简介
[转]GC简介 原文链接:http://www.cnblogs.com/cposture/p/4845189.html 原文写得太好了,这里转一下. 1 GC机制 1.1 对象 从计算机的角度,装有数 ...
- [ASP.NET MVC 小牛之路]06 - 使用 Entity Framework
在家闲着也是闲着,继续写我的[ASP.NET MVC 小牛之路]系列吧.在该系列的上一篇博文中,在显示书本信息列表的时候,我们是在程序代码中手工造的数据.本文将演示如何在ASP.NET MVC中使用E ...
- SSH实战 · SSH项目开发环境搭建
一:SSH整合 创建一个新的WEB项目 引入struts2.3.15.3: jar包: struts-2.3.15.3\apps\struts2-blank.war\W ...
- JVM系列-分代收集垃圾回收
Java自动垃圾回收(Automatic Garbage Collection)是自动回收堆上不再使用的内存,new的对象在程序中没有引用指向它,就会被回收.回收的实现很多,有Reference Co ...
- Chrome开发者工具不完全指南(四、性能进阶篇)
前言 Profiles面板功能的作用主要是监控网页中各种方法执行时间和内存的变化,简单来说它就是Timeline的数字化版本.它的功能选项卡不是很多(只有三个),操作起来比较前面的几块功能版本来说简单 ...
- CefSharp初识--把网页移到桌面
在开发中我们可曾有过这样的需求,将某个网页嵌入到.Net应用中来,但Winform自带的web browser不怎么理想.CefSharp可以让我们在.Net应用中嵌入一个Chromium.它提供了W ...
- atom-shell程序打包
上一篇:http://www.cnblogs.com/luobenCode/p/4504910.html 打包之前要准备一下 请看这篇http://www.cnblogs.com/seanlv/arc ...