各位高手看看我的问题吧,纠结
在调用“Fill”前,SelectCommand 属性尚未初始化
Public Function ds(ByVal str As String) As DataSet
Dim myConn As SqlConnection
Dim myComm As SqlCommand
'Dim myDataread As SqlDataReader
Dim myds As DataSet = New DataSet()
Dim myds2 As SqlDataAdapter = New SqlDataAdapter()
Dim datasource As String
datasource = "DATA SOURCE=(local);UID=sa;PWD=7682;DATABASE=JickLiClass"
myConn = New SqlConnection(datasource)
myConn.Open()
myComm = New SqlCommand(str, myConn)
myds2.Fill(myds, "student")
Return myds
myConn.Close()
End Function
End Class
这是我的代码,然后我在下面应用时候的代码如下:
Dim class2 As New Class1
Dim strsql As String
strsql = "select * form student"
GridView1.DataSource = class2.ds(strsql).Tables("student")
GridView1.DataBind()
[解决办法]
直接把 SqlDataAdapter 定义成这样
Dim myds2 As SqlDataAdapter = New SqlDataAdapter(str, myConn)
myds2.Fill(myds, "student")
Return myds
[解决办法]
myComm = New SqlCommand(str, myConn)
因为你只是把他new出来了,但从来就没使用过