Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net SQLite数据库中的第二条记录未更新_Vb.net_Sqlite - Fatal编程技术网

Vb.net SQLite数据库中的第二条记录未更新

Vb.net SQLite数据库中的第二条记录未更新,vb.net,sqlite,Vb.net,Sqlite,我对VB.Net和SQLite有一个奇怪的问题,还有一个更新过程 小背景首先,我想在VB.Net项目中将一些数据从Derby数据库移动到SQLite数据库 因此,我在Netbeans Derby DB项目中有两项记录,日期分别为2019年12月3日和2019年12月30日 要在VB.Net项目中使用日期设置,我将计算机日期和时间设置回2019年12月3日 在VB.Net SQLite DB项目中没有记录的情况下,我只将Netbeans项目中的文本框多行复制并粘贴到VB.Net项目中,并保存了保存

我对VB.Net和SQLite有一个奇怪的问题,还有一个更新过程
小背景首先,我想在VB.Net项目中将一些数据从Derby数据库移动到SQLite数据库
因此,我在Netbeans Derby DB项目中有两项记录,日期分别为2019年12月3日和2019年12月30日
要在VB.Net项目中使用日期设置,我将计算机日期和时间设置回2019年12月3日
在VB.Net SQLite DB项目中没有记录的情况下,我只将Netbeans项目中的文本框多行复制并粘贴到VB.Net项目中,并保存了保存时应按原样工作的数据
关闭所有未完成的项目,继续将日期和时间设置为2019年12月30日,并重复上述过程。很好,我现在有两个遗留记录
所有项目结束后,我将计算机设置回今天的日期和时间

这是我无法理解的地方
如果我尝试更新2019年12月3日的第一条记录,则更新代码有效
如果我尝试更新2019年12月30日的第二条记录,更新将失败 为了清楚起见,如果我输入了2020年9月的两条记录,并尝试更新其中一条记录,则更新代码即使在不同日期也有效

为什么第二条旧记录没有更新?
您将注意到一个全局变量gv_childInt,little MesBox确认使用了正确的值
用注释掉的更改更新下面的代码,我甚至尝试了WHERE CID=“&gv_childInt

    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click

    If Len(tbTitle.Text.Trim) = 0 Then
        tbMessage.ForeColor = Color.Blue
        tbMessage.Text = "Enter the Title"
        tbTitle.Select()
        Return
    End If
    'If rtbEnter.Text.Trim().Length = 0 Then
    If tb.Text.Trim().Length = 0 Then
        tbMessage.ForeColor = Color.Red
        tbMessage.Text = "Enter Data to Update"
        'rtbEnter.Select()
        'Rewrite code for tb WPF below
        tb.Focus()
        Return

    End If

    MsgBox("Value CID " & gv_childInt)
    Using conn As New SQLiteConnection($"Data Source = '{gv_dbName}';Version=3;")
        conn.Open()
        Using cmd As New SQLiteCommand
            cmd.Connection = conn
            Try
                cmd.CommandText = $"UPDATE ChildTable SET cDispDate = '{tbDispDate.Text.Trim}', cMonth = '{tbMonth.Text.Trim}',cYear = '{tbYear.Text.Trim}',cTitle = '{tbTitle.Text.Trim}', cEntry = '{tb.Text.Trim}' WHERE CID = '{gv_childInt}'"
                'Using cmd As SQLiteCommand = New SQLiteCommand($"UPDATE ChildTable Set cDispDate = '{tbDispDate.Text.Trim}', cMonth = '{tbMonth.Text.Trim}',cYear = '{tbYear.Text.Trim}',cTitle = '{tbTitle.Text.Trim}',cEntry = '{tb.Text.Trim}' WHERE CID = '{gv_childInt}'", conn)
                ' NOTE added tb.Text to line of code above it was rtbEnter.Text.Trim
                '====================================================================

                cmd.Parameters.Add("@cDispDate", CType(SqlDbType.VarChar, DbType)).Value = tbDispDate.Text.Trim
                cmd.Parameters.Add("@cMonth", CType(SqlDbType.VarChar, DbType)).Value = tbMonth.Text.Trim
                cmd.Parameters.Add("@cYear", CType(SqlDbType.VarChar, DbType)).Value = tbYear.Text.Trim
                cmd.Parameters.Add("@cTitle", CType(SqlDbType.VarChar, DbType)).Value = tbTitle.Text.Trim
                'cmd.Parameters.Add("@cEntry", CType(SqlDbType.VarChar, DbType)).Value = rtbEnter.Text.Trim
                'Rewrite code for tb WPF below
                cmd.Parameters.Add("@cEntry", CType(SqlDbType.VarChar, DbType)).Value = tb.Text.Trim
                cmd.ExecuteNonQuery()
                conn.Close()


                'gv_doEdit = False
                'frmStart.Show()
                'Close()
            Catch ex As Exception
                tbMessage.Text = "Child Table Failed"
            End Try
        End Using
    End Using
    gv_doEdit = False
    frmStart.Show()
    Close()
