问题描述
我有一个readini文件可连接到SQL Server表,并且在查询代码中显示该表中的数据时,出现了我无法解决的错误,这里有人可以吗?
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?
这是错误:
这是代码:
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行:
The line that the error is occurring on is line 13:
Dim da As New OleDbDataAdapter(sql, connection)
getConnectionString的代码;
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!
推荐答案
我相信您会收到此错误,因为OleDbDataAdpater的构造函数需要两个字符串,而您的connection
变量不是字符串.我怀疑您的代码需要如下所示:
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"
getConnectionString方法还需要修改以添加Return
语句:
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
这篇关于SQL查询返回“过载解析错误"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!