C# 调用SQL的存储过程的接口及实现
2024-10-21 12:44:26
1. 接口为ExecuteStoredProcedure(string storedProcedureName, params ObjectParameter[] parameters)
2. 参数为存储过程名字, 及输入值。
3. 思路:创建连接(连接中指定了是Sql/MySql/ODBC等等); 创建通用DbCommand;更改Text以及Type;添加通用Parameter(DBParameter是抽象类,因此需要判断connection类型);判断连接状态(需Open); 调用Execute方法; 关闭连接。
4.
实现如下:
// In V1 of the EF, the context connection is always an EntityConnection
EntityConnection entityConnection = (EntityConnection)protocolDB.Connection; // The EntityConnection exposes the underlying store connection
DbConnection storeConnection = entityConnection.StoreConnection;
DbCommand command = storeConnection.CreateCommand();
command.CommandText = storedProcedureName;
command.CommandType = CommandType.StoredProcedure; if (storeConnection is SqlConnection)
{
foreach (ObjectParameter p in parameters)
{
command.Parameters.Add(new SqlParameter { ParameterName = p.Name, Value = p.Value });
}
}
else if (storeConnection is MySqlConnection)
{
foreach (ObjectParameter p in parameters)
{
command.Parameters.Add(new MySqlParameter { ParameterName = p.Name, Value = p.Value });
}
}
else
{
return enProtocolDBReturnCodes.OPERATION_FAILED;
} bool openingConnection = command.Connection.State == ConnectionState.Closed;
if (openingConnection)
{
command.Connection.Open();
} command.ExecuteNonQuery(); if (openingConnection && command.Connection.State == ConnectionState.Open)
{
command.Connection.Close();
}
最新文章
- mongodb 3.x 之实用新功能窥看[2] ——使用$lookup做多表关联处理
- angular使用post、get向后台传参的问题
- 多边形碰撞 -- SAT方法
- Sqlserver 函数(例子)
- 关于Yii2中CSS,JS文件的引入心得
- org.hibernate.QueryException: could not resolve property
- 数据结构【二】:简单阻塞队列BlockingQueue
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) A. Bear and Poker 分解
- 【转】C#路径/文件/目录/I/O常见操作汇总
- 基于Predictive Parsing的ABNF语法分析器(十)——AbnfParser文法解析器之数值类型(num-val)
- C++实现引用和被引用对象分离
- App.config自定义节点读取
- 如何在电脑上配置两个tomcat
- F#周报2019年第9期
- java数据结构分析
- Django积木块六——验证用户是否登录
- 获取DataSet中某行某列的数据
- TensorFlow实战Google深度学习框架10-12章学习笔记
- 用智能TFT液晶模块这种串口屏做产品界面设计太简单了,大大的节省了开发时间
- MySQL的nnodb引擎表数据分区存储