End Sub
因此,该代码也存在问题

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

    If Len(tbTitle.Text.Trim) = 0 Then
        tbMessage.ForeColor = Color.Blue
        tbMessage.Text = "Enter the Title"
        tbTitle.Select()
        Return
    End If
    'If rtbEnter.Text.Trim().Length = 0 Then
    If tb.Text.Trim().Length = 0 Then
        tbMessage.ForeColor = Color.Red
        tbMessage.Text = "Enter Data to SAVE"
        'rtbEnter.Select()
        'Rewrite code for tb WPF below
        tb.Focus()
        Return
    End If

    Using conn As New SQLiteConnection($"Data Source = '{gv_dbName}';Version=3;")
        conn.Open()

        Using cmd As New SQLiteCommand
            cmd.Connection = conn
            Try
                cmd.CommandText = "INSERT INTO ChildTable (cDispDate,cMonth,cYear,cTitle,cEntry) VALUES (@cDispDate,@cMonth,@cYear,@cTitle,@cEntry)"

                cmd.Parameters.Add("@cDispDate", CType(SqlDbType.VarChar, DbType)).Value = tbDispDate.Text.Trim
                cmd.Parameters.Add("@cMonth", CType(SqlDbType.VarChar, DbType)).Value = tbMonth.Text.Trim
                cmd.Parameters.Add("@cYear", CType(SqlDbType.VarChar, DbType)).Value = tbYear.Text.Trim
                cmd.Parameters.Add("@cTitle", CType(SqlDbType.VarChar, DbType)).Value = tbTitle.Text.Trim
                'cmd.Parameters.Add("@cEntry", CType(SqlDbType.VarChar, DbType)).Value = rtbEnter.Text.Trim
                'Rewrite code for tb WPF below
                cmd.Parameters.Add("@cEntry", CType(SqlDbType.VarChar, DbType)).Value = tb.Text.Trim

                cmd.ExecuteNonQuery()
                conn.Close()

                If colMonth = tbMonth.Text Then '<== Need To TEST in September <=== READ
                    frmStart.tbMessage.ForeColor = Color.Blue
                    frmStart.tbMessage.Text = "Record Added"
                    frmStart.Show()
                    Close()
                Else
                    InsertInToParent()
                End If
                'Notice Code Above it only saves to parent if entry is for a new Month
                '======================================================================
            Catch ex As Exception
                tbMessage.Text = "Child Table Failed"
            End Try
        End Using
    End Using

