0

我认为自己是一个非常有耐心的人,有很大的决心让事情在我正在研究的程序中运行。然而,我觉得我只是在浪费宝贵的时间,我可以用这些时间来真正获得知识,而不是把头撞到墙上什么也没做。所以..我要假装我今天花了 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 例程中,但我不能肯定地说,因为我自己无法解决这个问题。非常感谢花时间阅读所有这些内容的人,如果它目前草率并且加载了未使用的方法或变量,我深表歉意......我已经完全破坏了这个东西,试图让它工作。

4

1 回答 1

1

我认为问题在于您使用的是 ADO 而不是 ADO.NET。

我没有时间编写您的代码,但也许您应该从可行的东西开始。这应该从您的数据库中提取一些数据并填充 datagridview。您当然需要更改连接字符串和表/字段名等:

  Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
    Dim strCnn As String = "Data Source=sqlservername;Timeout=10;Database=databasename;Trusted_Connection=no;User ID=userid;Password=password;"
    Using cnn As New SqlConnection(strCnn)
      cnn.Open()
      Dim strSql As String = "SELECT * FROM Tablename"
      Dim dtb As New DataTable
      Using dad As New SqlDataAdapter(strSql, cnn)
        dad.Fill(dtb)
      End Using
      'now hide all the columns except the ones we want to show'
      dgv.AutoGenerateColumns = False 'dgv is the name of the datagridview'
      Dim intColIndex As Integer
      intColIndex = dgv.Columns.Add("Field1", "Field 1") 'aliases'
      dgv.Columns(intColIndex).DataPropertyName = "F1" 'column name in database'
      intColIndex = dgv.Columns.Add("Field3", "Field 3") 'aliases'
      dgv.Columns(intColIndex).DataPropertyName = "F3" 'column name in database'
      'now bind'
      dgv.DataSource = dtb
    End Using
  End Sub

哦,你需要

Imports System.Data
Imports System.Data.SqlClient

在表单源代码的顶部。

于 2012-06-04T03:41:49.177 回答