Skip to content

Commit fc4cb43

Browse files
committed
♻️ refactor: make it simple, put logic in one class
1 parent c510666 commit fc4cb43

File tree

7 files changed

+135
-172
lines changed

7 files changed

+135
-172
lines changed

Class/MarkerFinder.cs

Lines changed: 0 additions & 29 deletions
This file was deleted.

Class/SQLVersion.cs

Lines changed: 0 additions & 45 deletions
This file was deleted.

Class/TapeHeaderValidator.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.

Class/VersionDeterminer.cs

Lines changed: 0 additions & 55 deletions
This file was deleted.

Main.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,10 @@ private void ProcessBakFile(string filePath)
8989
{
9090
lblDragDrop.Text = Path.GetFileName(filePath);
9191

92-
string output = string.Empty;
93-
if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
94-
output = "Please select a valid .bak file.";
95-
else
96-
output = sqlVersion.GetVersion(filePath);
97-
98-
lblVersion.Text = output;
99-
if (output.StartsWith("SQL"))
100-
lblVersion.ForeColor = Color.LightGreen;
101-
else
102-
lblVersion.ForeColor = Color.Red;
103-
104-
if (!lblVersion.Visible)
105-
lblVersion.Visible = true;
92+
string versionInfo = File.Exists(filePath) ? sqlVersion.GetVersion(filePath) : "File not found";
93+
lblVersion.Text = versionInfo;
94+
lblVersion.ForeColor = versionInfo.StartsWith("SQL") ? Color.LightGreen : Color.Red;
95+
lblVersion.Visible = true;
10696
}
10797
}
10898
}

SQLBakVersion.csproj

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@
4949
<Reference Include="System.Xml" />
5050
</ItemGroup>
5151
<ItemGroup>
52-
<Compile Include="Class\MarkerFinder.cs" />
53-
<Compile Include="Class\SQLVersion.cs" />
54-
<Compile Include="Class\TapeHeaderValidator.cs" />
55-
<Compile Include="Class\VersionDeterminer.cs" />
52+
<Compile Include="SQLVersion.cs" />
5653
<Compile Include="Main.cs">
5754
<SubType>Form</SubType>
5855
</Compile>

SQLVersion.cs

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
5+
namespace SQLBakVersion.Class
6+
{
7+
/// <summary>
8+
/// Main class for SQL Server version detection from BAK files
9+
/// </summary>
10+
public class SQLVersion
11+
{
12+
private const int TapeHeaderStart = 0x45504154; // "TAPE" in little endian
13+
private const int MsciMarker = 0x4943534D; // "MSCI" in little endian
14+
15+
private static readonly Dictionary<int, string> VersionMap = new Dictionary<int, string>
16+
{
17+
// SQL Server 2005
18+
{ 611, "SQL Server 2005" },
19+
{ 612, "SQL Server 2005" },
20+
21+
// SQL Server 2008/R2
22+
{ 655, "SQL Server 2008" },
23+
{ 660, "SQL Server 2008" },
24+
{ 661, "SQL Server 2008" },
25+
26+
// SQL Server 2012
27+
{ 684, "SQL Server 2012" },
28+
{ 706, "SQL Server 2012" },
29+
30+
// SQL Server 2014
31+
{ 782, "SQL Server 2014" },
32+
33+
// SQL Server 2016
34+
{ 852, "SQL Server 2016" },
35+
36+
// SQL Server 2017
37+
{ 868, "SQL Server 2017" },
38+
{ 869, "SQL Server 2017" },
39+
40+
// SQL Server 2019
41+
{ 895, "SQL Server 2019" },
42+
{ 896, "SQL Server 2019" },
43+
{ 897, "SQL Server 2019" },
44+
{ 902, "SQL Server 2019" },
45+
{ 904, "SQL Server 2019" },
46+
47+
// SQL Server 2022
48+
{ 950, "SQL Server 2022" },
49+
{ 957, "SQL Server 2022" }
50+
};
51+
52+
/// <summary>
53+
/// Get SQL Server version from a BAK file
54+
/// </summary>
55+
/// <param name="filePath">Path to the .bak file</param>
56+
/// <returns>SQL Server version or error message</returns>
57+
public string GetVersion(string filePath)
58+
{
59+
try
60+
{
61+
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
62+
{
63+
// Step 1: Validate the BAK file header
64+
if (!ValidateTapeHeader(fs))
65+
{
66+
return "Not a valid SQL Server backup file";
67+
}
68+
69+
// Step 2: Find the version marker
70+
if (!FindVersionMarker(fs))
71+
{
72+
return "SQL version information not found";
73+
}
74+
75+
// Step 3: Read the version number
76+
return DetermineVersion(fs);
77+
}
78+
}
79+
catch (IOException ex)
80+
{
81+
return $"Cannot access file: {ex.Message}";
82+
}
83+
catch (Exception ex)
84+
{
85+
return $"Error: {ex.Message}";
86+
}
87+
}
88+
89+
/// <summary>
90+
/// Validates if the file has a valid SQL BAK header
91+
/// </summary>
92+
private bool ValidateTapeHeader(FileStream fs)
93+
{
94+
byte[] headerBytes = new byte[4];
95+
if (fs.Read(headerBytes, 0, 4) != 4)
96+
return false;
97+
98+
return BitConverter.ToInt32(headerBytes, 0) == TapeHeaderStart;
99+
}
100+
101+
/// <summary>
102+
/// Finds the MSCI marker that precedes version info
103+
/// </summary>
104+
private bool FindVersionMarker(FileStream fs)
105+
{
106+
byte[] markerBytes = new byte[4];
107+
while (fs.Read(markerBytes, 0, 4) == 4)
108+
{
109+
if (BitConverter.ToInt32(markerBytes, 0) == MsciMarker)
110+
{
111+
return true;
112+
}
113+
}
114+
return false;
115+
}
116+
117+
/// <summary>
118+
/// Reads and interprets the SQL Server version number
119+
/// </summary>
120+
private string DetermineVersion(FileStream fs)
121+
{
122+
byte[] versionBytes = new byte[2];
123+
fs.Seek(168, SeekOrigin.Current);
124+
fs.Read(versionBytes, 0, 2);
125+
int dbVersion = BitConverter.ToInt16(versionBytes, 0);
126+
127+
return VersionMap.TryGetValue(dbVersion, out string version) ? version : $"Unknown SQL Server version (code: {dbVersion})";
128+
}
129+
}
130+
}

0 commit comments

Comments
 (0)