SQL Server CE - 数据库不能为空

时间:2012-09-22 01:51:21

标签: database vb.net winforms null sql-server-ce

每当我创建新数据库并插入数据时,我的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桌子上有一把主键时,我没有遇到任何问题,但现在我一直在

  

数据库不能为空,空字符串或仅为字符串   空格。

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

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

1 个答案:

答案 0 :(得分:0)

自己解决了。我忘了启动我用来创建数据库的相同连接字符串,而是使用不同数据库的连接字符串。愚蠢的我。我现在真的要睡觉了。

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