SQLDMO(testing)2
‘Connecting using SQL Authentication3
Dim objDMO As SQLServer4
objDMO = New SQLDMO.SQLServer5
objDMO.Connect("(local)", "sa", "of_course_I_changed_my_password")6
Dim objSQLServer As SQLDMO.SQLServer7
objSQLServer = New SQLDMO.SQLServer8
objSQLServer.LoginSecure = True9
Call objSQLServer.Connect("cnmtldv01psge")10

11
Console.WriteLine(objSQLServer.Name())12
Console.WriteLine(objSQLServer.HostName())13
Console.WriteLine(objSQLServer.Status())14

15
display SpaceAvailableInMB for each Database16
Chr(9): tab; chr(13): carriage return; chr(10): line feed17
Dim oDatabase As SQLDMO.Database18
For Each oDatabase In objSQLServer.Databases19
Console.WriteLine(oDatabase.Name & Chr(9) & oDatabase.SpaceAvailableInMB & Chr(13))20
Next21

22
‘create a database23
Dim nDatabase As New SQLDMO.Database24
Dim nDBFileData As New SQLDMO.DBFile25
Dim nLogFile As New SQLDMO.LogFile26
Dim databaseName As String27
databaseName = "zzz"28
nDatabase.Name = databaseName29
nDBFileData.Name = databaseName30
nDBFileData.PhysicalName = "D:\\sqldata\\MSSQL\\Data\\" & databaseName & "_Data.mdf"31
nDBFileData.PrimaryFile = True32
nDBFileData.Size = 233
nDBFileData.MaximumSize = 1034
nDBFileData.FileGrowthType = SQLDMO.SQLDMO_GROWTH_TYPE.SQLDMOGrowth_Percent35
nDBFileData.FileGrowth = 1036
nDatabase.FileGroups.Item("PRIMARY").DBFiles.Add(nDBFileData)37
nLogFile.Name = databaseName + "Log"38
nLogFile.PhysicalName = "D:\\sqldata\\MSSQL\\Data\\" & databaseName + "_Log.ldf"39
nLogFile.Size = 240
nLogFile.MaximumSize = 541
nDatabase.TransactionLog.LogFiles.Add(nLogFile)42
objSQLServer.Databases.Add(nDatabase)43

44
Update(AutoUpdateStat)45
Dim objDBz As SQLDMO._Database46
objDBz = objSQLServer.Databases.Item("zxx")47
objdbz.Owner as string48
objDBz.CreateDate as string49
objdbz.Size readonly as integer50
objdbz.SpaceAvailableInMB as single51
If objDBz.DBOption.AutoUpdateStat = False Then52
objDBz.DBOption.AutoUpdateStat = True53
End If54

55
‘query the database56
Dim oTheResults As SQLDMO.QueryResults57
oTheResults = objDBz.ExecuteWithResults("SELECT * FROM Table1") ‘Length of the Batch. For example, we are currently using 50 characters.58
Dim numOfCols As Integer59
numOfCols = oTheResults.Columns60
Dim num As Integer61
For num = 1 To oTheResults.Rows62
Dim j As Integer63
For j = 1 To numOfCols64
Console.Write(oTheResults.GetColumnString(num, j))65
Console.Write(Chr(9))66
Next67
Console.Write(Chr(10))68
Next69

70
‘display tables inside a database; Assuming, we have set up our connection.71
Dim objDB As SQLDMO.Database72
objDB = objSQLServer.Databases.Item("northwind")73
Dim oTable As SQLDMO.Table74
For Each oTable In objDB.Tables75
‘Console.WriteLine(oTable.Name)76
Next77

78

79
‘scripting: Assuming, we have set up our connection.80
Dim oTable2 As SQLDMO.Table81
oTable2 = objDB.Tables.Item("employees")82
‘Console.WriteLine("Script of table employees: " & oTable2.Script())83

84
‘display indexed columns85
Dim indexcolname As String86
Dim idxcnt As Integer87
Dim idxloops As Integer88
Dim colCount As Integer89
Dim idxObj As SQLDMO.Index90
Dim idxcol As SQLDMO.Column91
idxcnt = 092
objDB = objSQLServer.Databases.Item("zxx")93
Dim objTable As SQLDMO.Table94
objTable = objDB.Tables.Item("Table1")95
idxcnt = objTable.Indexes.Count ‘ number of indexes in the table96
Dim i As Integer97
For i = 1 To idxcnt98
idxObj = objTable.Indexes.ItemByID(i) ‘one-based (instead of zero based)99
colCount = idxObj.ListIndexedColumns.Count ‘number of cols in this index100
Dim j As Integer101
For j = 1 To colCount102
idxcol = idxObj.ListIndexedColumns.Item(j)103
‘Console.WriteLine("index Name: " & idxObj.Name & " __and col Name of it: " & idxcol.Name)104
Next105
Next106

107
‘display and update config values108
Dim cv As SQLDMO.ConfigValue109
With objSQLServer.Configuration110
.ShowAdvancedOptions = True111
For Each cv In .ConfigValues112
‘Console.WriteLine(cv.Name & "_" & cv.CurrentValue & "_" & cv.RunningValue)113
Next114
End With115

116
following is not tested117
allow changes to system tables118
With SQLServer.Configuration119
.ConfigValues("allow updates").CurrentValue = 1120
.ReconfigureWithOverride()121
End With122

123
display table information124
Dim col2 As SQLDMO.Column125
col2 = objTable.Columns.ItemByID(1)126
Dim ie As IEnumerator = col2.Datatype.GetEnumerator()127
While ie.MoveNext128
Console.WriteLine(ie.Current.ToString())129
End While130

131
objSQLServer.DisConnect()132

133
Console.WriteLine(goSQLApp.SQLServers.Item("(local)").Databases.Item("zxx").Tables.ItemByID(0).Name())

