【问题标题】:Vb.net data misplacedvb.net数据错位
【发布时间】:2013-02-02 10:00:33
【问题描述】:

使用 Visual Studio 2010 我正在开发一个 vb.net 应用程序,我需要在一个表中存储超过 10000 条记录。表格格式为

|empno |year |january|february| march |april |may |june |july |august |September |october |november  |december| total|

样本数据:

101 2010 10000 15000 25000
101 2011 15000 20000 35000
101 2012 20000 25000 45000
101 2013 25000 30000 55000
102 2008 1000  1500  2500
102 2009 1500  2000  3500
102 2010 10000 15000 25000
102 2011 15000 20000 35000
102 2012 20000 25000 45000
102 2013 25000 30000 55000

现在的问题是当我将数据插入此表时,它没有正确存储。我的意思是,它似乎是

102 2012 20000 25000 45000
101 2010 10000 15000 25000
101 2011 15000 20000 35000
101 2012 20000 25000 45000
101 2013 25000 30000 55000
102 2008 1000  1500  2500
102 2009 1500  2000  3500
102 2010 10000 15000 25000
102 2011 15000 20000 35000
102 2013 25000 30000 55000

在每组数据中,有一个年份的记录存储在表格的顶部。因此,当我显示特定员工的记录时,它无法正确显示(我的意思是不按顺序显示)。例如:对于empno 102,结果是这样的

102 2012 20000 25000 45000
102 2008 1000  1500  2500
102 2009 1500  2000  3500
102 2010 10000 15000 25000
102 2011 15000 20000 35000
102 2013 25000 30000 55000

我认为按年份排序不是解决方案。因为,它一年有很多记录。

我检查了所有记录。只有一些记录有这个问题。我对这个问题以及如何解决它一无所知。任何人都可以这是什么原因以及如何避免?

提前致谢

编辑:代码

