简体   繁体   English

Lightswitch执行选择的存储过程以显示数据

[英]Lightswitch execute a select stored procedure to display data

i am new in Lightswitch, and i want to accomplish the following: 我是Lightswitch的新手,我想完成以下任务:

in my db i have a stored procedure which displays (a select sp) some data, how can i call this sp from lightswitch to display the data retrieved from the sp? 在我的数据库中,我有一个存储过程,该过程显示(选择sp)一些数据,如何从lightswitch调用此sp以显示从sp检索到的数据?

thanks in advance. 提前致谢。

This seems like a reasonable task, but Lightswitch does not make it easy. 这似乎是一项合理的任务,但是Lightswitch并不容易。 Just to help you along, here are a few tips: 只是为了帮助您,这里有一些提示:

  1. First, this becomes much easier when you write your own RIA Service or OData source and you assume full control over your entities. 首先,当您编写自己的RIA服务或OData源并假定完全控制实体时,这变得容易得多。
  2. When using Lightswitch out-of-the-box, there is a command-pattern 'hack' to accomplish this that is recognized in the forums as an established pattern. 开箱即用地使用Lightswitch时,存在一种命令模式“ hack”来实现此目的,在论坛中将其识别为既定模式。

The short overview is: you define a custom entity (or table) in Lightswitch. 简短的概述是:在Lightswitch中定义自定义实体(或表)。 It can represent about anything. 它可以代表任何东西。 If your sp takes parameters, often the recommendation is to create columns for the parameters. 如果您的sp带有参数,通常建议您为参数创建列。 The idea is that you have a command button or such that gets invoked. 这个想法是您有一个命令按钮或被调用的按钮。 In the MyButton_Execute() override, you insert values into the table (You call workspace.ApplicationData.MyCustomTable.AddNew() , set the values, and call Save_Changes() ). MyButton_Execute()覆盖中,将值插入表中(调用workspace.ApplicationData.MyCustomTable.AddNew() ,设置值,然后调用Save_Changes() )。 Then you intercept the save pipeline: the MyCustomTable_Inserting(MyCustomTable entity) method. 然后,您将截取保存管道: MyCustomTable_Inserting(MyCustomTable entity)方法。 Here, you write your ado.net code to invoke the sp. 在这里,您编写ado.net代码以调用sp。 And the hack is then to call this.Details.DiscardChanges() ...you aren't really adding a new row...you are just using the pipeline to get access to a point where you can inject custom ado code. 然后黑客将其称为this.Details.DiscardChanges() ...您并没有真正添加新行...您只是在使用管道来访问可以注入自定义ado代码的点。

Now, displaying the results is a bit trickier. 现在,显示结果有点棘手。 Usually, the idea is that your sp updated some underlying tables, and if you refresh your screen, the updated data will show in the controls for those related tables. 通常,其想法是您的sp更新了一些基础表,并且如果刷新屏幕,则更新的数据将显示在那些相关表的控件中。

To capture and display the actual results of your sp, you will have to experiment. 若要捕获并显示sp的实际结果,您必须进行实验。 Perhaps your screen displays the 'MyCustomTable' as a collection, and so you capture the sp output to that same entity, if it is returning a result set. 也许您的屏幕将“ MyCustomTable”显示为集合,因此,如果sp输出返回结果集,则您会将sp输出捕获到该相同实体。 Or if it returns a single row, maybe you can manually update some screen parameters that are linked to text boxes....as I said, Lightswitch does not make it easy, because it is not tracking the invocation of the sp as a table entity that has been modified. 或者,如果返回一行,也许您可​​以手动更新链接到文本框的一些屏幕参数。...正如我所说,Lightswitch并不容易,因为它没有将sp的调用作为表进行跟踪已修改的实体。 Also be aware that if you try to update your screen after the ado invocation has completed, you will be on a different thread. 另请注意,如果您在ado调用完成后尝试更新屏幕,则您将处于其他线程上。 Probably your best bet is to have a table in your actual database that receives the output of the sp, and then have Lightswitch simply treat that results table as any other. 最好的选择也许是在实际数据库中建立一个表,以接收sp的输出,然后让Lightswitch将该结果表与其他任何表一样对待。 You may need to add some sort of identifier so that you can filter the current screen to display only the sp results of the invocation specific to that screen. 您可能需要添加某种标识符,以便可以筛选当前屏幕以仅显示特定于该屏幕的调用的sp结果。 Best of luck. 祝你好运。

i have found a way to solve this problem, or at least some sites that tell you how: 我找到了解决此问题的方法,或者至少找到了一些告诉您如何做的网站:

link1 link2 链接1 链接2

summary: 摘要:

you have to work your way around it via a RIA WCF library,(look at post above), and than add this "class" in your lightswitch application as a refrence. 您必须通过RIA WCF库来解决它(请看上面的文章),然后在您的lightswitch应用程序中添加此“类”作为参考。 Than make an object which get the data your sp gives back. 比创建一个对象可以获取sp返回的数据要多。

how i solved this 我如何解决这个问题

what I did was, I made my selected sp into a insert sp and used this sp to insert the data into an empty table. 我所做的是,将选定的 sp插入了插入 sp,然后使用该sp将数据插入到空表中。 What you do need to do is create a different table for your sp itself to go in. After doing that you can add this code to your Data Source: 您要做的是为sp本身创建一个不同的表。完成后,可以将此代码添加到数据源中:

 partial void StoredProcedureDefinitions_Inserting(StoredProcedureDefinition entity)
    {
        using (SqlConnection connection = new SqlConnection())
        {
            string connectionStringName = this.DataWorkspace.dbsMSccData.Details.Name;

            connection.ConnectionString =
            ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;

            string procedure = entity.Procedure;
            using (SqlCommand command = new SqlCommand(procedure, connection))
            {
                command.CommandType = CommandType.StoredProcedure;

                //foreach (var item in entity.StoredProcedureParameters)
                //{
                //    command.Parameters.Add(
                //        new SqlParameter(item.ParameterName, item.ParameterValue));
                //}

                connection.Open();
                command.ExecuteNonQuery();
            }
        }
        this.Details.DiscardChanges();
    }

and add this code to your screen: 并将此代码添加到屏幕上:

partial void Overzicht_project_telling_Created()
    {
        DataWorkspace dataWorkspace = new DataWorkspace();
        var operation = dataWorkspace.ApplicationData.StoredProcedureDefinitions.AddNew();

        //operation.Database = "dbMSccData";
        operation.Procedure = "dbo.Overzicht_Project_telling_s001";


        dataWorkspace.ApplicationData.SaveChanges();

        this.Refresh();
    }

that should do the trick 这应该够了吧

i hope this was usefull. 我希望这是有用的。 -regards Corwin 问候科温

I found another way to overcome the issue. 我找到了解决该问题的另一种方法。 My current solution has heavy use of SP layer for everything. 我当前的解决方案大量使用SP层。 So to be able to get results of complex SP delivered to LS I just make TSQL table function to run SP. 因此,为了获得传递到LS的复杂SP的结果,我只需使TSQL表函数运行SP。 And then add SQL View with simple select * from that function. 然后使用该函数的简单select *添加SQL View。 LS allows import of Views. LS允许导入视图。 Works just fine. 效果很好。

To force LS treat a field as primary key you could use ISNULL([field],-1). 要强制LS将字段视为主键,可以使用ISNULL([field],-1)。

To force LS to NOT treat a field as PK you use NULLIF([field], '') 要强制LS不将字段视为PK,请使用NULLIF([field],'')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM