简体   繁体   中英

Stored Procedure Call from Lightswitch

I'm using Lightswitch 2013 (C#) over SQL Server to do some basic data entry each month. After the user enters data on a series of screens, I want them to be able execute a Stored Procedure on the database which will kick off a series of tasks to build an SSAS cube. eg. usp_DoTasks

So the button is called "Process Data" and is on the EBIT screen.

在此处输入图片说明

I can't seem to find anything regarding just calling a basic stored proc, thats not linked to a table insert etc.. I'm new to lightswitch, this is the first app i have built so far.

using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using System.Configuration;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;

namespace LightSwitchApplication
{
    public partial class EditableEBiTByYearCountryGrid
    {
        partial void EditableEBiTByYearCountryGrid_Created()
        {
            //Set the defaults for the parameters
            SelectedYear =    this.DataWorkspace.MyData.Years_SingleOrDefault(DateTime.Today.Year);
            SelectedCountry = this.DataWorkspace.MyData.SalesCountries_SingleOrDefault(3);
        }

        partial void ProcessData_Execute()
        {
        //help???
        }

    }
}

Thanks!

Have you seen this blog post? It provides step-by-step instructions on how to execute stored procedures in LightSwitch.

ok, i think i will dumb this down so that someone like me can do this in the future...

It's for a simple stored procedure call, no parameters, and not really related to the screen you're editing.

Note: I have a tables called Year and Month that are reference tables, and not user maintained. Theres no actual data being inserted into the Year Table.

Here's what to put on the Button Execute within the code on your 'screen'

    partial void ProcessData_Execute()
    {
        // Write your code here.
        DataWorkspace dataWorkspace = new DataWorkspace();
        Year operation = dataWorkspace.MyData.Years.AddNew();
        dataWorkspace.MyData.SaveChanges();
    }

To do the next bit you'll need to add a reference to System.Configuration It's described in the link above as

You will have to add a reference from your Server project to the System.Configuration assembly in order to use the ConfigurationManager class. (In the Solution Explorer toolbar, drop down the View toggle and select “File View”. Then right-click the Server project and say “Add Reference”. In the .NET tab, select System.Configuration.)

Within the Data Source 'MyData', right click it and 'View Code'

partial void Years_Inserting(Year entity)
    {
        using (SqlConnection connection = new SqlConnection())
        {
            string connectionStringName = this.DataWorkspace.ProfitData.Details.Name;
            connection.ConnectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;

            string procedure = "usp_DoTask";
            using (SqlCommand command = new SqlCommand(procedure, connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                connection.Open();
                command.ExecuteNonQuery();
            }

        }
        this.Details.DiscardChanges();
    }

And hey presto, it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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