End Sub
Private Sub btnSave\u单击(发送者作为对象,e作为事件参数)处理btnSave。单击
如果Len(tbTitle.Text.Trim)=0,则
tbMessage.ForeColor=Color.Blue
tbMessage.Text=“输入标题”
tbTitle.Select()
返回
如果结束
'如果rtbEnter.Text.Trim().Length=0,则
如果tb.Text.Trim().Length=0,则
tbMessage.ForeColor=Color.Red
tbMessage.Text=“输入要保存的数据”
'rtbEnter.Select()
'重写下面tb WPF的代码
tb.Focus()
返回
如果结束
将conn用作新的SQLiteConnection($“数据源={gv_dbName}';版本=3;”)
康涅狄格州公开赛
使用cmd作为新的SQLiteCommand
cmd.Connection=conn
尝试
cmd.CommandText=“插入子表(cDispDate、cMonth、cYear、cTitle、cEntry)值(@cDispDate、@cMonth、@cYear、@cTitle、@cEntry)”
cmd.Parameters.Add(“@cDispDate”,CType(SqlDbType.VarChar,DbType)).Value=tbDispDate.Text.Trim
cmd.Parameters.Add(“@cMonth”,CType(SqlDbType.VarChar,DbType)).Value=tbMonth.Text.Trim
cmd.Parameters.Add(“@cYear”,CType(SqlDbType.VarChar,DbType)).Value=tbYear.Text.Trim
cmd.Parameters.Add(“@cTitle”,CType(SqlDbType.VarChar,DbType)).Value=tbTitle.Text.Trim
'cmd.Parameters.Add(“@cEntry”,CType(SqlDbType.VarChar,DbType)).Value=rtbEnter.Text.Trim
'重写下面tb WPF的代码
cmd.Parameters.Add(“@cEntry”,CType(SqlDbType.VarChar,DbType)).Value=tb.Text.Trim
cmd.ExecuteNonQuery()
康涅狄格州关闭

如果colMonth=tbMonth.Text,那么“首先,UI事件中发生的事情太多了。请尝试将代码分成逻辑方法,而不是一个大的整体

您可以通过将多个数据库对象组合在一个USE块中来保存缩进。只需用逗号分隔即可。
我将命令文本和连接直接传递给命令的构造函数。您有点遗漏了参数的要点。参数的名称直接用于sql更新字符串。我为子ID添加了一个参数。
对于Sqlite,在.Add方法中为datatype参数使用DbType。Intellisense提供了可用的选项

Private Sub btnUpdate\u Click(发送者作为对象,e作为事件参数)处理btnUpdate。单击
如果没有ValidateUpdateInput(),则
返回
如果结束
MsgBox(“值CID”和gv_childInt)
尝试
Dim RowsUpdated=UpdateDatabase()
特例
MessageBox.Show(例如Message)
结束尝试
如果RowsUpdated=1,则
gv_doEdit=False
frmStart.Show()
关闭()
其他的
MessageBox.Show(“更新失败”)
如果结束
端接头
私有函数ValidateUpdateInput()为布尔值
如果Len(tbTitle.Text.Trim)=0,则
tbMessage.ForeColor=Color.Blue
tbMessage.Text=“输入标题”
tbTitle.Select()
返回错误
如果结束
'如果rtbEnter.Text.Trim().Length=0,则
如果tb.Text.Trim().Length=0,则
tbMessage.ForeColor=Color.Red
tbMessage.Text=“输入要更新的数据”
'rtbEnter.Select()
'重写下面tb WPF的代码
tb.Focus()
返回错误
如果结束
返回真值
端函数
公共函数UpdateDatabase()为整数
作为整数的Dim RetVal
使用conn作为新的SQLiteConnection($“数据源={gv_dbName}';版本=3;”),
cmd作为新的SQLiteCommand(“更新子表集cDispDate=@cDispDate,cMonth=@cMonth,cYear=@cYear,cTitle=@cTitle,cEntry=@cEntry,其中CID=@ID;”,conn)
'使用cmd作为SQLiteCommand=New SQLiteCommand($“UPDATE ChildTable Set cDispDate='{tbdispddate.Text.Trim}',cMonth='{tbMonth.Text.Trim}',cYear='{tbYear.Text.Trim}',cTitle='{tbTitle.Text.Trim}',cEntry='{tb Text.Trim}',其中CID='{gv_childInt}',conn)
'注释将tb.Text添加到其上方的代码行rtbEnter.Text.Trim中
'====================================================================
cmd.Parameters.Add(“@cDispDate”,DbType.String)。Value=tbDispDate.Text.Trim
cmd.Parameters.Add(“@cmmonth”,DbType.String)。Value=tbMonth.Text.Trim
cmd.Parameters.Add(“@cYear”,DbType.String).Value=tbYear.Text.Trim
cmd.Parameters.Add(“@cTitle”,DbType.String).Value=tb