C# 调用SQL的存储过程的接口及实现
2013-07-10 15:42
381 查看
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(); }本文出自 “木子纵横” 博客,请务必保留此出处http://muzizongheng.blog.51cto.com/856912/1333047
相关文章推荐
- C# 调用SQL的存储过程的接口及实现
- C# 调用SQL的存储过程的接口及实现
- 使用ADO.NET 实体数据模型实现SQL存储过程调用
- [转]C# 调用sql 2000存储过程
- 存储过程和SQL语句比较及存储过程在C#中调用方法(转)
- 存储过程在C#中调为何会是空值,在SQL调用有结果。
- C#调用SQL存储过程以及返回值和参数的获取
- C#调用带有参数的SQL存储过程
- 存储过程和SQL语句比较及存储过程在C#中调用方法
- 数据库知识体系+sql语句实现存储过程和函数+应用程序接口
- 简单C# 与SQL存储过程调用
- 存储过程和SQL语句比较及存储过程在C#中调用方法
- C#调用SQL 存储过程的两个方法
- c#调用sql存储过程的步骤
- C#调用SQL中的存储过程中有output参数,存储过程执行过程中返回信息
- 利用Sql作业在asp.net 里面实现异步调用存储过程.
- C#中调用SQL存储过程
- c#学习之-c#通过sql存储过程实现分页
- C#调用SQL存储过程
- C# asp.net SQL存储过程写法Insert 插入语句,及其在asp.net中的调用方法