Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions ExportLicense/ExportLicense/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
mc:Ignorable="d"
WindowStyle="SingleBorderWindow"
ResizeMode="NoResize"
Title="Erik's Tools: Export NAV License from SQL Database" Height="280" Width="525">
Title="Erik's Tools: Export NAV License from SQL Database" Height="380" Width="525">
<Grid>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal" Margin="5">
Expand All @@ -33,8 +33,9 @@
<ComboBoxItem>Database</ComboBoxItem>
</ComboBox>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="5">
<Button Width="300" Margin="200,0,0,0" Height="25" Click="Button_Click">Export License</Button>
<StackPanel Orientation="Vertical" Margin="5" HorizontalAlignment="Left">
<Button Width="300" Margin="200,0,0,5" Height="25" Click="ButtonExportLicense_Click">Export License</Button>
<Button Width="300" Margin="200,0,0,0" Height="25" Click="ButtonImportLicense_Click">Import License</Button>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="5">
<TextBlock Margin="200,0,0,0"><Hyperlink RequestNavigate="Hyperlink_RequestNavigate" NavigateUri="http://www.hougaard.com">More Info at: Hougaard.com - Applied Hacking</Hyperlink></TextBlock>
Expand Down
86 changes: 61 additions & 25 deletions ExportLicense/ExportLicense/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,21 @@ public MainWindow()
DataContext = this;
combo.SelectedIndex = 0;
}

// Model
public string SQLServer { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string Database { get; set; }
public SQLType ConnectionType { get; set; }

private void Button_Click(object sender, RoutedEventArgs e)
private void ButtonExportLicense_Click(object sender, RoutedEventArgs e)
{
try
{
string table;
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = SQLServer;
if (string.IsNullOrEmpty(Database))
{
builder.InitialCatalog = "master";
table = "[$ndo$srvproperty]";
}
else
{
builder.UserID = UserName;
builder.Password = Password;
builder.InitialCatalog = Database;
table = "[$ndo$dbproperty]";
}
if (ConnectionType == SQLType.Windows)
builder.IntegratedSecurity = true;
SqlConnection Con = new SqlConnection(builder.ConnectionString);
Con.Open();
SqlCommand cmd = new SqlCommand("SELECT license from "+table, Con);
{
string table = "";
SqlConnection con = InitializeConnection(ref table);
SqlCommand cmd = new SqlCommand("SELECT license from " + table, con);
SqlDataReader data = cmd.ExecuteReader();
while (data.Read())
{
Expand All @@ -75,16 +58,69 @@ private void Button_Click(object sender, RoutedEventArgs e)
ms.Position = 0;
ms.CopyTo(fs);
fs.Close();
MessageBox.Show("Succes, exported as " + sfd.FileName);
MessageBox.Show("Success, exported as " + sfd.FileName);
}
}
}
catch(Exception ex)
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}

private void ButtonImportLicense_Click(object sender, RoutedEventArgs e)
{
try
{
string table = "";
SqlConnection con = InitializeConnection(ref table);

OpenFileDialog openFileDlg = new OpenFileDialog();
openFileDlg.Filter = "License|*.flf";
if (openFileDlg.ShowDialog() == true)
{
byte[] bytes = System.IO.File.ReadAllBytes(openFileDlg.FileName);
SqlCommand cmd = new SqlCommand("UPDATE " + table + " SET license = @File", con);
var param = cmd.Parameters.Add("@File", System.Data.SqlDbType.Image, bytes.Length);
param.Value = bytes;
cmd.ExecuteNonQuery();
MessageBox.Show("Success, imported " + openFileDlg.FileName);
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
}

private SqlConnection InitializeConnection(ref string table)
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = SQLServer;
if (string.IsNullOrEmpty(Database))
{
builder.InitialCatalog = "master";
table = "[$ndo$srvproperty]";
}
else
{
builder.InitialCatalog = Database;
table = "[$ndo$dbproperty]";
}
if (ConnectionType == SQLType.Windows)
{
builder.IntegratedSecurity = true;
}
else
{
builder.UserID = UserName;
builder.Password = Password;
}
SqlConnection con = new SqlConnection(builder.ConnectionString);
con.Open();
return con;
}

private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
Expand Down