在最近的项目中我想建立一个EFDBfirst的模型但是失败了,生成的edmx中没有实体类和表结构,到处需求解决方案,未果。
问题请见:https://q.cnblogs.com/q/102743/
后来使用本文写的这个方法
///
1.在sqlserver中建立存储过程
在一个database中展开Programmability,展开Stored Procedures,右击New一个Stroed Procedure
在Query中会出现如下Code:
-- ================================================ -- Template generated from Template Explorer using: -- Create Procedure (New Menu).SQL -- -- Use the Specify Values for Template Parameters -- command (Ctrl-Shift-M) to fill in the parameter -- values below. -- -- This block of comments will not be included in -- the definition of the procedure. -- ================================================ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date,,> -- Description: <Description,,>//下面要添加名字 -- ============================================= CREATE PROCEDURE <Procedure_Name, sysname, ProcedureName> -- Add the parameters for the stored procedure here//添加参数<@Param1, sysname, @p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>, <@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0> AS BEGIN-- SET NOCOUNT ON added to prevent extra result sets from-- interfering with SELECT statements.SET NOCOUNT ON;-- Insert statements for procedure here//添加sql语句SELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2> END GO
根据提示添加所需参数和sql语句,在上面代码中已经用中文标注位置
给个小demo:
添加如下代码(由于项目原因隐掉了数据库名和表名,见谅)
USE [xxx] GO /****** Object: StoredProcedure [dbo].[GetPluralProject] Script Date: 1/5/2018 1:39:18 PM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date,,> -- Description: <Description,,> -- ============================================= ALTER PROCEDURE [dbo].[GetPluralProject]--这边是名字-- Add the parameters for the stored procedure here//参数@Language nvarchar(20) AS BEGIN-- SET NOCOUNT ON added to prevent extra result sets from-- interfering with SELECT statements.SET NOCOUNT ON;-- Insert statements for procedure here//sql语句SELECT distinct trans.[ProjectName] as ProjectNameFROM [xxx].[dbo].[xxx] transWhere trans.string like '%plural,%' END
然后运行一下就好了,在刚才打开的目录下会出现你建好的存储过程
在当前数据库下新建一个Query(也可以use当前数据库,不然找不到,因为这个存储过程毕竟是存在当前数据库下的),sql语句如下:
exec GetPluralProject ''
运行,结果就出来了
2.现在要用C#代码来调用这个存储过程,并给它传参数
我这边有个写好的DbHelper.cs文件,这是需要用到的,下载路径:https://pan.baidu.com/s/1bpLfw7t
在Models中添加这个文件
以下是该文件片段(咱们需要用到的,不需要修改什么,贴出来看看)
/// <summary>/// Executes the stored procedure, and return DataTable/// </summary>/// <param name="paraValues"></param>/// <returns></returns>/// public DataTable ExecuteDataTable(string ProName, params object[] paraValues)//存储过程名字,参数{SqlCommand cmd = CreateSqlCommand(ProName);try{this.DeriveParameters(cmd);this.AssignParameterValues(cmd, paraValues);SqlDataAdapter adapter = new SqlDataAdapter(cmd);DataTable dataTable = new DataTable();adapter.Fill(dataTable);return dataTable;}catch{throw;}}
好我们来调用这个函数,我是在webapi中用到的,所以写在controller中,可以看上一篇博客
代码如下:
using System.Collections.Generic; using System.Data; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using xxxService.Models;namespace xxxService.Controllers {public class ProjectsController : ApiController{[CrossSite]public DataTable Get(){DbHelper DBH = new DbHelper();DataTable result = DBH.ExecuteDataTable("GetPluralProject", "");return result;}} }
这里没有写到怎么定义参数补充下:
object[] Params = new object[2];Params[0] = ProjectName;Params[1] = CultureName;DbHelper DBH = new DbHelper();DataTable result = DBH.ExecuteDataTable("xxx", Params);return result;
调用到这个函数后就得到了数据
我是用ajax调用api以json的形式返回的这个数据,用jquery处理的,还挺方便的
在C#中怎么处理还没怎么研究,但是应该不难,数据在Rows下面Results View里面