Skip to content

Commit

Permalink
Add DB selection form
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex McCool authored Jul 28, 2016
1 parent a730fc2 commit b31f0a5
Show file tree
Hide file tree
Showing 7 changed files with 288 additions and 8 deletions.
12 changes: 8 additions & 4 deletions SDSetup/StaticGenerator.Binaries.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
<Component Id="cmpE2C13EF5DCCB582D5BA5738AEDBA007D" Guid="*">
<File Id="fil74DDEF1847E91488033A1C826F053C19" KeyPath="yes" Source="$(var.StaticGenerator.TargetDir)\DefaultTemplate.sql" />
</Component>
<Component Id="cmpA6F47B6AB656E3CDDC72CEACF9FFFE73" Guid="*">
<File Id="fil62930B32247048FBDAB011C2E5CF3F87" KeyPath="yes" Source="$(var.StaticGenerator.TargetDir)\Microsoft.Data.ConnectionUI.Dialog.dll" />
</Component>
<Component Id="cmp08A599F537C9B42E7A57B633371807AD" Guid="*">
<File Id="fil34C0D24520E552FA11F67E5B3B4C7E9C" KeyPath="yes" Source="$(var.StaticGenerator.TargetDir)\Microsoft.Data.ConnectionUI.dll" />
</Component>
<Component Id="cmp44E3C853A640B32490A80667A97C5E1D" Guid="*">
<File Id="fil23D7CE083418B666C0CB3704EC205F34" KeyPath="yes" Source="$(var.StaticGenerator.TargetDir)\Static Data Script Generator.exe" />
</Component>
Expand All @@ -21,9 +27,6 @@
<Component Id="cmp84990E56BDCC5720BB2D3AE45AC155DA" Guid="*">
<File Id="fil4CFED195558343C393CFB61EF2507E83" KeyPath="yes" Source="$(var.StaticGenerator.TargetDir)\Static Data Script Generator.vshost.exe.config" />
</Component>
<Component Id="cmpFC8F54BAB84ABB72B41A0155B856F5CF" Guid="*">
<File Id="filECE57EC5077B2DFA30F4B3814AC83C9B" KeyPath="yes" Source="$(var.StaticGenerator.TargetDir)\Static Data Script Generator.vshost.exe.manifest" />
</Component>
<Component Id="cmp7D2C1543D9CD7F0762293898BD6B539C" Guid="*">
<File Id="fil997036E070F87588870E5B084105E712" KeyPath="yes" Source="$(var.StaticGenerator.TargetDir)\StaticGeneratorCommon.dll" />
</Component>
Expand All @@ -36,12 +39,13 @@
<Fragment>
<ComponentGroup Id="Binaries">
<ComponentRef Id="cmpE2C13EF5DCCB582D5BA5738AEDBA007D" />
<ComponentRef Id="cmpA6F47B6AB656E3CDDC72CEACF9FFFE73" />
<ComponentRef Id="cmp08A599F537C9B42E7A57B633371807AD" />
<ComponentRef Id="cmp44E3C853A640B32490A80667A97C5E1D" />
<ComponentRef Id="cmp402581BB4E505DC02ADD144314113554" />
<ComponentRef Id="cmp7F505D14AE1DF17327A17562DB88E646" />
<ComponentRef Id="cmp9BE3443FEAB75BD7FC19430EB26B2689" />
<ComponentRef Id="cmp84990E56BDCC5720BB2D3AE45AC155DA" />
<ComponentRef Id="cmpFC8F54BAB84ABB72B41A0155B856F5CF" />
<ComponentRef Id="cmp7D2C1543D9CD7F0762293898BD6B539C" />
<ComponentRef Id="cmp6CDB32C278DDAEE2F45E21861BB883B8" />
</ComponentGroup>
Expand Down
6 changes: 6 additions & 0 deletions StaticGeneratorCommon/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public static string CreateStaticDataManager(string pstrTableName, string pstrTe
string strTableDef = "DECLARE @tblTempTable TABLE (" + Environment.NewLine;
ArrayList strColumns = new ArrayList();
bool blnHasIdentity = false;
bool blnHasColumnsToUpdate = false;
ArrayList strPrimaryKeyColumns = new ArrayList();
foreach (DataRow drTableField in dtTableSchema.Rows)
{
Expand All @@ -127,6 +128,11 @@ public static string CreateStaticDataManager(string pstrTableName, string pstrTe
if (!drTableField[dtTableSchema.Columns["DataTypeName"]].ToString().Equals("timestamp"))
{
strColumns.Add(drTableField[dtTableSchema.Columns["ColumnName"]].ToString());
// Does this table have a column that is not a key?
if ((bool) drTableField[dtTableSchema.Columns["IsKey"]] == false)
{
blnHasColumnsToUpdate = true;
}
}

// Add to the table definition
Expand Down
216 changes: 216 additions & 0 deletions Win/DatabaseConnection/DataConnectionConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
//------------------------------------------------------------------------------
// <copyright company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;
using System.Xml;

namespace Microsoft.Data.ConnectionUI
{
/// <summary>
/// Provide a default implementation for the storage of DataConnection Dialog UI configuration.
/// </summary>
public class DataConnectionConfiguration : IDataConnectionConfiguration
{
private const string configFileName = @"DataConnection.xml";
private string fullFilePath = null;
private XDocument xDoc = null;

// Available data sources:
private IDictionary<string, DataSource> dataSources;

// Available data providers:
private IDictionary<string, DataProvider> dataProviders;

/// <summary>
/// Constructor
/// </summary>
/// <param name="path">Configuration file path.</param>
public DataConnectionConfiguration(string path)
{
if (!String.IsNullOrEmpty(path))
{
fullFilePath = Path.GetFullPath(Path.Combine(path, configFileName));
}
else
{
fullFilePath = Path.Combine(System.Environment.CurrentDirectory, configFileName);
}
if (!String.IsNullOrEmpty(fullFilePath) && File.Exists(fullFilePath))
{
xDoc = XDocument.Load(fullFilePath);
}
else
{
xDoc = new XDocument();
xDoc.Add(new XElement("ConnectionDialog", new XElement("DataSourceSelection")));
}

this.RootElement = xDoc.Root;
}

public XElement RootElement { get; set; }

public void LoadConfiguration(DataConnectionDialog dialog)
{
dialog.DataSources.Add(DataSource.SqlDataSource);
dialog.DataSources.Add(DataSource.SqlFileDataSource);
dialog.DataSources.Add(DataSource.OracleDataSource);
dialog.DataSources.Add(DataSource.AccessDataSource);
dialog.DataSources.Add(DataSource.OdbcDataSource);
//dialog.DataSources.Add(SqlCe.SqlCeDataSource);

dialog.UnspecifiedDataSource.Providers.Add(DataProvider.SqlDataProvider);
dialog.UnspecifiedDataSource.Providers.Add(DataProvider.OracleDataProvider);
dialog.UnspecifiedDataSource.Providers.Add(DataProvider.OleDBDataProvider);
dialog.UnspecifiedDataSource.Providers.Add(DataProvider.OdbcDataProvider);
dialog.DataSources.Add(dialog.UnspecifiedDataSource);

this.dataSources = new Dictionary<string, DataSource>();
this.dataSources.Add(DataSource.SqlDataSource.Name, DataSource.SqlDataSource);
this.dataSources.Add(DataSource.SqlFileDataSource.Name, DataSource.SqlFileDataSource);
this.dataSources.Add(DataSource.OracleDataSource.Name, DataSource.OracleDataSource);
this.dataSources.Add(DataSource.AccessDataSource.Name, DataSource.AccessDataSource);
this.dataSources.Add(DataSource.OdbcDataSource.Name, DataSource.OdbcDataSource);
//this.dataSources.Add(SqlCe.SqlCeDataSource.Name, SqlCe.SqlCeDataSource);
this.dataSources.Add(dialog.UnspecifiedDataSource.DisplayName, dialog.UnspecifiedDataSource);

this.dataProviders = new Dictionary<string, DataProvider>();
this.dataProviders.Add(DataProvider.SqlDataProvider.Name, DataProvider.SqlDataProvider);
this.dataProviders.Add(DataProvider.OracleDataProvider.Name, DataProvider.OracleDataProvider);
this.dataProviders.Add(DataProvider.OleDBDataProvider.Name, DataProvider.OleDBDataProvider);
this.dataProviders.Add(DataProvider.OdbcDataProvider.Name, DataProvider.OdbcDataProvider);
//this.dataProviders.Add(SqlCe.SqlCeDataProvider.Name, SqlCe.SqlCeDataProvider);


DataSource ds = null;
string dsName = this.GetSelectedSource();
if (!String.IsNullOrEmpty(dsName) && this.dataSources.TryGetValue(dsName, out ds))
{
dialog.SelectedDataSource = ds;
}

DataProvider dp = null;
string dpName = this.GetSelectedProvider();
if (!String.IsNullOrEmpty(dpName) && this.dataProviders.TryGetValue(dpName, out dp))
{
dialog.SelectedDataProvider = dp;
}
}

public void SaveConfiguration(DataConnectionDialog dcd)
{
if (dcd.SaveSelection)
{
DataSource ds = dcd.SelectedDataSource;
if (ds != null)
{
if (ds == dcd.UnspecifiedDataSource)
{
this.SaveSelectedSource(ds.DisplayName);
}
else
{
this.SaveSelectedSource(ds.Name);
}
}
DataProvider dp = dcd.SelectedDataProvider;
if (dp != null)
{
this.SaveSelectedProvider(dp.Name);
}

xDoc.Save(fullFilePath);
}
}

public string GetSelectedSource()
{
try
{
XElement xElem = this.RootElement.Element("DataSourceSelection");
XElement sourceElem = xElem.Element("SelectedSource");
if (sourceElem != null)
{
return sourceElem.Value as string;
}
}
catch
{
return null;
}
return null;
}

public string GetSelectedProvider()
{
try
{
XElement xElem = this.RootElement.Element("DataSourceSelection");
XElement providerElem = xElem.Element("SelectedProvider");
if (providerElem != null)
{
return providerElem.Value as string;
}
}
catch
{
return null;
}
return null;
}

public void SaveSelectedSource(string source)
{
if (!String.IsNullOrEmpty(source))
{
try
{
XElement xElem = this.RootElement.Element("DataSourceSelection");
XElement sourceElem = xElem.Element("SelectedSource");
if (sourceElem != null)
{
sourceElem.Value = source;
}
else
{
xElem.Add(new XElement("SelectedSource", source));
}
}
catch
{
}
}

}

public void SaveSelectedProvider(string provider)
{
if (!String.IsNullOrEmpty(provider))
{
try
{
XElement xElem = this.RootElement.Element("DataSourceSelection");
XElement sourceElem = xElem.Element("SelectedProvider");
if (sourceElem != null)
{
sourceElem.Value = provider;
}
else
{
xElem.Add(new XElement("SelectedProvider", provider));
}
}
catch
{
}
}
}
}
}
21 changes: 21 additions & 0 deletions Win/DatabaseConnection/IDataConnectionConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//------------------------------------------------------------------------------
// <copyright company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.Data.ConnectionUI
{
public interface IDataConnectionConfiguration
{
string GetSelectedSource();
void SaveSelectedSource(string provider);

string GetSelectedProvider();
void SaveSelectedProvider(string provider);
}
}
12 changes: 12 additions & 0 deletions Win/StaticGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@
<ApplicationManifest>Properties\app.manifest</ApplicationManifest>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Data.ConnectionUI, Version=0.0.0.0, Culture=neutral, PublicKeyToken=f4ca07f51760da93, processorArchitecture=MSIL">
<HintPath>..\packages\DataConnectionDialog.1.2\lib\Microsoft.Data.ConnectionUI.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Data.ConnectionUI.Dialog, Version=0.0.0.0, Culture=neutral, PublicKeyToken=f4ca07f51760da93, processorArchitecture=MSIL">
<HintPath>..\packages\DataConnectionDialog.1.2\lib\Microsoft.Data.ConnectionUI.Dialog.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.configuration" />
<Reference Include="System.Core">
Expand All @@ -84,11 +92,14 @@
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\SolutionInfo.cs">
<Link>SolutionInfo.cs</Link>
</Compile>
<Compile Include="DatabaseConnection\DataConnectionConfiguration.cs" />
<Compile Include="DatabaseConnection\IDataConnectionConfiguration.cs" />
<Compile Include="frmGenerate.cs">
<SubType>Form</SubType>
</Compile>
Expand Down Expand Up @@ -119,6 +130,7 @@
<None Include="App.config">
<SubType>Designer</SubType>
</None>
<None Include="packages.config" />
<None Include="Properties\app.manifest" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
Expand Down
25 changes: 21 additions & 4 deletions Win/frmGenerate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Data.SqlClient;
using System.IO;
using System.Collections;
using Microsoft.Data.ConnectionUI;

namespace StaticGenerator
{
Expand Down Expand Up @@ -38,8 +39,24 @@ private void frmGenerate_Load(object sender, EventArgs e)
}
this.chkCreateIndex.Checked = Properties.Settings.Default.LastIndexChoice;

// Populate the table list
LoadTableList();

DataConnectionDialog dcd = new DataConnectionDialog();
DataConnectionConfiguration dcs = new DataConnectionConfiguration(null);
dcs.LoadConfiguration(dcd);

if (DataConnectionDialog.Show(dcd) == DialogResult.OK)
{

Globals.ConnectionString = dcd.ConnectionString;


// Populate the table list
LoadTableList();
}
else
{
this.Close();
}
}

private void frmGenerate_Closing(Object sender, FormClosingEventArgs e)
Expand All @@ -54,8 +71,8 @@ private void LoadTableList()
{
try
{
string strConnectionString = ConfigurationManager.ConnectionStrings["default"].ToString();
Globals.ConnectionString = strConnectionString;
//string strConnectionString = ConfigurationManager.ConnectionStrings["default"].ToString();
//Globals.ConnectionString = strConnectionString;

DataTable dtTableList = new DataTable("Tables");
SqlCommand cdTableList = Globals.Connection.CreateCommand();
Expand Down
4 changes: 4 additions & 0 deletions Win/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DataConnectionDialog" version="1.2" targetFramework="net45" />
</packages>

0 comments on commit b31f0a5

Please sign in to comment.