Dobrý den, snažím se o zápis do SQL databáze. A vždy dostanu error: The parameterized query '(@CurrentVLSN nvarchar(4000))SELECT COUNT (*) FROM [SenderLogica' expects the parameter '@CurrentVLSN', which was not supplied. Omlouvám se za nejspíš stupidní dotaz, ale už asi tři dny si nevím rady a pořád zkouším. Tady jsou kódy pro vytvoření tabulek a pro zápis:
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Timers
Imports System.DateTime
Module LogicalDiskInfo
Public Function putSenderLogicalDisksInfo()
'nastavení SQL spojení
Dim ConnectionString As String = System.Configuration.ConfigurationManager.AppSettings("Databaze")
Dim sqlConnection As New SqlClient.SqlConnection(ConnectionString)
Dim sqlcmd As New SqlClient.SqlCommand
Dim CountCurrentVLSN As Integer
Dim mngmtClass As ManagementClass
mngmtClass = New ManagementClass("Win32_LogicalDisk")
Dim mngmtObject As ManagementObject
For Each mngmtObject In mngmtClass.GetInstances()
Dim sqlCommand As New SqlCommand("SELECT COUNT (*) FROM [SenderLogicalDisks] WHERE [SenderLogicalDiskVolumeSerialNumber] = @CurrentVLSN", sqlConnection)
sqlCommand.Parameters.AddWithValue("@CurrentVLSN", mngmtObject("VolumeSerialNumber")) 'pitcho tu je nejaky problemecek
sqlConnection.Open()
CountCurrentVLSN = CInt(sqlCommand.ExecuteScalar())
sqlConnection.Close()
If Not CountCurrentVLSN = 0 Then 'jednotka exituje
sqlcmd = New SqlCommand("UPDATE [SenderLogicalDisks] SET [SenderUUID] = @SenderUUID, [SenderLogicalDiskName] = @SenderLogicalDiskName, [SenderLogicalDiskType] = @SenderLogicalDiskType, [SenderLogicalDiskFileSystem] = @SenderLogicalDiskFileSystem, [SenderLogicalDiskSize] = @SenderLogicalDiskSize, [SenderLogicalDiskFreeSpace] = @SenderLogicalDiskFreeSpace WHERE [SenderLogicalDiskVolumeSerialNumber] = @SenderLogicalDiskVolumeSerialNumber", sqlConnection)
Else 'jednotka neexistuje
sqlcmd = New SqlCommand("INSERT INTO [SenderLogicalDisks] ([SenderUUID], [SenderLogicalDiskName], [SenderLogicalDiskType], [SenderLogicalDiskFileSystem], [SenderLogicalDiskSize], [SenderLogicalDiskFreeSpace], [SenderLogicalDiskVolumeSerialNumber]) VALUES (@SenderUUID, @SenderLogicalDiskName, @SenderLogicalDiskType, @SenderLogicalDiskFileSystem, @SenderLogicalDiskSize, @SenderLogicalDiskFreeSpace, @SenderLogicalDiskVolumeSerialNumber)", sqlConnection)
End If
sqlcmd.Parameters.AddWithValue("@SenderUUID", getLocalUUID())
'sqlcmd.Parameters.AddWithValue("@SenderLogicalDiskName", mngmtObject("Name"))
'sqlcmd.Parameters.AddWithValue("@SenderLogicalDiskType", mngmtObject("DriveType"))
'sqlcmd.Parameters.AddWithValue("@SenderLogicalDiskFileSystem", mngmtObject("FileSystem"))
'sqlcmd.Parameters.AddWithValue("@SenderLogicalDiskSize", mngmtObject("Size"))
'sqlcmd.Parameters.AddWithValue("@SenderLogicalDiskFreeSpace", mngmtObject("FreeSpace"))
sqlcmd.Parameters.AddWithValue("@SenderLogicalDiskVolumeSerialNumber", mngmtObject("VolumeSerialNumber"))
sqlcmd.Parameters.AddWithValue("@SenderLogicalDiskName", "")
sqlcmd.Parameters.AddWithValue("@SenderLogicalDiskType", "")
sqlcmd.Parameters.AddWithValue("@SenderLogicalDiskFileSystem", "")
sqlcmd.Parameters.AddWithValue("@SenderLogicalDiskSize", "")
sqlcmd.Parameters.AddWithValue("@SenderLogicalDiskFreeSpace", "")
sqlConnection.Open()
sqlcmd.ExecuteNonQuery()
sqlConnection.Close()
Next
Return 0
End Function
End Module
Imports System
Imports System.Management
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Module Module1
Sub Main()
Dim ConnectionString As String = System.Configuration.ConfigurationManager.AppSettings("TestovaciDatabaze")
Dim sqlConnection As New SqlClient.SqlConnection(ConnectionString) ' inicializace objektu spojení na databázi
Dim sqlCreateTableSenders As New SqlCommand("CREATE TABLE [Senders] ([SenderID] INT NOT NULL PRIMARY KEY IDENTITY(1,1),[SenderUUID] VARCHAR(100) NOT NULL, [SenderDNSHostName] VARCHAR(100) NOT NULL,[SenderDomain] VARCHAR(100) NOT NULL, [SenderLastContact] DATETIME,[SenderManufacturer] VARCHAR(100) NOT NULL,[SenderModel] VARCHAR(100) NOT NULL,[SenderOS] VARCHAR(50))", sqlConnection)
Dim sqlCreateTableSendersLogicalDisks As New SqlCommand("CREATE TABLE [SenderLogicalDisks] ([SenderLogicalDiskID] INT NOT NULL PRIMARY KEY IDENTITY(1,1),[SenderUUID] VARCHAR(100), [SenderLogicalDiskName] VARCHAR(5),[SenderLogicalDiskType] VARCHAR(100), [SenderLogicalDiskFileSystem] VARCHAR(20),[SenderLogicalDiskSize] BIGINT,[SenderLogicalDiskFreeSpace] BIGINT,[SenderLogicalDiskVolumeSerialNumber] VARCHAR(11))", sqlConnection)
sqlConnection.Open() ' pomocí dat z ConnectionStringu spojení otevřeme
sqlCreateTableSenders.ExecuteNonQuery()
sqlCreateTableSendersLogicalDisks.ExecuteNonQuery()
sqlConnection.Close() ' a zase uzavřeme
Console.Write("Press any key...")
Console.ReadKey()
End Sub
End Module
|