2010-03-05 14 views
4

Je suis l'apprentissage de VB.Net et doivent travailler avec une base de données SQLite en utilisant la solution System.Data.SQLite ADO.Net open sourceINSERT avec transaction et paramètres?

Les exemples que je trouve dans la section HOWTO ne sont en C#. Quelqu'un aurait-il un exemple simple dans VB.Net que je pourrais étudier pour comprendre comment utiliser les transactions lors de l'insertion de plusieurs paramètres?

FWIW, voici le code que je travaille sur:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    Dim SQLconnect As New SQLite.SQLiteConnection() 
    Dim SQLcommand As SQLite.SQLiteCommand 
    Dim SQLtransaction As SQLite.SQLiteTransaction 

    SQLconnect.ConnectionString = "Data Source=test.sqlite;" 
    SQLconnect.Open() 

    SQLcommand = SQLconnect.CreateCommand 

    SQLcommand.CommandText = "CREATE TABLE IF NOT EXISTS files (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, hash TEXT);" 
    SQLcommand.ExecuteNonQuery() 

     '================ INSERT starts here 
    SQLtransaction = SQLconnect.BeginTransaction() 
    Dim myparam As New SQLite.SQLiteParameter() 

    SQLcommand.CommandText = "INSERT INTO [files] ([name],[hash]) VALUES(?,?)" 

    SQLcommand.Parameters.Add(myparam) 

    'How to set all parameters? myparam.Value 

    SQLcommand.ExecuteNonQuery() 
    SQLtransaction.Commit() 
     '================ INSERT ends here 

    SQLcommand.CommandText = "SELECT id,name,hash FROM files" 
    'How to tell if at least one row? 
    Dim SQLreader As SQLite.SQLiteDataReader = SQLcommand.ExecuteReader() 
    While SQLreader.Read() 
     ListBox1.Items.Add(SQLreader(1)) 
    End While 

    SQLcommand.Dispose() 
    SQLconnect.Close() 
End Sub 

Merci.


Edit: Pour les intéressés, voici un code de travail:

SQLtransaction = SQLconnect.BeginTransaction() 
SQLcommand.CommandText = "INSERT INTO files (name,hash) VALUES(@name,@hash)" 
SQLcommand.Parameters.AddWithValue("@name", "myfile") 
SQLcommand.Parameters.AddWithValue("@hash", "123456789") 
SQLcommand.ExecuteNonQuery() 
SQLtransaction.Commit() 

Répondre

0

L'approche de la transaction devrait être la même (fournissant l'API SQLite prend en charge les transactions.) En ce qui concerne plusieurs paramètres, vous devez déclarer une instance de classe SqlParameter pour chaque paramètre, puis ajoutez chacun à la requête.

Dim myparam As New SQLite.SQLiteParameter() 
myparam.Value = "Parameter 1's value" 

Dim myparam2 As New SQLite.SQLiteParameter() 
myparam2.Value = "Parameter 2's value" 

SQLcommand.Parameters.Add(myparam) 
SQLcommand.Parameters.Add(myparam2) 

Quant à votre question propriété « Comment savoir si au moins une rangée » ont une « de HasRows » de la norme .NET SQLReader. c'est-à-dire

If SQLreader.HasRows Then 
    While SQLreader.Read() 
     ListBox1.Items.Add(SQLreader(1)) 
    End While 
End If 

Je présume que le pilote SQLlite devrait aussi bien.

Désolé si ce code n'est pas propre VB, je ne l'ai pas touché dans environ 5 ans!

+0

Merci les gars. J'ai trouvé un exemple qui a utilisé SQLcommand.Parameters.AddWithValue() pour ajouter des éléments à une requête préparée. – Gulbahar

+0

Ah bon à savoir, j'ai toujours fait "SqlCommand.Parameters.Add (new SqlParameter (name, value));" - Je peux me sauver un peu de frappe - merci :) –