SQL query returning 'Overload resolution error

2019-07-31 23:31发布

问题:

I have a readini file to connect to my SQL Server table, and in my query code to display data from it, I'm getting an error that I've not been able to solve, is there anybody here who can?

This is the error:

Error 1
Overload resolution failed because no accessible 'New' can be called with these arguments: 'Public Sub New(selectCommandText As String, selectConnection As System.Data.OleDb.OleDbConnection)': Value of type 'SQLServerApplication.readini' cannot be converted to 'System.Data.OleDb.OleDbConnection'.
'Public Sub New(selectCommandText As String, selectConnectionString As String)': Value of type 'SQLServerApplication.readini' cannot be converted to 'String'.

This is the code:

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

The line that the error is occurring on is line 13:

 Dim da As New OleDbDataAdapter(sql, connection)

Code for 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

Thanks in advance if you can get it!

回答1:

I believe you are getting the error as the constructor for OleDbDataAdpater is expecting two strings and your connection variable isn't a string. I suspect your code needs to look like this:

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"

The getConnectionString method also needed amending to add the Return statement:

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