简体   繁体   English

VB.Net SQL 问题

[英]VB.Net SQL issues

I've been receiving errors with the following code below saying that the index is incorrect.我一直收到以下代码的错误,表示索引不正确。 I'm assuming this is an error with the SQL statement but I'm unsure what's wrong.我假设这是 SQL 语句的错误,但我不确定出了什么问题。

        Private Sub btnStock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStock.Click
    frmStock.Visible = True
    Dim SQLCmd As String
    SQLCmd = "SELECT PartID,PartName,PartStockLevel,MakeName,ModelName FROM tblParts WHERE PartStockLevel <= StockWarnLevel;"
    RunSQLCmd("dt", SQLCmd)
    Dim inc As Integer = 0
    Dim NoLowStock As Boolean
    If DataTable IsNot Nothing AndAlso DataTable.Rows.Count > 0 Then
        frmStock.txtPartID.Text = DataTable.Rows(inc).Item(0)
        frmStock.txtName.Text = DataTable.Rows(inc).Item(1)
        frmStock.NUDStockLvl.Value = DataTable.Rows(inc).Item(2)
        frmStock.txtMake.Text = DataTable.Rows(inc).Item(3)
        frmStock.txtModel.Text = DataTable.Rows(inc).Item(4)
    Else
        frmStock.lblLowStock.Visible = True
        frmStock.btnFirstRecord.Visible = False
        frmStock.btnIncDown.Visible = False
        frmStock.btnIncUp.Visible = False
        frmStock.btnLastRecord.Visible = False
        NoLowStock = True
    End If
    If NoLowStock = False Then
        frmStock.Panel1.Visible = False
    End If
End Sub

    Public Sub RunSQLCmd(ByVal DTorDS As String, ByRef SQLCmd As String)
    DataAdapter = New OleDb.OleDbDataAdapter(SQLCmd, con)
    ConnectDB()
    Try
        If DTorDS = "dt" Then
            DataTable = New DataTable
            DataAdapter.Fill(DataTable)
        Else
            DataSet = New DataSet
            DataAdapter.Fill(DataSet, "srcDataSet")
        End If
    Catch ex As Exception
        con.Close()
    End Try
    con.Close()
End Sub

Thanks in advance!提前致谢!

The error probably comes from you looking at a row index that doesn't exist in the results.该错误可能来自您查看结果中不存在的行索引。 You should check that the table has rows before trying to get the data.在尝试获取数据之前,您应该检查表中是否有行。

Something like this: If DataTable IsNot Nothing AndAlso DataTable.Rows.Count >0 Then... Code here... End If像这样: If DataTable IsNot Nothing AndAlso DataTable.Rows.Count >0 Then... Code here... End If

Try this simple way试试这个简单的方法

dim dt as new datatable

using connection as new sqlconnection("server=SERVER;database=DATABASE;uid=USERID;pwd=PASSWORD")
connection.open()
     using cmd as new sqlcommand("SELECT PartID,PartName,PartStockLevel,MakeName,ModelName FROM tblParts WHERE PartStockLevel <= StockWarnLevel", connection)
     dt.load(cmd.executereader)
     end using
end using

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM