0

每当我创建新数据库并插入数据时,我的 WinForms 应用程序都会不断出错。

这就是我创建数据库的方式

Public Function CreateDatabase() As String
    Dim ds As String = "Data Source=" + (My.Computer.FileSystem.SpecialDirectories.MyDocuments) + "\" + (Application.ProductName) + "\" + (DBName) + ".wv" + ";Persist Security Info=True;"
    Dim Conn As New SqlCeConnection(ds)
    Dim Cmd1 As New SqlCeCommand()
    Dim Cmd2 As New SqlCeCommand()
    Dim SqlCeDB As New SqlCeEngine(ds)
    Dim Msg As String
    Try
        If Not System.IO.File.Exists(DBName) Then
            Msg = "Database successfully created in " & Application.StartupPath
            SqlCeDB.CreateDatabase()
            Cmd1.Connection = Conn
            Conn.Open()
            'Create acPassword Table
            Cmd1.CommandText = "CREATE TABLE [wvAccount](" & _
                                "[acName] [nvarchar](100) NOT NULL, " & _
                                "[acPassword] [nvarchar](100) NOT NULL," & _
                                "[acKeyFile] [nvarchar](64) NULL, " & _
                                "[acLastLogin] [nvarchar](100) NULL, " & _
                                "[acLastModified] [nvarchar](100) NULL)"
            Cmd1.ExecuteNonQuery()
            'Create pmPassword Table
            Cmd2.Connection = Conn
            Cmd2.CommandText = "CREATE TABLE [wvPassword](" & _
                                "[pmID] [int] CONSTRAINT pmID PRIMARY KEY NOT NULL, " & _
                                "[pmCat] [nvarchar](100) NOT NULL, " & _
                                "[pmTitle] [nvarchar](100) NOT NULL," & _
                                "[pmUsername] [nvarchar](100) NOT NULL," & _
                                "[pmSiteURL] [nvarchar](100) NULL," & _
                                "[pmNotes] [nvarchar](300) NULL)"
            Cmd2.ExecuteNonQuery()
        Else
            Msg = "There is already a Database with that name."
        End If
    Catch ex As Exception
        Msg = ex.Message
    End Try
    Return Msg
End Function

这就是我在创建数据库并填充表后立即插入数据的方式

Private Sub ADD_PA()
    Dim StrSQL_vault As String = "INSERT INTO [wvAccount] ([acName], [acPassword], [acKeyFile], [acLastLogin], [acLastModified]) VALUES (@acName, @acPassword, @acKeyFile, @acLastLogin, @acLastModified)"
    classlibrary_vault = New ConnectionLibrary_Vault
    classlibrary_vault.openConnection_vault(strconnection_vault)
    If Not classlibrary_vault.isConnectionOpen_vault() Then
        Exit Sub
    End If
    classlibrary_vault.initializeCommand_vault(StrSQL_vault)
    classlibrary_vault.addParameter_vault("@acName", SqlDbType.NVarChar, txtName.TextLength, txtName.Text)
    classlibrary_vault.addParameter_vault("@acPassword", SqlDbType.NVarChar, txtEncryptedCode.TextLength, txtEncryptedCode.Text)
    classlibrary_vault.addParameter_vault("@acKeyFile", SqlDbType.NVarChar, txtKeyFile.TextLength, txtKeyFile.Text)
    classlibrary_vault.addParameter_vault("@acLastLogin", SqlDbType.NVarChar, txtLastLogin.TextLength, txtLastLogin.Text)
    classlibrary_vault.addParameter_vault("@acLastModified", SqlDbType.NVarChar, txtLastModified.TextLength, txtLastModified.Text)
    Try
        objcommand_vault.ExecuteNonQuery()
        txtAccountName.Text = txtName.Text
        'Set controls
        _Main.NotifyIcon.Visible = True
        _Main.NotifyIcon.ShowBalloonTip(1, Application.ProductName, VAULT_CREATED, ToolTipIcon.Info)
        Me.ParentForm.Controls.Remove(Me)
        controlManagePasswords()
    Catch ex As Exception
        DevComponents.DotNetBar.MessageBoxEx.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Try
End Sub

起初,当我的wvAccount桌子上有一个主键时,我没有遇到任何问题,但现在我不断收到

数据库不能为 null、空字符串或只有空格的字符串。

我创建数据库没有问题,唯一困扰我的是当我尝试在wvAccount. 顺便说一句,我正在使用 SQL Server CE 4.0 和 VB.Net

.wv扩展允许我以前在我的数据库上读写,所以可能没有问题。

4

1 回答 1

0

自己解决了。我忘记启动用于创建数据库的相同连接字符串,而是将连接字符串用于不同的数据库。让我哑口无言。我现在真的要睡觉了。

Dim ds As String = "Data Source=" + (My.Computer.FileSystem.SpecialDirectories.MyDocuments) + "\" + (Application.ProductName) + "\" + (DBName) + ".wv" + ";Persist Security Info=True;"

于 2012-09-22T02:19:36.993 回答