{"id":22065719,"url":"https://github.com/karenpayneoregon/microsoftconnectiondialog","last_synced_at":"2025-05-13T01:29:49.615Z","repository":{"id":110840647,"uuid":"216398551","full_name":"karenpayneoregon/MicrosoftConnectionDialog","owner":"karenpayneoregon","description":"Microsoft data connection wizards at runtime","archived":false,"fork":false,"pushed_at":"2023-03-10T21:24:51.000Z","size":1123,"stargazers_count":5,"open_issues_count":2,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-01T05:41:19.571Z","etag":null,"topics":["csharp","csharp-library","dialog","framework48"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/karenpayneoregon.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-20T17:16:43.000Z","updated_at":"2025-03-20T11:06:39.000Z","dependencies_parsed_at":null,"dependency_job_id":"ab4d9bbc-223d-4437-bec1-d56eb1d6b95c","html_url":"https://github.com/karenpayneoregon/MicrosoftConnectionDialog","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karenpayneoregon%2FMicrosoftConnectionDialog","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karenpayneoregon%2FMicrosoftConnectionDialog/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karenpayneoregon%2FMicrosoftConnectionDialog/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karenpayneoregon%2FMicrosoftConnectionDialog/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/karenpayneoregon","download_url":"https://codeload.github.com/karenpayneoregon/MicrosoftConnectionDialog/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253853409,"owners_count":21974106,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","csharp-library","dialog","framework48"],"created_at":"2024-11-30T19:21:36.969Z","updated_at":"2025-05-13T01:29:49.605Z","avatar_url":"https://github.com/karenpayneoregon.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Using Microsoft Visual Studio Connection Dialog at runtime\n\n## Enhancements 02/2023\n\nAdded pre-defined menu items for SQL-Server\n\n![Options](assets/options.png)\n\nUse encryption\n\n![Use Encyption](assets/useEncyption.png)\n\n\n\n\n\u003e **Note**\n\u003e 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.\n\nIn 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.\n\n![Figure 1](assets/Figure1.png)\n\nThis 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.\n\n\n\nThat 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.\n\nTo 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.\n\n![Figure 3](assets/main.png)\n\nPressing \"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.\n\n```csharp\nusing System; \nusing System.Diagnostics; \nusing System.Linq; \n \nnamespace WindowsFormsApplication1_cs \n{ \n    public class Exporter \n    { \n        public void ToCsv(string ServerName, string DatabaseName, string SelectStatement, string FileName) \n        { \n            string DoubleQuote = ((char)(34)).ToString(); \n            string QueryToExceute = DoubleQuote + SelectStatement + DoubleQuote; \n            string ExportFileName = DoubleQuote + FileName + DoubleQuote; \n \n            var Process = new Process(); \n             \n            Process.StartInfo.UseShellExecute = false; \n            Process.StartInfo.RedirectStandardOutput = true; \n            Process.StartInfo.RedirectStandardError = true; \n            Process.StartInfo.CreateNoWindow = true; \n            Process.StartInfo.FileName = \"SQLCMD.EXE\"; \n            Process.StartInfo.Arguments = \"-S \" + ServerName + \" -d \" + DatabaseName + \" -E -Q \" +  \n                QueryToExceute + \" -o \" + ExportFileName + \"  -h-1 -s\\\",\\\" -w 700\"; \n            Console.WriteLine($\"SQLCMD.EXE {Process.StartInfo.Arguments}\"); \n            Process.Start(); \n            Process.WaitForExit(); \n \n            if (System.IO.File.Exists(FileName)) \n            { \n                var contents = System.IO.File.ReadAllLines(FileName) \n                    .Where(line =\u003e !line.ToLower().Contains(\"rows affected\") \u0026\u0026 !string.IsNullOrWhiteSpace(line)).ToArray(); \n                System.IO.File.WriteAllLines(FileName, contents); \n            } \n        } \n    } \n} \n```\n\nThe command for SQLCMD in this case is\nSQLCMD.EXE -S KARENS-PC -d NorthWindAzure -E -Q \"SELECT [ProductID],[ProductName],[UnitsInStock] FROM Products\" -o \"C:\\Data\\test.csv\"  -h-1 -s\",\" -w 700\nPartial contents of the file generate\n\n![Figure 5](assets/Figure5.jpg)\n\n## How to use in your project\nLooking at the Visual Studio solution, the highlighted in yellow get compiled. Unload the projects in grey.\nOpen your Visual Studio solution, add a reference to the DLL created in the solution above and now you can use them.\nThe project WindowsFormsApplication1_cs shows how to use the dialog and also how to do exporting.\n\n![Figure 6](assets/Figure66.jpg)\n\n## IMPORTANT\nThings 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.\n\nNote 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.\n\n![Figure 7](assets/Figure7.jpg)\n\nThe 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.\n\n## Copyrights\nAs I did not create the classes for the Connection Dialog they are copyrighted to Microsoft as each class has a copyright notice within.\n\nOriginal source code was Framework 3.5, Karen updated to Framework 4.8 02/2023.\n\n## 02/2023\n\nAdded use encryption option\n\n![Title](assets/title.png)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkarenpayneoregon%2Fmicrosoftconnectiondialog","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkarenpayneoregon%2Fmicrosoftconnectiondialog","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkarenpayneoregon%2Fmicrosoftconnectiondialog/lists"}