如何使用sqlite将.dat文件导入VB,net [英] How do I import .dat file to VB, net using sqlite

查看:237
本文介绍了如何使用sqlite将.dat文件导入VB,net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码将.dat文件导入SQLite表

我的连接

 公共 strConn 作为 字符串 =  数据源= DTS.db;版本= 3; 



我的程序

 私有  Sub  btnImportDB_Click(发件人< span class =code-keyword>作为 对象,e  As  EventArgs)句柄 btnImportDB.Click 

如果 txtDatFile.Text = 字符串 .Empty 然后 退出 Sub

Dim fname As String = txtDatFile.Text
Dim colsexpected 作为 整数 = 6
Dim Filereader As StreamReader( fname,编码。默认
Dim sLine 正如 字符串 = 字符串 .Empty

使用 MyConn 作为 SQLiteConnection(strConn)
执行
sLine = Filereader.ReadLine
如果 sLine Nothing 然后 退出 执行
Dim vArray()作为 字符串 = sLine.Split( CType (vbTab,< span class =code-keyword> Char ()))
' 将数据保存到表
Dim InsertData 作为 String = INSERT INTO tblTempData(fldMacId,fldDateTime,fldRem1,fldRem2,fldRem3,fldRem4)值(@ MacId,@ xDateTime,@ Rem1,@ Rem2,@ Rem3,@ Rem4)
Dim cmd 作为 新 SQLiteCommand(InsertData,MyConn)
cmd.Parameters.AddWithValue( @ MacId,vArray( 0 ))
cmd.Parameters.AddWithValue( @ xDateTime,vArray( 1 ))
cmd。 Parameters.AddWithValue( @ Rem1,vArray( 2 ))
cmd.Parameters.AddWithValue( @ Rem2,vArray ( 3 ))
cmd.Parameters.AddWithValue( @ Rem3,vArray( 4 ))
cmd.Parameters.AddWithValue( <跨度class =code-string> @ Rem4,vArray( 5 ))
尝试
使用 MyConn
如果 .State = ConnectionState.Open < span class =code-keyword>然后 .Close()
.ConnectionString = strConn
.Open()
MyConn.Open()
cmd.ExecuteNonQuery ()
Cursor.Current = Cursors.WaitCursor
' MsgBox(连接已打开)
结束 使用
Catch ex As Exception
MessageBox.Show(ex.Message)
结束 尝试

循环
结束 使用
MessageBox.Show( 记录成功导入。 Save,MessageBoxButtons.OK,MessageBoxIcon.Information)
txtDatFile.Clear()
End Sub





这是错误

 { 由于对象的当前状态,操作无效。} 





我尝试过:



我无法找到导致错误的部分..

解决方案

移动e。打开循环调用。你只需要在开始循环之前调用它。



另外,移动代码来创建你的SQLiteCommand对象,它的参数不在循环中。创建它ONCE并重用它!不要使用.AddWithValue,而是使用.Add来描述参数。



在循环中,你可以设置参数对象的值。

Im using this code to import .dat file to SQLite Table
My connection

Public strConn As String = "Data Source =DTS.db;Version=3;"


My Procedure

 Private Sub btnImportDB_Click(sender As Object, e As EventArgs) Handles btnImportDB.Click

        If txtDatFile.Text = String.Empty Then Exit Sub

        Dim fname As String = txtDatFile.Text
        Dim colsexpected As Integer = 6
        Dim Filereader As New StreamReader(fname, Encoding.Default)
        Dim sLine As String = String.Empty

        Using MyConn As New SQLiteConnection(strConn)
            Do
                sLine = Filereader.ReadLine
                If sLine Is Nothing Then Exit Do
                Dim vArray() As String = sLine.Split(CType(vbTab, Char()))
                'Save data to table
                Dim InsertData As String = "INSERT INTO tblTempData (fldMacId,fldDateTime,fldRem1,fldRem2,fldRem3,fldRem4) Values (@MacId,@xDateTime,@Rem1,@Rem2,@Rem3,@Rem4)"
                Dim cmd As New SQLiteCommand(InsertData, MyConn)
                cmd.Parameters.AddWithValue("@MacId", vArray(0))
                cmd.Parameters.AddWithValue("@xDateTime", vArray(1))
                cmd.Parameters.AddWithValue("@Rem1", vArray(2))
                cmd.Parameters.AddWithValue("@Rem2", vArray(3))
                cmd.Parameters.AddWithValue("@Rem3", vArray(4))
                cmd.Parameters.AddWithValue("@Rem4", vArray(5))
                Try
                    With MyConn
                        If .State = ConnectionState.Open Then .Close()
                        .ConnectionString = strConn
                        .Open()
                        MyConn.Open()
                        cmd.ExecuteNonQuery()
                        Cursor.Current = Cursors.WaitCursor
                        'MsgBox("Connection is  Open")
                    End With
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                End Try

            Loop
        End Using
        MessageBox.Show("Records successfully imported.", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information)
        txtDatFile.Clear()
End Sub



This is the error

{"Operation is not valid due to the current state of the object."}



What I have tried:

I cant find which part cause the error..

解决方案

Move the .Open call out of the loop. You only need to call it ONCE, before you start the loop.

Also, Move the code to create your SQLiteCommand object and it's parameters out of the loop. Create it ONCE and reuse it! Do NOT use .AddWithValue, use .Add instead and just describe the parameter.

Inside the loop, is where you set the values of your parameter objects.


这篇关于如何使用sqlite将.dat文件导入VB,net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