SQL query returning 'Overload resolution error'

£可爱£侵袭症+ 提交于 2019-12-24 16:53:34

问题


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


来源:https://stackoverflow.com/questions/38323709/sql-query-returning-overload-resolution-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!