2015-10-22 46 views
0

我正在使用VB.net,我正在創建一個數據庫。我似乎無法工作的部分是如何讓它有一個密碼。vb.net SQLite密碼

我使用的代碼是:

Imports Finisar.SQLite 


Public Class Form1 
    Const CONNECTION_STR As String = "Data Source=customers.db;Version=3;" 

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     CreateDatabase() 
    End Sub 

    Private Sub CreateDatabase() 
     '=================================================== 
     ' Create a new database and populate it with some data 
     '=================================================== 

     'Declare the main SQLite data access objects 
     Dim objConn As SQLiteConnection 
     Dim objCommand As SQLiteCommand 

     Try 
      'Create a new database connection 
      'Note - use New=True to create a new database 
      objConn = New SQLiteConnection(CONNECTION_STR & "New=True;") 

      'Open the connection 
      objConn.Open() 

      'Create a new SQL command 
      objCommand = objConn.CreateCommand() 

      'Setup and execute the command SQL to create a new table 
      objCommand.CommandText = "CREATE TABLE customer (id integer primary key, name varchar(100));" 
      objCommand.ExecuteNonQuery() 

      'Insert a couple of records into the table 
      objCommand.CommandText = "INSERT INTO customer (id, name) VALUES (1, 'John Smith');" 
      objCommand.ExecuteNonQuery() 
      objCommand.CommandText = "INSERT INTO customer (id, name) VALUES (2, 'Jane Jones');" 
      objCommand.ExecuteNonQuery() 

     Finally 
      'Cleanup and close the connection 
      If Not IsNothing(objConn) Then 
       objConn.Close() 
      End If 
     End Try 

    End Sub 
End Class 

上述工作,並創建數據庫,但我不能讓它有一個密碼時,它會創建它。

我試圖改變這一行:

objConn = New SQLiteConnection(CONNECTION_STR & "New=True;") 

有:

objConn = New SQLiteConnection(CONNECTION_STR & "New=True;Password=myPassword;") 

除了它不能正常工作。我注意到它出現說密碼不是一個有效的參數。

我想知道如何將密碼添加到數據庫,因此需要密碼才能打開它?

編輯: 我這樣做是在VB.net和NOT C#。

在C#中,你可以使用:

SQLiteConnection conn = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;"); 
conn.SetPassword("password"); 
conn.open(); 

但是在VB.net我不會讓我和不能工作了如何做到這一點。

+0

[密碼的可能的複製保護一個SQLite數據庫。是否有可能?](http://stackoverflow.com/questions/1381264/password-protect-a-sqlite-db-is-it-possible) – Plutonix

+0

@Plutonix - 我想在VB.net中做到這一點,而不是C#。 – Aaron

回答

0

我嘗試了很多辦法,對我只有這一個工作:

''create db 
SQLiteConnection.CreateFile(databaseName) 
''sett conn details 
Dim sqlConn As SQLiteConnection = New SQLiteConnection() 
sqlConn.ConnectionString = "DataSource=" & databaseName & ";Version=3;New=False;Compress=True;" 

''open and change password 
sqlConn.Open() 
sqlConn.ChangePassword("hello") 
sqlConn.Close() 

''next time you open db use this code: 
Dim sqlConn As SQLiteConnection = New SQLiteConnection() 
Using Query As New SQLiteCommand() 
    sqlConn.ConnectionString = "DataSource=" & databaseName & ";Version=3;New=False;Compress=True;Password=hello;" 
    sqlConn.Open() 
    With Query 
     .Connection = sqlConn 
     .CommandText = sql 
    End With 
    Query.ExecuteNonQuery() 
    sqlConn.Close() 
End Using