- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLVersion.cs
More file actions
Latest commit
154 lines (133 loc) · 4.93 KB
/
Copy pathSQLVersion.cs
File metadata and controls
154 lines (133 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
usingSystem;
usingSystem.Collections.Generic;
usingSystem.IO;
namespaceSQLBakVersion.Class
{
/// <summary>
/// Main class for SQL Server version detection from BAK files
/// </summary>
publicclassSQLVersion
{
privateconstintTapeHeaderStart=0x45504154;// "TAPE" in little endian
privateconstintMsciMarker=0x4943534D;// "MSCI" in little endian
privatestaticreadonlyDictionary<int,string>VersionMap=newDictionary<int,string>
{
// SQL Server 2005
{611,"SQL Server 2005"},
{612,"SQL Server 2005"},
// SQL Server 2008/R2
{655,"SQL Server 2008"},
{660,"SQL Server 2008"},
{661,"SQL Server 2008"},
// SQL Server 2012
{684,"SQL Server 2012"},
{706,"SQL Server 2012"},
// SQL Server 2014
{782,"SQL Server 2014"},
// SQL Server 2016
{852,"SQL Server 2016"},
// SQL Server 2017
{868,"SQL Server 2017"},
{869,"SQL Server 2017"},
// SQL Server 2019
{895,"SQL Server 2019"},
{896,"SQL Server 2019"},
{897,"SQL Server 2019"},
{902,"SQL Server 2019"},
{904,"SQL Server 2019"},
// SQL Server 2022
{950,"SQL Server 2022"},
{957,"SQL Server 2022"}
};
/// <summary>
/// Get SQL Server version from a BAK file
/// </summary>
/// <param name="filePath">Path to the .bak file</param>
/// <returns>SQL Server version or error message</returns>
publicstringGetVersion(stringfilePath)
{
if(string.Equals(Path.GetExtension(filePath),".mdf",StringComparison.OrdinalIgnoreCase))
{
returnGetMdfVersion(filePath);
}
try
{
using(FileStreamfs=newFileStream(filePath,FileMode.Open,FileAccess.Read))
{
// Step 1: Validate the BAK file header
if(!ValidateTapeHeader(fs))
{
return"Not a valid SQL Server backup file";
}
// Step 2: Find the version marker
if(!FindVersionMarker(fs))
{
return"SQL version information not found";
}
// Step 3: Read the version number
returnDetermineVersion(fs);
}
}
catch(IOExceptionex)
{
return$"Cannot access file: {ex.Message}";
}
catch(Exceptionex)
{
return$"Error: {ex.Message}";
}
}
/// <summary>
/// Gets the SQL Server version recorded in an MDF boot page.
/// </summary>
privatestringGetMdfVersion(stringfilePath)
{
MdfVersionversion=MdfVersionDetector.Detect(filePath);
if(!version.InternalVersion.HasValue)
{
returnstring.IsNullOrEmpty(version.FailureReason)
?"SQL version information not found"
:version.FailureReason;
}
returnversion.Year.HasValue
?string.Format("SQL Server {0}",version.Year.Value)
:string.Format("Unknown SQL Server version (code: {0})",version.InternalVersion.Value);
}
/// <summary>
/// Validates if the file has a valid SQL BAK header
/// </summary>
privateboolValidateTapeHeader(FileStreamfs)
{
byte[]headerBytes=newbyte[4];
if(fs.Read(headerBytes,0,4)!=4)
returnfalse;
returnBitConverter.ToInt32(headerBytes,0)==TapeHeaderStart;
}
/// <summary>
/// Finds the MSCI marker that precedes version info
/// </summary>
privateboolFindVersionMarker(FileStreamfs)
{
byte[]markerBytes=newbyte[4];
while(fs.Read(markerBytes,0,4)==4)
{
if(BitConverter.ToInt32(markerBytes,0)==MsciMarker)
{
returntrue;
}
}
returnfalse;
}
/// <summary>
/// Reads and interprets the SQL Server version number
/// </summary>
privatestringDetermineVersion(FileStreamfs)
{
byte[]versionBytes=newbyte[2];
fs.Seek(168,SeekOrigin.Current);
fs.Read(versionBytes,0,2);
intdbVersion=BitConverter.ToInt16(versionBytes,0);
returnVersionMap.TryGetValue(dbVersion,outstringversion)?version:$"Unknown SQL Server version (code: {dbVersion})";
}
}
}