How do I upload an Excel sheet to a SQL table in a Lightswitch web client? - sql

I have a lightswitch project in visual studio 2013, using vb.net. I would like the user to be able to click a button and have lightswitch find an excel file, and upload that file to a sql table according to a pre-determined column mapping.
My preference was to use the Office Integration extension for visual studio, which I got working with VS 2013 by downloading it from this link: http://www.ge.tt/71iuRQv/v/0
However, the documentation and examples for office integration seem to be very heavy on getting an excel spreadsheet into a display in the lightswitch web client, rather than into the sql data table, which is where I need it. Here are the examples I've been following:
http://blogs.msdn.com/b/bethmassi/archive/2012/07/18/new-and-improved-office-integration-pack-extension-for-lightswitch.aspx
Alternatively, I have an existing stored procedure, and I can request that the server call this stored proc by sending a web API extension from the client to the server. I have this working already for other stored procs, according to examples from Beth Masi and Paul van Bladel. (Stackoverflow won't let me post the links...)
The undesirable part of this approach is that the stored proc is old, and messy.
I've searched and searched, but have yet to find anyone approach this problem for VS 2013 with lightswitch. Any useful advice?

I've used the method detailed by Matt Sampson to store word files and it works very well. Since it just stores the raw binary it should handle Excel files just as well.
If you're creating the table in LightSwitch, use the Binary Type. If you use SQL create scripts use varbinary(MAX).
You need to create a custom Silverlight dialog box to gain access to the OpenFileDialog object. Then open the file in as a FileStream.
Finally, you need to add a handler for the closed method of the control and then show the control to the user. Most likely done in a button. This needs to be done on the main dispatcher.
The code examples are in C# but I just used one of the many available translators out on the web and copy/pasted the VB.NET code.

Another option might be to consider shelling out to the DTSEXEC run-time to execute an SSIS package to perform the upload - especially if the target Excel spreadsheets have predefined layouts and content data types.
Even simpler, you might be able to use the SQL Server BULK INSERT command to get the job done - although that would require a SQL Client connection to your database.
HTH

Related

Export SQL code from Excel into SQL

My skill level: basic user of VBA, know next to nothing about SQL (though I’ve recently learned how to execute code, etc).
I’ve created a user interface in an Excel worksheet to allow user input of report variables (dates, etc). These pass into a second Excel worksheet that, through the use of various Excel statements, creates the necessary SQL code. My macro then copies this worksheet and opens Microsoft SQL Server 2008 Rx via a shell command. Currently, I then manually click “connect” on the resultant Microsoft SQL Server 2008 R2 connect-to-server window (which does not need a password, just requires me to click “connect”), I create a new query, I paste the code in and then execute in SQL. So it is these latter steps (opening SQL, click “connect”, copy/paste code, execute) that I’m trying to automate via VBA.
To offer more context, once the SQL code has run, I then have a second macro that uses the Excel Data Connection wizard to bring the resultant SQL data back into my worksheet. This is working well.
I’ve researched this for several days on various boards. None seem to observe my approach of creating the code in Excel (probably because it is an inelegant approach). Or the post responses are beyond my skill level to understand. But I do fear that I’m asking a question that has been answered before (apologies in advance).
Thanks for whatever advice/time can be offered.
I’m using:
Excel/Office 2010
Microsoft SQL Server2008 R2 (is this the same as Microsoft SQL Server Management Studios? My SQL application shows both names)
Microsoft ActiveX Data Objects 6.1 Library (this is NOT currently checked off. One post I saw referenced the need for this. I have multiple prior AciveX Library versions available as well.)
What you're doing is very ingenious, but it's probably not necessary. Excel has a number of features for importing external data.
I would look at the Data tab in Excel and the From Other Sources section of the ribbon. You have at least three options: the From SQL Server, From Data Connection Wizard, and From Microsoft Query options.
All of these will set up a connection to a database server and execute SQL against it. They return data to your spreadsheet in different ways.
Where VBA will come into play again is in automating the generation and parameterization of the queries these will execute.
But I would set up something that works, with constants for your parameter values, first, so you get an idea of the possibilities, and then read and research dynamically setting your SQL with VBA.
You might also investigate PowerPivot, by the way.

Create Access databases programatically through vb.net

Let me preface this by saying that I know this is a stupid way to go about this, but it needs to be handled in this way.
I need to make an application that from a master database creates a number of access database files (tables of a larger db), then these are manually given to users who fill in data, the database files are emailed back to a user who, through the application, combines them again.
The only part of this that I am unsure about is problematically creating the access db's. I have read that through Microsoft Jet OLE DB Provider and Microsoft ADO Ext I can create them (the tables and data), but I also need forms.
I have yet to start writing anything and this is away from my area of work, so any insight/links would be much appreciated.
Also, I would hope to be able to write this in the free express version of visual studio. Are there components needed that would prevent me from this? Thanks.
If you are developing in vb.net then why do you need forms in the Access database? Create a vb.net application for the "client" pcs with the required forms reading and writing to the Access database.
If however you do need to have forms in an Access database. Create a database with the required forms and code. Distribute the code/form database and the data database. Add a method to the code/form database the creates links to the tables in the "data" database.

How to communicate ms word and ms access in vb.net?

I want to create a program that could generate reports from an ms access database and place it on ms word, how can I do that. Please help, I dont have any idea on how to do this.
If you're doing this from vb.net, it seems to me that it would be simpler to automate Word, which has no difficulty utilizing Jet/ACE data as data source. I don't see any reason to involve the Access application in this, unless you have complex logic that needs to be executed within Access to get correct results in Word. For instance, Word doesn't do parent/child data display very well, but Access reports do that very easily.
Another question is the final disposition of the results. It may be that you don't need Word at all, e.g., if you're using Word for nothing more than generating PDFs. In that case, it might make more sense to create Access reports and output those to PDF (there's an add-in available for for A2007, and in previous versions, you can use the VB automatable PDFCreator, which is free).
In short, my conclusions would be:
if you're just merging data in Word from a Jet/ACE data store, then automate Word.
if you need to execute complex logic, it's probably better to automate Access and either output Access reports directly, or automate Word from Access.
In neither case, I believe, would you be automating both Word and Access directly from your VB.NET application.
You should connect to the Access Database using a Typed Dataset.
Right-click the project, click Add New Item, select Typed Dataset, and follow the wizard.
To control Word, add a reference to Microsoft.Office.Word and use the Application class.
You can build reports in MS Access that have the Export to Word functionality built into them.
You can invoke the report from your code and then call the export functionality and give it parameters so that it will export to a .doc file that you name. You can then invoke MS Word and open the .doc file up for display.

Writing data back to SQL from Excel sheet

I know it is possible to get data from a SQL database into an excel sheet, but i'm looking for a way to make it possible to edit the data in excel, and after editing, writing it back to the SQL database.
It appears this is not a function in excel, and google didn't come up with much usefull.
If you want to have the Excel file do all of the work (retrieve from DB; manipulate; update DB) then you could look at ActiveX Data Objects (ADO). You can get an overview at:
http://msdn.microsoft.com/en-us/library/ms680928(VS.85).aspx
You want the Import/Export wizard in SQL Management Studio. Depending on which version of SQL Server you are using, open SSMS (connect to the SQL instance you desire), right click on the database you want to import into and select Tasks.. "Import Data".
In the wizard, click Next (past the intro screen) and from the Data Source drop list select "Microsoft Excel". You specify the path and file name of the Excel spreadsheet, whether you have column headings or not.. then press Next. Just follow the wizard through, it'll set up the destination (can be SQL Server or another destination) etc.
There is help available for this process in SQL Server Books Online and more (a walkthrough) from MSDN.
If you need something deployable/more robust (or less wizard driven) then you'd need to take a look at SQL Server Integration Services (for a more "Enterprise" and security conscious approach). It's probably overkill for what you want to accomplish though.
There is a new Excel plug-in named "MySQL for Excel" : http://www.mysql.com/why-mysql/windows/
I just had a need to do this, and this thread has been quiet for a long time, so I thought it might be useful to supply a recent data point.
In my application roving salespeople use a copy of an Excel workbook that tracks the progress of a prospect through a loan application. The current stage of the application needs to be automatically saved back to a remote SQL database so that we can run reporting on it.
Rejected methods for updating the database from Excel:
SSIS and OpenRowSet are both methods for allowing SQL Server to pull the data from Excel, and don't work very well when the Excel workbook is sitting in an undefined location on a user's computer, and certainly not when the workbook is currently open in Excel.
ADO is now, if not actually deprecated, nevertheless looking very long in the tooth. Also, I wanted the solution to be robust in the face of the user possibly not being connected to the internet.
I also considered running a web API on the destination server. Macros in the Excel workbook connect to the web API to transfer data. However, it can sometimes be painful to allow a web API to talk to the outside world. Also, the code to make it robust in the face of temporary loss of internet connection is painful.
The adopted solution:
The solution I plan to adopt is low-tech: email. Excel emails the data to an address hosted on an Exchange server. Everyone in the company has Outlook installed, so the emails are sent by programmatically adding them to the Outlook Outbox. Outlook nicely handles the case when the user is offline. At the server end, a custom C# executable, fired up at regular intervals by the Task Scheduler, polls the inbox and processes the emails.
You could use try these add-ins :
www.QueryCell.com (I created this one)
www.SQLDrill.com
www.Excel-DB.net
You can use the OPENROWSET function to manipulate Excel data from a T-SQL script. Example usage would be:
UPDATE OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;DATABASE=c:\MySpreadsheet.xls',
'Select * from MyTable')
SET Field1='Value1' WHERE Field2 = 'Value2'

List reports with inline SQL in SQL Server Reporting Services?

I have a couple of inline SQL in many of our reports on the report manager.
Now I need to do some code change for all of them, is there a way to list all those reports based on the inline SQL and not any SP?
Thanks,
D
You can use the reporting services API to get the report from the web service and do a search for the appropriate tags in the XML. The MSDN documentation for the report definition XML schema and diagrams can be found here and a diagram of the data source related parts here. The API documentation can be found here.
You can write .Net programs that upload and download reports from the Reporting Services API, using ReportingService.GetReportDefinition and SetReportDefinition. You can upload or download the report definition into a buffer, which can be read or written to disk. Somewhere I have an Ironpython prototype that does this, which I'll post if I find it.
To use this, you need to query the WSDL and generate a stub. The tool to do this is called WSDL.EXE. If I can find it I'll post it here, but you could just as easily use C#.
There is also a tool called rs.exe that comes with BIDS which takes a VB.net program and tops and tails it with includes for the Reporting Services API. You can use this to execute a VB.Net script from the command line - essentially it tops and tails the program, compiles it behind the scenes and runs it against a report server.
You can either do what NXC suggested or if this is a one time activity, you can simply open the RDL file for the report in a text editor and search for common SQL strings like "select", "delete" etc in order to find the reports which have inline SQL