分别以几个特别的存储过程为例说明下,Wcf Ria Service中怎么样调用存储过程。
源代码下载
(一)带输入参数和输出参数的存储过程。
在Wcf Ria Services学习笔记(一)中有个存储过程名为totalcredit的存储过程 [dbo].[totalcredit](@name varchar(40),@total int output) ,如何在Wcf Ria Service中调用此存储过程呢?
(1)导入存储过程
我们在上前章节可以看到,只有表,视图才添加到了Model1.edmx中,而存储过程和自定义函数是没有添加到*.edmx。这个需要我们手动添加的, 我们需要使用 ADO.NET 实体数据模型设计器(实体设计器)来导入存储过程!
首先:从“模型浏览器”中,打开“存储过程”文件夹(在存储模型信息中),然后双击没有对应函数导入的存储过程。如图:
其次:填入新函数导入的设置,在弹出的对话框中,指定下列四种基本返回类型之一:“无”、“标量”、“复杂”或“实体”,然后从可用下拉列表中选择特定返回类型。这里指定为"无"。并为存储过程领取一个名字。如图: 点确定后,会在Model.edmx中自动添加如下代码。
view plain copy to clipboard print ? #region 函数导入 /// <summary> /// 没有元数据文档可用。 /// </summary> /// <param name="name">没有元数据文档可用。</param> /// <param name="total">没有元数据文档可用。</param> public int GetTotalCredit(global::System.String name, ObjectParameter total) { ObjectParameter nameParameter; if (name != null) { nameParameter = new ObjectParameter("name", name); } else { nameParameter = new ObjectParameter("name", typeof(global::System.String)); } return base.ExecuteFunction("GetTotalCredit", nameParameter, total); } #endregion #region 函数导入 /// <summary> /// 没有元数据文档可用。 /// </summary> /// <param name="name">没有元数据文档可用。</param> /// <param name="total">没有元数据文档可用。</param> public int GetTotalCredit(global::System.String name, ObjectParameter total) { ObjectParameter nameParameter; if (name != null) { nameParameter = new ObjectParameter("name", name); } else { nameParameter = new ObjectParameter("name", typeof(global::System.String)); } return base.ExecuteFunction("GetTotalCredit", nameParameter, total); } #endregion
(2)在服务器端的 DomainService1.cs 中添加如下代码! 一定要加Invoke,表示是调用的服务,而且输出参数要自己用 ObjectParamete定义!
view plain copy to clipboard print ? [Invoke] public int GetTotalCreditByStudentName(string studentName) { ObjectParameter parameter = new ObjectParameter("total", typeof(int)); this.ObjectContext.GetTotalCredit(studentName, parameter); return Convert.ToInt32( parameter.Value); } [Invoke] public int GetTotalCreditByStudentName(string studentName) { ObjectParameter parameter = new ObjectParameter("total", typeof(int)); this.ObjectContext.GetTotalCredit(studentName, parameter); return Convert.ToInt32( parameter.Value); }
(3)在MainPage.xaml文件中调用该服务.因为Invoke操作时立即执行的,所以不需要submitchange()操作
view plain copy to clipboard print ? private void button1_Click(object sender, RoutedEventArgs e) { //调用存储过程 InvokeOperation<int> invokeOperation = client.GetTotalCreditByStudentName(this.textBox1.Text.Trim(), OnInvokeCallback,null); } private void OnInvokeCallback(InvokeOperation<int> invokeOp) { if (invokeOp.HasError) { MessageBox.Show(string.Format("Method Failed:{0}", invokeOp.Error.Message)); invokeOp.MarkErrorAsHandled(); } else { this.textBlock1.Text = invokeOp.Value.ToString(); } } private void button1_Click(object sender, RoutedEventArgs e) { //调用存储过程 InvokeOperation<int> invokeOperation = client.GetTotalCreditByStudentName(this.textBox1.Text.Trim(), OnInvokeCallback,null); } private void OnInvokeCallback(InvokeOperation<int> invokeOp) { if (invokeOp.HasError) { MessageBox.Show(string.Format("Method Failed:{0}", invokeOp.Error.Message)); invokeOp.MarkErrorAsHandled(); } else { this.textBlock1.Text = invokeOp.Value.ToString(); } }
以上就完成了带输入输出参数存储过程的调用!
(二)执行一个命令或事务(如delete操作)
操作过程也和上面差不多,比如我们想做一个删除操作新建了一个存储过程!
view plain copy to clipboard print ? create procedure sp_DelStudentByMaxStudentID @ID int as delete from Student_Course where Student_Course.ID=@ID; create procedure sp_DelStudentByMaxStudentID @ID int as delete from Student_Course where Student_Course.ID=@ID;
我们在导入存储过程时,设置返回类型为“无”,并命名为DelStudentByMaxStudentID,在DomainService1中这样写的
view plain copy to clipboard print ? [Invoke] public int Del(int ID) { return this.ObjectContext.DelStudentByMaxStudentID(ID); } [Invoke] public int Del(int ID) { return this.ObjectContext.DelStudentByMaxStudentID(ID); }
在MainPage.xaml中和前面的差不多,只是返回值,是删除数据库记录的条数!
(三)执行返回多个字段的存储过程
(1)这个比较麻烦些,在设置返回类型是为 “复杂”,例如第一章节的存储过程sp_SelectAllStudent
(2)需要在DomainService1.metadata.cs添加自定义的复杂类型,如下面的代码
view plain copy to clipboard print ? // The MetadataTypeAttribute identifies AllStudentMetadata as the class // that carries additional metadata for the AllStudent class. [MetadataTypeAttribute(typeof(AllStudent.AllStudentMetadata))] public partial class AllStudent { // This class allows you to attach custom attributes to properties // of the Student class. // // For example, the following marks the Xyz property as a // required property and specifies the format for valid values: // [Required] // [RegularExpression("[A-Z][A-Za-z0-9]*")] // [StringLength(32)] // public string Xyz { get; set; } internal sealed class AllStudentMetadata { // Metadata classes are not meant to be instantiated. private AllStudentMetadata() { } public Nullable<DateTime> Birthday { get; set; } public string Major { get; set; } public string Remark { get; set; } public Nullable<bool> Sex { get; set; } //请注意,一定要为该复杂类型加上个【Key】这个属性,以表示为关键字段 //不然编译是不会通过的! [Key] public string StudentID { get; set; } public string StudentName { get; set; } public Nullable<byte> TotalCredit { get; set; } } } // The MetadataTypeAttribute identifies AllStudentMetadata as the class // that carries additional metadata for the AllStudent class. [MetadataTypeAttribute(typeof(AllStudent.AllStudentMetadata))] public partial class AllStudent { // This class allows you to attach custom attributes to properties // of the Student class. // // For example, the following marks the Xyz property as a // required property and specifies the format for valid values: // [Required] // [RegularExpression("[A-Z][A-Za-z0-9]*")] // [StringLength(32)] // public string Xyz { get; set; } internal sealed class AllStudentMetadata { // Metadata classes are not meant to be instantiated. private AllStudentMetadata() { } public Nullable<DateTime> Birthday { get; set; } public string Major { get; set; } public string Remark { get; set; } public Nullable<bool> Sex { get; set; } //请注意,一定要为该复杂类型加上个【Key】这个属性,以表示为关键字段 //不然编译是不会通过的! [Key] public string StudentID { get; set; } public string StudentName { get; set; } public Nullable<byte> TotalCredit { get; set; } } }
(3)在DomainService1.cs中代码如下
view plain copy to clipboard print ? [Query] public IQueryable<AllStudent> GetSelectAllStudent() { return this.ObjectContext.SelectAllStudent().AsQueryable<AllStudent>(); } [Query] public IQueryable<AllStudent> GetSelectAllStudent() { return this.ObjectContext.SelectAllStudent().AsQueryable<AllStudent>(); }
(4)调用基本和调用一般的查询方法一样,这里不再重复!
(四)数据库中自定义函数是不能被导入的!双击会弹出对话框!所以要使用自定义函数,最好用存储过程先封装!
转载于:https://www.cnblogs.com/nnkook/archive/2011/12/13/2285831.html
相关资源:数据结构—成绩单生成器