If yodor1 <= 2015 Then
            up = 2015
        ElseIf yodor1 <= 2025 Then
            up = 2025
        ElseIf yodor1 <= 2035 Then
            up = 2035
        ElseIf yodor1 <= 2045 Then
            up = 2045
        End If

        con.Open()
        txt = "select * from pension where empno='" & TextBox5.Text & "' "
        cmd = New SqlCommand(txt, con)
        reader = cmd.ExecuteReader()
        While reader.Read
            If yodor1 <= up Then
                flag = False
                display2.grid1.set_TextMatrix(a, 0, reader("year"))
                display2.grid1.set_TextMatrix(a, 1, reader("january"))
                display2.grid1.set_TextMatrix(a, 2, reader("february"))
                display2.grid1.set_TextMatrix(a, 3, reader("march"))
                display2.grid1.set_TextMatrix(a, 4, reader("april"))
                display2.grid1.set_TextMatrix(a, 5, reader("may"))
                display2.grid1.set_TextMatrix(a, 6, reader("june"))
                display2.grid1.set_TextMatrix(a, 7, reader("july"))
                display2.grid1.set_TextMatrix(a, 8, reader("august"))
                display2.grid1.set_TextMatrix(a, 9, reader("septemper"))
                display2.grid1.set_TextMatrix(a, 10, reader("october"))
                display2.grid1.set_TextMatrix(a, 11, reader("november"))
                display2.grid1.set_TextMatrix(a, 12, reader("december"))
                a = a + 1
                yodor1 = yodor1 + 1
            ElseIf yodor1 <= (up + 10) Then
                flag = False
                display2.grid2.set_TextMatrix(b, 0, reader("year"))
                display2.grid2.set_TextMatrix(b, 1, reader("january"))
                display2.grid2.set_TextMatrix(b, 2, reader("february"))
                display2.grid2.set_TextMatrix(b, 3, reader("march"))
                display2.grid2.set_TextMatrix(b, 4, reader("april"))
                display2.grid2.set_TextMatrix(b, 5, reader("may"))
                display2.grid2.set_TextMatrix(b, 6, reader("june"))
                display2.grid2.set_TextMatrix(b, 7, reader("july"))
                display2.grid2.set_TextMatrix(b, 8, reader("august"))
                display2.grid2.set_TextMatrix(b, 9, reader("septemper"))
                display2.grid2.set_TextMatrix(b, 10, reader("october"))
                display2.grid2.set_TextMatrix(b, 11, reader("november"))
                display2.grid2.set_TextMatrix(b, 12, reader("december"))
                b = b + 1
                yodor1 = yodor1 + 1
            ElseIf yodor1 <= (yodor + 19) Then
                flag = False
                DISPLAY3.grid2.set_TextMatrix(f, 0, reader("year"))
                DISPLAY3.grid2.set_TextMatrix(f, 1, reader("january"))
                DISPLAY3.grid2.set_TextMatrix(f, 2, reader("february"))
                DISPLAY3.grid2.set_TextMatrix(f, 3, reader("march"))
                DISPLAY3.grid2.set_TextMatrix(f, 4, reader("april"))
                DISPLAY3.grid2.set_TextMatrix(f, 5, reader("may"))
                DISPLAY3.grid2.set_TextMatrix(f, 6, reader("june"))
                DISPLAY3.grid2.set_TextMatrix(f, 7, reader("july"))
                DISPLAY3.grid2.set_TextMatrix(f, 8, reader("august"))
                DISPLAY3.grid2.set_TextMatrix(f, 9, reader("septemper"))
                DISPLAY3.grid2.set_TextMatrix(f, 10, reader("october"))
                DISPLAY3.grid2.set_TextMatrix(f, 11, reader("november"))
                DISPLAY3.grid2.set_TextMatrix(f, 12, reader("december"))
                f = f + 1
                yodor1 = yodor1 + 1
            ElseIf yodor1 < (yodor + 30) Then
                flag = False
                DISPLAY3.grid1.set_TextMatrix(c, 0, reader("year"))
                DISPLAY3.grid1.set_TextMatrix(c, 1, reader("january"))
                DISPLAY3.grid1.set_TextMatrix(c, 2, reader("february"))
                DISPLAY3.grid1.set_TextMatrix(c, 3, reader("march"))
                DISPLAY3.grid1.set_TextMatrix(c, 4, reader("april"))
                DISPLAY3.grid1.set_TextMatrix(c, 5, reader("may"))
                DISPLAY3.grid1.set_TextMatrix(c, 6, reader("june"))
                DISPLAY3.grid1.set_TextMatrix(c, 7, reader("july"))
                DISPLAY3.grid1.set_TextMatrix(c, 8, reader("august"))
                DISPLAY3.grid1.set_TextMatrix(c, 9, reader("septemper"))
                DISPLAY3.grid1.set_TextMatrix(c, 10, reader("october"))
                DISPLAY3.grid1.set_TextMatrix(c, 11, reader("november"))
                DISPLAY3.grid1.set_TextMatrix(c, 12, reader("december"))
                c = c + 1
                yodor1 = yodor1 + 1
            End If
        End While
        reader.Close()
        con.Close()

【问题讨论】:

  • 您是如何从数据库中获取记录的?你能发布一些代码吗?

标签: .net database vb.net visual-studio-2010


【解决方案1】:

首先,您最好使用参数化查询来防止 SQL Injection 。将ORDER BY 子句添加到您的查询中以按year 对记录进行排序

试试这个:

txt = "select * from pension where empno= @empno ORDER BY year" 
cmd = New SqlCommand(txt, con)
cmd.Parameters.AddWithValue("@empno", TextBox5.Text)  

【讨论】:

  • 哇.. 太棒了。非常感谢
  • 其实有什么问题?
  • @user2021114 什么问题?
  • @user2021114 查询需要ORDER BY 子句来获取按年份列排序的记录。
  • @user2021114 只是好奇,你为什么撤消了UpVote?
【解决方案2】:

我终于找到了答案。我用过 Clear() 方法

display2.grid1.clear()
display2.grid2.clear()
DISPLAY3.grid1.clear()
DISPLAY3.grid2.clear()

现在我的问题解决了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多