执行update, insert,delete 语句, 不返回结果集,(类型化参数)
/// <summary>
/// 执行update, insert,delete 语句, 不返回结果集,(类型化参数)
/// </summary>
/// <param name="connString">连接字符串</param>
/// <param name="sql">待执行的sql语句</param>
/// <param name="errMsg">如果成功执行,返回受影响的行数, 如果执行失败, 返回错误信息</param>
/// <param name="paras">传递给查询的参数</param>
/// <returns>成功执行返回true, 否则返回false</returns>
public static bool ExecSql(string connString, string sql, out string errMsg, params object[] paras)
{
return DB.ExecSql(connString, DB.GetParametricSql(sql, paras), out errMsg);
}
private static string GetParametricSql(string sql, object[] paras)
{
sql = sql.Replace("'", "''");
sql = "EXEC sp_executesql N'" + sql + "'" + DB.ParseSqlArgument(paras);
return sql;
}
/// <summary>
/// 参数化ExecSql 函数调用的子函数, 它把对象参数转换成字符串
/// </summary>
/// <param name="paras"></param>
/// <returns></returns>
private static string ParseSqlArgument(object[] paras)
{
if (paras == null || paras.Length < )
{
return "";
}
string text = ",N'";
string text2 = ",";
int num = ;
for (int i = ; i < paras.Length; i++)
{
object obj;
if (paras[i] is Tuple<int, object>)
{
num = (paras[i] as Tuple<int, object>).Item1;
obj = (paras[i] as Tuple<int, object>).Item2;
}
else
{
obj = paras[i];
}
string text3 = "@p" + num.ToString();
num++;
if (obj == null)
{
throw new Exception("Null argument is not allowed.");
}
if (obj is string)
{
string text4 = obj as string;
text4 = text4.Replace('\'', ''');
object obj2 = text;
text = string.Concat(new object[]
{
obj2,
text3,
" nvarchar(",
(text4.Length > ) ? text4.Length : ,
"),"
});
string text5 = text2;
text2 = string.Concat(new string[]
{
text5,
text3,
"='",
text4,
"',"
});
}
else if (obj is int)
{
int num2 = (int)obj;
text = text + text3 + " int,";
string text6 = text2;
text2 = string.Concat(new string[]
{
text6,
text3,
"=",
num2.ToString(),
","
});
}
else if (obj is float)
{
float num3 = (float)obj;
text = text + text3 + " float,";
string text7 = text2;
text2 = string.Concat(new string[]
{
text7,
text3,
"=",
num3.ToString(),
","
});
}
else if (obj is double)
{
double num4 = (double)obj;
text = text + text3 + " real,";
string text8 = text2;
text2 = string.Concat(new string[]
{
text8,
text3,
"=",
num4.ToString(),
","
});
}
else if (obj is decimal)
{
decimal num5 = (decimal)obj;
string text9 = text;
text = string.Concat(new string[]
{
text9,
text3,
" decimal(18,",
StringTool.GetSectionValue(num5.ToString(), ".", ).Length.ToString(),
"),"
});
string text10 = text2;
text2 = string.Concat(new string[]
{
text10,
text3,
"=",
num5.ToString(),
","
});
}
else if (obj is DateTime)
{
DateTime dateTime = (DateTime)obj;
text = text + text3 + " datetime,";
string text11 = text2;
text2 = string.Concat(new string[]
{
text11,
text3,
"='",
dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff"),
"',"
});
}
else
{
if (!(obj is char))
{
throw new Exception("The data type is not supported currently,please notify the author of this function this error.");
}
char c = (char)obj;
text = text + text3 + " char(1),";
string text12 = text2;
text2 = string.Concat(new string[]
{
text12,
text3,
"='",
c.ToString(),
"',"
});
}
}
text = text.Substring(, text.Length - ) + "'";
text2 = text2.Substring(, text2.Length - );
return text + text2;
}
/// <summary>
/// 取得一个由分隔符分隔的字符串的分段信息. 如果参数不合法, 将取最接近的值返回,而不返回错误信息
/// </summary>
/// <param name="input"></param>
/// <param name="sep"></param>
/// <param name="section">从0开始编号的段号</param>
/// <returns></returns>
public static string GetSectionValue(string input, string sep, int section)
{
string[] array = StringTool.Split(input, sep);
if (section >= array.Length)
{
return "";
}
return array[section];
}
/// <summary>
/// 根据分割符将字符串割成数组,如果参数非法,返回一个长度为1,首元素为母串的数组,
/// 如果母串为null, 则首元素为空串, 不会返回null值.
/// </summary>
/// <param name="input">母串</param>
/// <param name="sep">分割符</param>
/// <returns>返回分割后的数组</returns>
public static string[] Split(string input, string sep)
{
if (input == null || sep == null || input == "" || sep == "" || input.IndexOf(sep) < )
{
return new string[]
{
input ?? ""
};
}
return input.Split(new string[]
{
sep
}, StringSplitOptions.None);
}
执行update, insert,delete 语句, 不返回结果集,(类型化参数)的更多相关文章
- spring data jpa执行update和delete语句时报错处理
之前项目中使用spring data jpa时,遇到删除记录的需求时,主要利用spring data中自带的delete()方法处理,最近在dao层使用delete sql语句时报错,代码如下: @Q ...
- sql insert、update、delete完以后返回主键ID
以前只用过在insert完以后利用select @@IDENTITY返回主键ID,最近在做微信公众平台,遇到一个需求是在帮绑定万微信openid后自动完成登陆,这就需要update以后返回主键ID,查 ...
- SQL Server中UPDATE和DELETE语句结合INNER/LEFT/RIGHT/FULL JOIN的用法
在SQL Server中,UPDATE和DELETE语句是可以结合INNER/LEFT/RIGHT/FULL JOIN来使用的. 我们首先在数据库中新建两张表: [T_A] CREATE TABLE ...
- SQL server触发器中 update insert delete 分别给写个例子被。
SQL server触发器中 update insert delete 分别给写个例子以及解释下例子的作用和意思被, 万分感谢!!!! 主要想知道下各个语句的书写规范. INSERT: 表1 (ID, ...
- (转)jdbc 调用 sql server 的存储过程时“该语句没有返回结果集”的解决方法
本文转载自:http://hedyn.iteye.com/blog/856040 在JDBC中调用SQL Server中的存储过程时出现如下异常: com.microsoft.sqlserver.jd ...
- mybatis的select、insert、update、delete语句
一.select <!-- 查询学生,根据id --> <select id="getStudent" parameterType="String&qu ...
- ASP入门(二十)-INSERT、UPDATE、DELETE语句
插入记录 INSERT INTO 语句 单条记录插入语法 INSERT INTO target [(field1[, field2[, ...]])] VALUES (value1[, value2[ ...
- 轻量ORM-SqlRepoEx (四)INSERT、UPDATE、DELETE 语句
*本文中所用类声明见上一篇博文<轻量ORM-SqlRepoEx (三)Select语句>中Customers类 一.增加记录 1.工厂一个实例仓储 var repository = Rep ...
- oracle数据库高级应用之《自动生成指定表的insert,update,delete语句》
/* * 多条记录连接成一条 * tableName 表名 * type 类型:可以是insert/update/select之一 */ create or replace function my_c ...
随机推荐
- 【Codeforces 584C】Marina and Vasya
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 设cnt表示s1和s2不同的字符的个数 如果cnt>2t 因为这cnt个位置肯定至少有一边不同 显然肯定会有一个f(s,S)的值大于t的 ...
- ansible plugins简介
ansible插件是增强ansible的核心功能的代码片段,ansible使用插件架构来实现丰富,灵活和可扩展的功能集. Ansible提供了许多方便的插件,您可以轻松编写自己的插件. 下边简单介绍A ...
- New Barns
New Barns 时间限制: 1 Sec 内存限制: 128 MB 题目描述 Farmer John notices that his cows tend to get into argument ...
- ZOJ 1516 Uncle Tom's Inherited Land
题目大意: 除去那些作为荷塘的土地块,将剩余的土地希望每次将两块相邻的地一起卖出,最多能卖出多少种这样的由相邻土地 合成的长方形土地块 很明显的二分图问题,但是要考虑如何建模 一个长方形土地总是由相邻 ...
- 欧拉回路输出(DFS,不用回溯!)Watchcow POJ 2230
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8109 Accepted: 3551 Special Judge D ...
- [bzoj1878][SDOI2009]HH的项链_莫队
HH 的项链 bzoj-1878 SDOI-2009 题目大意:给定一个n个数的序列.m次询问,每次询问一段区间内数的种类数. 注释:$1\le n\le 5\cdot 10^4$,$1\le m\l ...
- leetcode_Product of Array Except Self
描写叙述: Given an array of n integers where n > 1, nums, return an array output such that output[i] ...
- QSettings读写注冊表、配置文件
简述 普通情况下.我们在开发软件过程中,都会缓存一些信息到本地,能够使用轻量级数据库sqlite.也能够操作注冊表.读写配置文件. 关于QSettings的使用前面已经介绍过了.比較具体,见" ...
- Java 实现简答的单链表的功能
作者:林子木 博客网址:http://blog.csdn.net/wolinxuebin 參考网址:http://blog.csdn.net/sunsaigang/article/details/5 ...
- 8.30 "我什么都不会"
/* 抢名额第一场 GG "我什么都不会阿" 这场磕死在E题了 按说应该能想到费马小定理 毕竟p is a prime 别的队都过了 大家都比较熟悉的就只有这一个 然后还有I题一开 ...