SQL查询返回'重载解析错误'

时间:2016-07-12 08:32:33

标签: sql .net sql-server vb.net oledb

我有一个readini文件连接到我的SQL Server表,在我的查询代码中显示来自它的数据,我收到一个我无法解决的错误,这里有人可以吗?

这是错误:

  

错误1
  重载解析失败,因为无法使用以下参数调用可访问的“New”:   'Public Sub New(selectCommandText As String,selectConnection As System.Data.OleDb.OleDbConnection)':'SQLServerApplication.readini'类型的值无法转换为'System.Data.OleDb.OleDbConnection'。
  'Public Sub New(selectCommandText As String,selectConnectionString As String)':'SQLServerApplication.readini'类型的值不能转换为'String'。

这是代码:

Imports System.Data.OleDb
Imports System.Data.SqlClient

Public Class frmViewDtb


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim connection As readini = New readini()
    connection.getConnectionString()

    Dim sql As String = "SELECT * FROM tblPerson"
    Dim da As New OleDbDataAdapter(sql, connection)
    Dim ds As New DataSet()
    da.Fill(ds, "tblPerson")
    DataGridView1.DataSource = ds
    DataGridView1.DataMember = "tblPerson"

End Sub
End Class

发生错误的行是第13行:

 Dim da As New OleDbDataAdapter(sql, connection)

getConnectionString的代码;

    Public Function getConnectionString() As String

    Dim s As String =
        "Provider=" & provider & ";" &
        "user ID=" & username & ";" &
        "password=" & password & ";" &
        "initial catalog=" & databasename & ";" &
        "data source=" & servername & "; " &
    "Persists Security Info=False"

End Function

如果你能得到它,请提前致谢!

1 个答案:

答案 0 :(得分:1)

我相信你收到错误,因为OleDbDataAdpater的构造函数需要两个字符串而你的connection变量不是一个字符串。我怀疑你的代码需要看起来像这样:

Dim connection As readini = New readini()
Dim ConnString = connection.getConnectionString()

Dim sql As String = "SELECT * FROM tblPerson"
Dim da As New OleDbDataAdapter(sql, ConnString)
Dim ds As New DataSet()
da.Fill(ds, "tblPerson")
DataGridView1.DataSource = ds
DataGridView1.DataMember = "tblPerson"

getConnectionString方法还需要修改以添加Return语句:

Public Function getConnectionString() As String

    Dim s As String =
        "Provider=" & provider & ";" &
        "user ID=" & username & ";" &
        "password=" & password & ";" &
        "initial catalog=" & databasename & ";" &
        "data source=" & servername & "; " &
        "Persists Security Info=False"
    Return s
End Function