我认为自己是一个非常有耐心的人,有很大的决心让事情在我正在研究的程序中运行。然而,我觉得我只是在浪费宝贵的时间,我可以用这些时间来真正获得知识,而不是把头撞到墙上什么也没做。所以..我要假装我今天花了 11 个小时试图解决这个问题并没有浪费,只要我能够解决它。我是数据库新手,正在开发数据库程序。它是用 vb6 编写的,我试图在 vb.net 中重写它。我已经将程序从所有不需要的花哨插件中完全剥离出来,现在只需要运行它所需的代码。该程序检索记录,但不会将这些记录填充到数据网格中。是的,
表格1
Imports ADS_SQL_TEST_VBNET
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Module1.Main()
End Sub
Public Sub cmdSQLBound_Click()
sWhichDataType = "SQL"
Call SetGridSize("SQL Table Bound.")
End Sub
Public Sub optYes01ph_Click()
BuildTable() 'sets sFields and sQuery
cmdSQLBound_Click()
End Sub
End Class
模块1
Imports System.Data
Imports System.Data.SqlClient
Imports ADS_SQL_TEST_VBNET.Form1
Imports ADS_SQL_TEST_VBNET.Class1
Public Module Module1
Public rs As ADODB.Recordset
Public db As ADODB.Connection
Public dgvDataGrid As DataGridView
Public DataGridRef As New DataGridView
Public sWhichDataType, sSource, sTable, sFields, sQuery, sFilter, sOrderBy, sSelectedIndex, sStoreId
Public lblCount As Object
Public Ctrl1, Ctrl2, Freq, Overhead As Decimal
Public l, x As Integer
Public A, I As Long
Public Sub Main()
Form1.optYes01ph_Click()
End Sub
Public Sub BuildTable()
If sTable = "yes01ph" Or sTable = "" Then
sTable = "yes01ph"
sFields = "store_id,control_no,first_name,last_name,company,address,city,State,zip,phone,total,stat_code,Year,make_id,model_id,engine_id,contact,phone1,phone2,phone3,phone4,invoice_no,ready_proc,summary,note"
sOrderBy = "control_no"
Else
sFields = "store_id,ml_id,first_name,last_name,company,address,city,state,zip,phone,last_upd,address2,phone2,ref_count,comments,fobs_key,quest,lockedby,rmte_stat,serv_upd,serv_id,psm_use,psm_points"
sOrderBy = "ml_id"
End If
BuildFilter()
End Sub
Public Sub BuildFilter()
sFilter = " AND control_no > 1 AND control_no < 500000"
BuildQuery()
End Sub
Public Sub BuildQuery()
sQuery = "SELECT " & sFields & " FROM " & sTable & " WHERE Store_ID Like 'XXX' " & sFilter & " Order By " & sOrderBy
sQuery = Replace(sQuery, "'XXX'", "'" & Trim(UCase("250")) & "'")
End Sub
Public Sub SetGridSize(ByVal sMessage As String)
Dim lblTableType As String
lblTableType = " " & sMessage
Form1.dgvDataGrid.Text = "Data populated using: " & UCase(sMessage)
DataGridRef.ColumnCount = 99
DataGridRef.RowCount = 1
DataSetRef.LoadDataBound()
End Sub
End Module
第一类
Public Class Class1
Public Sub LoadDataBound()
rs = GetData()
If Not rs.EOF Then
dgvDataGrid.DataSource = rs ' Bind the Datagrid to the recordset
dgvDataGrid.Refresh()
Else
MsgBox("No Records Found in DataGrid!")
End If
End Sub
Public Function GetData() As ADODB.Recordset
sWhichDataType = UCase(Trim(sWhichDataType))
sSource = "development"
GetData = OpenSQL()
End Function
Public Function OpenSQL() As ADODB.Recordset
db = New ADODB.Connection : rs = New ADODB.Recordset ' Initialize Connection object & RecordSet object
With db
.CursorLocation = ADODB.CursorLocationEnum.adUseClient
.Mode = ADODB.ConnectModeEnum.adModeReadWrite
.ConnectionString = "Provider=SQLOLEDB.1;Password=pacesql;Persist Security Info=True;User ID=sa;Initial Catalog=speedwrench;Data Source=" & sSource
.Open() ' "Provider=SQLOLEDB.1;Data Source=development", "sa", "pacesql", -1
End With
OpenSQL = db.Execute(sQuery)
End Function
End Class
我认为问题出在 LoadDataBound() Sub 例程中,但我不能肯定地说,因为我自己无法解决这个问题。非常感谢花时间阅读所有这些内容的人,如果它目前草率并且加载了未使用的方法或变量,我深表歉意......我已经完全破坏了这个东西,试图让它工作。