https://github.com/karenpayneoregon/microsoftconnectiondialog
Microsoft data connection wizards at runtime
https://github.com/karenpayneoregon/microsoftconnectiondialog
csharp csharp-library dialog framework48
Last synced: about 1 year ago
JSON representation
Microsoft data connection wizards at runtime
- Host: GitHub
- URL: https://github.com/karenpayneoregon/microsoftconnectiondialog
- Owner: karenpayneoregon
- Created: 2019-10-20T17:16:43.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-03-10T21:24:51.000Z (over 3 years ago)
- Last Synced: 2025-04-01T05:41:19.571Z (over 1 year ago)
- Topics: csharp, csharp-library, dialog, framework48
- Language: C#
- Homepage:
- Size: 1.07 MB
- Stars: 5
- Watchers: 1
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Using Microsoft Visual Studio Connection Dialog at runtime
## Enhancements 02/2023
Added pre-defined menu items for SQL-Server

Use encryption

> **Note**
> Original code from Microsoft was in .NET Framework 3.5, Karen updated to .NET Framework 4.8. If using .NET Core it would be a time consuming effort to move to .NET Core, mainly the user interfaces.
In Visual Studio when a developer wants to create strong typed classes for database tables either for the conventional TableAdapter or Entity Framework there is a place in the process where a dialog is displayed as shown below. I will show you how to do this at runtime and a bit more.

This code sample will show you the basics of using this dialog by working with the source code. In the next screenshot I have modified the source code so that any time SQL-Server data provider is used and it's my login I get a context menu item that allows me to auto fill the Server Name.
That is cool but we will now take advantage of this dialog so that (in this code sample) can create a SQL SELECT statement or create a CSV file for a table.
To demonstrate this I created a simple windows forms project with the following interface to select a server, select a table, select columns. After making these selections pressing "Generate SQL SELECT" you get the following in the second image below.

Pressing "Export CSV" takes the SQL SELECT statement and passes it off to a process which uses SQLCMD.EXE. My Exporter class takes information obtained from the Connection Dialog and selections done in the form to create a CSV file.
```csharp
using System;
using System.Diagnostics;
using System.Linq;
namespace WindowsFormsApplication1_cs
{
public class Exporter
{
public void ToCsv(string ServerName, string DatabaseName, string SelectStatement, string FileName)
{
string DoubleQuote = ((char)(34)).ToString();
string QueryToExceute = DoubleQuote + SelectStatement + DoubleQuote;
string ExportFileName = DoubleQuote + FileName + DoubleQuote;
var Process = new Process();
Process.StartInfo.UseShellExecute = false;
Process.StartInfo.RedirectStandardOutput = true;
Process.StartInfo.RedirectStandardError = true;
Process.StartInfo.CreateNoWindow = true;
Process.StartInfo.FileName = "SQLCMD.EXE";
Process.StartInfo.Arguments = "-S " + ServerName + " -d " + DatabaseName + " -E -Q " +
QueryToExceute + " -o " + ExportFileName + " -h-1 -s\",\" -w 700";
Console.WriteLine($"SQLCMD.EXE {Process.StartInfo.Arguments}");
Process.Start();
Process.WaitForExit();
if (System.IO.File.Exists(FileName))
{
var contents = System.IO.File.ReadAllLines(FileName)
.Where(line => !line.ToLower().Contains("rows affected") && !string.IsNullOrWhiteSpace(line)).ToArray();
System.IO.File.WriteAllLines(FileName, contents);
}
}
}
}
```
The command for SQLCMD in this case is
SQLCMD.EXE -S KARENS-PC -d NorthWindAzure -E -Q "SELECT [ProductID],[ProductName],[UnitsInStock] FROM Products" -o "C:\Data\test.csv" -h-1 -s"," -w 700
Partial contents of the file generate

## How to use in your project
Looking at the Visual Studio solution, the highlighted in yellow get compiled. Unload the projects in grey.
Open your Visual Studio solution, add a reference to the DLL created in the solution above and now you can use them.
The project WindowsFormsApplication1_cs shows how to use the dialog and also how to do exporting.

## IMPORTANT
Things to watch out for concerning SQLCMD.EXE, if you attempt to create a export file and get error messages the common reason is that there is an authenication issue which you need to figure out the login and security which is outside the scope of this code sample.
Note in Operations class, GetConnection that the optional parameter SaveConfiguration is false, setting it to true will create a xml file names DataConnections.xml which save the last data provider used and will be the default each time until you change it.

The two things the connection dialog is good for is when writing code in development and also for when a user at runtime needs to select an alternate server. Needless to say there are many possibilities here.
## Copyrights
As I did not create the classes for the Connection Dialog they are copyrighted to Microsoft as each class has a copyright notice within.
Original source code was Framework 3.5, Karen updated to Framework 4.8 02/2023.
## 02/2023
Added use encryption option
