WCF RIA Service中调用存储过程

分别以几个特别的存储过程为例说明下,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中自动添加如下代码。

       

       

[c-sharp]  view plain copy
  1. #region 函数导入  
  2.   
  3.    /// <summary>  
  4.    /// 没有元数据文档可用。  
  5.    /// </summary>  
  6.    /// <param name="name">没有元数据文档可用。</param>  
  7.    /// <param name="total">没有元数据文档可用。</param>  
  8.    public int GetTotalCredit(global::System.String name, ObjectParameter total)  
  9.    {  
  10.        ObjectParameter nameParameter;  
  11.        if (name != null)  
  12.        {  
  13.            nameParameter = new ObjectParameter("name", name);  
  14.        }  
  15.        else  
  16.        {  
  17.            nameParameter = new ObjectParameter("name"typeof(global::System.String));  
  18.        }  
  19.   
  20.        return base.ExecuteFunction("GetTotalCredit", nameParameter, total);  
  21.    }      
  22.  
  23.    #endregion  

       (2)在服务器端的 DomainService1.cs  中添加如下代码! 一定要加Invoke,表示是调用的服务,而且输出参数要自己用 ObjectParamete定义!

[c-sharp]  view plain copy
  1. [Invoke]  
  2.        public  int GetTotalCreditByStudentName(string studentName)  
  3.        {            
  4.            ObjectParameter parameter = new ObjectParameter("total"typeof(int));  
  5.            this.ObjectContext.GetTotalCredit(studentName, parameter);              
  6.            return Convert.ToInt32( parameter.Value);                  
  7.        }  

 

        (3)在MainPage.xaml文件中调用该服务.因为Invoke操作时立即执行的,所以不需要submitchange()操作

         

             

[c-sharp]  view plain copy
  1. private void button1_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     //调用存储过程  
  4.     InvokeOperation<int> invokeOperation = client.GetTotalCreditByStudentName(this.textBox1.Text.Trim(), OnInvokeCallback,null);                 
  5. }     
  6.   
  7. private void OnInvokeCallback(InvokeOperation<int> invokeOp)  
  8. {  
  9.     if (invokeOp.HasError)  
  10.     {  
  11.         MessageBox.Show(string.Format("Method Failed:{0}", invokeOp.Error.Message));  
  12.         invokeOp.MarkErrorAsHandled();  
  13.     }  
  14.     else  
  15.     {  
  16.         this.textBlock1.Text = invokeOp.Value.ToString();  
  17.     }  
  18. }  

   以上就完成了带输入输出参数存储过程的调用!

 

(二)执行一个命令或事务(如delete操作)

操作过程也和上面差不多,比如我们想做一个删除操作新建了一个存储过程!

[c-sharp]  view plain copy
  1. create procedure sp_DelStudentByMaxStudentID @ID int  
  2. as  
  3. delete  from Student_Course where Student_Course.ID=@ID;  

我们在导入存储过程时,设置返回类型为“无”,并命名为DelStudentByMaxStudentID,在DomainService1中这样写的

[c-sharp]  view plain copy
  1. [Invoke]  
  2.        public int Del(int ID)  
  3.        {  
  4.            return this.ObjectContext.DelStudentByMaxStudentID(ID);  
  5.        }  

在MainPage.xaml中和前面的差不多,只是返回值,是删除数据库记录的条数!

 

(三)执行返回多个字段的存储过程

(1)这个比较麻烦些,在设置返回类型是为 “复杂”,例如第一章节的存储过程sp_SelectAllStudent

(2)需要在DomainService1.metadata.cs添加自定义的复杂类型,如下面的代码

[c-sharp]  view plain copy
  1. // The MetadataTypeAttribute identifies AllStudentMetadata as the class  
  2.     // that carries additional metadata for the AllStudent class.  
  3.     [MetadataTypeAttribute(typeof(AllStudent.AllStudentMetadata))]  
  4.     public partial class AllStudent  
  5.     {  
  6.   
  7.         // This class allows you to attach custom attributes to properties  
  8.         // of the Student class.  
  9.         //  
  10.         // For example, the following marks the Xyz property as a  
  11.         // required property and specifies the format for valid values:  
  12.         //    [Required]  
  13.         //    [RegularExpression("[A-Z][A-Za-z0-9]*")]  
  14.         //    [StringLength(32)]  
  15.         //    public string Xyz { get; set; }  
  16.         internal sealed class AllStudentMetadata  
  17.         {  
  18.   
  19.             // Metadata classes are not meant to be instantiated.  
  20.             private AllStudentMetadata()  
  21.             {  
  22.             }  
  23.   
  24.             public Nullable<DateTime> Birthday { getset; }  
  25.   
  26.             public string Major { getset; }  
  27.   
  28.             public string Remark { getset; }  
  29.   
  30.             public Nullable<bool> Sex { getset; }  
  31.   
  32.             //请注意,一定要为该复杂类型加上个【Key】这个属性,以表示为关键字段  
  33.             //不然编译是不会通过的!  
  34.             [Key]  
  35.             public string StudentID { getset; }  
  36.   
  37.             public string StudentName { getset; }  
  38.   
  39.             public Nullable<byte> TotalCredit { getset; }  
  40.         }  
  41.     }  

(3)在DomainService1.cs中代码如下

[c-sharp]  view plain copy
  1. [Query]  
  2.        public IQueryable<AllStudent> GetSelectAllStudent()  
  3.        {  
  4.            return this.ObjectContext.SelectAllStudent().AsQueryable<AllStudent>();  
  5.        }  

(4)调用基本和调用一般的查询方法一样,这里不再重复!

 

(四)数据库中自定义函数是不能被导入的!双击会弹出对话框!所以要使用自定义函数,最好用存储过程先封装!


原文地址:http://blog.csdn.net/joetao/article/details/5734907

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值