2012-09-22 220 views
0

每當我創建新數據庫並插入數據時,我都會在我的WinForms應用程序中收到錯誤。SQL Server CE - 數據庫不能爲空

這是我如何創建數據庫

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擴展允許我讀和寫我的數據庫以前,所以可能沒有問題。

+0

我要睡午覺了。整夜持續30小時。 :/ –

+0

這裏提到刪除索引可以解決問題。 [鏈接](http://stackoverflow.com/questions/12511549/the-column-cannot-contain-null-values-sqlce-exception?rq=1) 我應該怎麼做? –

回答

0

自己解決。我忘了啓動用於創建數據庫的連接字符串,而是將連接字符串用於不同的數據庫。愚蠢的我。我現在真的得睡了。

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

+0

兩天內會接受。 StackOverflow限制。 –