Skip to content

Commit 61e2bf7

Browse files
committed
Option to save grid to SQL Server table
Added context menu option to save the grid to SQL Server table.
1 parent 4e6eaa7 commit 61e2bf7

File tree

8 files changed

+299
-8
lines changed

8 files changed

+299
-8
lines changed

DBADash.sln.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
<s:Boolean x:Key="/Default/UserDictionary/Words/=Topshelf/@EntryIndexedValue">True</s:Boolean>
106106
<s:Boolean x:Key="/Default/UserDictionary/Words/=Trimble/@EntryIndexedValue">True</s:Boolean>
107107
<s:Boolean x:Key="/Default/UserDictionary/Words/=unforce/@EntryIndexedValue">True</s:Boolean>
108+
<s:Boolean x:Key="/Default/UserDictionary/Words/=UNIQUEIDENTIFIER/@EntryIndexedValue">True</s:Boolean>
108109
<s:Boolean x:Key="/Default/UserDictionary/Words/=Updateability/@EntryIndexedValue">True</s:Boolean>
109110
<s:Boolean x:Key="/Default/UserDictionary/Words/=UPGRADESCRIPT/@EntryIndexedValue">True</s:Boolean>
110111
<s:Boolean x:Key="/Default/UserDictionary/Words/=UPGRADESCRIPTPATH/@EntryIndexedValue">True</s:Boolean>

DBADashGUI/CustomReports/DBADashDataGridView.cs

Lines changed: 242 additions & 7 deletions
Large diffs are not rendered by default.

DBADashGUI/ExtensionMethods.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,13 @@ public static SqlParameter Clone(this SqlParameter original)
425425
/// <returns></returns>
426426
public static string SqlSingleQuote(this string value) => value.Replace("'", "''");
427427

428+
/// <summary>
429+
/// Replicates SQL Server QUOTENAME function - wrapping text in square brackets and doubling up on right square bracket. Use SqlSingleQuote for single quotes.
430+
/// </summary>
431+
/// <param name="value"></param>
432+
/// <returns></returns>
433+
public static string SqlQuoteName(this string value) => $"[{value.Truncate(128).Replace("]", "]]")}]";
434+
428435
public static int ToWin32(this Color color) => color.R | (color.G << 8) | (color.B << 16);
429436

430437
public static string GetDescription(this Font font)
@@ -733,5 +740,25 @@ public static Type ToClrType(this SqlDbType sqlDbType)
733740
_ => throw new ArgumentOutOfRangeException(nameof(sqlDbType), $"Unsupported SqlDbType: {sqlDbType}")
734741
};
735742
}
743+
744+
/// <summary>
745+
/// Infers the data type of a DataGridViewColumn based on the first non-null value in the column, or uses ValueType of the column where available.
746+
/// </summary>
747+
/// <param name="column">The DataGridViewColumn to infer the type for.</param>
748+
/// <returns>The inferred data type, or typeof(string) if the column is empty or the DataGridView is not set.</returns>
749+
public static Type InferColumnType(this DataGridViewColumn column)
750+
{
751+
if (column.ValueType != null)
752+
{
753+
return column.ValueType;
754+
}
755+
var dgv = column.DataGridView;
756+
if (dgv == null) return typeof(string);
757+
var firstNonNullValue = dgv.Rows.Cast<DataGridViewRow>()
758+
.Select(row => row.Cells[column.Name].Value)
759+
.FirstOrDefault(value => value != null);
760+
761+
return firstNonNullValue?.GetType() ?? typeof(string);
762+
}
736763
}
737764
}

DBADashGUI/Properties/Resources.Designer.cs

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

DBADashGUI/Properties/Resources.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,4 +316,10 @@
316316
<data name="NewWindow_16x" type="System.Resources.ResXFileRef, System.Windows.Forms">
317317
<value>..\Resources\NewWindow_16x.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
318318
</data>
319+
<data name="SaveTable_16x" type="System.Resources.ResXFileRef, System.Windows.Forms">
320+
<value>..\Resources\SaveTable_16x.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
321+
</data>
322+
<data name="DataTable_16x" type="System.Resources.ResXFileRef, System.Windows.Forms">
323+
<value>..\Resources\DataTable_16x.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
324+
</data>
319325
</root>
320 Bytes
Loading
252 Bytes
Loading

DBADashSharedGUI/DBConnection.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public DBConnection()
1919

2020
public bool ValidateInitialCatalog = false;
2121

22+
public bool InitialCatalogRequired = false;
23+
2224
private SqlAuthenticationMethod SelectedAuthenticationMethod => cboAuthType.SelectedItem is KeyValuePair<SqlAuthenticationMethod, string> selectedPair ? selectedPair.Key : SqlAuthenticationMethod.ActiveDirectoryIntegrated;
2325
private SqlConnectionEncryptOption SelectedEncryptionOption => cboEncryption.SelectedItem is KeyValuePair<SqlConnectionEncryptOption, string> selectedPair ? selectedPair.Key : SqlConnectionEncryptOption.Mandatory;
2426

@@ -160,6 +162,11 @@ public static void TestConnection(string connectionString)
160162

161163
private void BttnConnect_Click(object sender, EventArgs e)
162164
{
165+
if (InitialCatalogRequired && string.IsNullOrEmpty(cboDatabase.Text))
166+
{
167+
MessageBox.Show("Please select a database", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
168+
return;
169+
}
163170
try
164171
{
165172
Cursor = Cursors.WaitCursor;

0 commit comments

Comments
 (0)