質問

i am new in Lightswitch, and i want to accomplish the following:

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?

thanks in advance.

役に立ちましたか?

解決

This seems like a reasonable task, but Lightswitch does not make it easy. 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.
  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.

The short overview is: you define a custom entity (or table) in Lightswitch. It can represent about anything. If your sp takes parameters, often the recommendation is to create columns for the parameters. 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()). Then you intercept the save pipeline: the MyCustomTable_Inserting(MyCustomTable entity) method. Here, you write your ado.net code to invoke the 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.

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.

To capture and display the actual results of your sp, you will have to experiment. 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. 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. Also be aware that if you try to update your screen after the ado invocation has completed, you will be on a different thread. 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. 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. Best of luck.

他のヒント

i have found a way to solve this problem, or at least some sites that tell you how:

link1 link2

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. Than make an object which get the data your sp gives back.

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. 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:

 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. So to be able to get results of complex SP delivered to LS I just make TSQL table function to run SP. And then add SQL View with simple select * from that function. LS allows import of Views. Works just fine.

To force LS treat a field as primary key you could use ISNULL([field],-1).

To force LS to NOT treat a field as PK you use NULLIF([field], '')

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top