初学者
我有以下代码,我想使用名为findCustomerBTN
的按钮来调用 Public Function Execute(ByVal sqlQuery As String) As ADODB.Recordset
If SecuritySSPIchkbx.Checked Then
chk = "TRUE"
Else chk = "FALSE"
End If
builder.DataSource = ServerBox.Text
builder.InitialCatalog = DatabaseBox.Text
builder.UserID = Username.Text
builder.Password = Password.Text
builder.IntegratedSecurity = chk
MessageBox.Show(builder.ConnectionString)
Using sqlConnection1 As New SqlConnection(builder.ConnectionString)
sqlConnection1.Open()
Try
command = New SqlCommand(sqlQuery, sqlConnection1)
adapter.SelectCommand = command
adapter.Fill(ds, "Create DataView")
adapter.Dispose()
command.Dispose()
sqlConnection1.Close()
dv = ds.Tables(0).DefaultView
DataGridView1.DataSource = dv
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Using
End Function
如何调用此功能:
Private Sub findCustomerBTN_Click(sender As Object, e As EventArgs) Handles
sqlquery = "Select * from customers where name = 'Smith'"
call function?
End Sub
我用谷歌搜索了但是我无法理解一个函数如何工作任何指针来帮助我理解会非常感谢
答案 0 :(得分:1)
在VB中,函数是一个返回值的代码块。您的代码不会返回值,并且您执行的查询执行类型将永远不会返回ADODB.RecordSet - 这是VB6时代的古老技术,并且您正在使用一个更现代化的数据访问策略(ADO.NET,DataTables和DataAdapter),虽然它不是最新的和最好的。
提供代码的运行以及其他问题:
Execute("SELECT * FROM kittens")
期待获得一些可爱的数据,而我得到的只是同一个老客户什么是功能?他们在做什么?他们需要一些输入并返回一些输出:
Public Function AddTheseTwo(a as Integer, b as Integer) As Integer
Return a + b
End Function
他们被这样称呼:
Dim sum = AddTheseTwo(2, 3)
即。你给出函数的名称和输入值,它们可以是变量,并存储结果(通常,因为你想使用它)。这是您的代码,它是一个不会返回值的代码块
Private Sub findCustomerBTN_Click(sender As Object, e As EventArgs) Handles
Execute("Select * from customers where name = 'Smith'")
End Sub
Handles
关键字后面没有任何内容。它应该是Handles findCustomerBTN.Click
之类的东西。你可以整天捣碎那个按钮而不会发生任何事情Execute
但没有存储返回值(因为它不需要,因为Execute没有返回任何内容,所以Execute应该被声明为sub ,而不是功能)编辑:
您提到希望函数返回数据表:
Public Function GetDataTable(ByVal sqlQuery As String) As DataTable 'need to Imports System.Data if you haven't already
If SecuritySSPIchkbx.Checked Then
chk = "TRUE"
Else chk = "FALSE"
End If
'better to declare builder in this function, not elsewhere
builder.DataSource = ServerBox.Text
builder.InitialCatalog = DatabaseBox.Text
builder.UserID = Username.Text
builder.Password = Password.Text
builder.IntegratedSecurity = chk
MessageBox.Show(builder.ConnectionString)
Using sqlConnection1 As New SqlConnection(builder.ConnectionString)
sqlConnection1.Open()
Try
'note: better to declare adapter and command in this function too
command = New SqlCommand(sqlQuery, sqlConnection1)
adapter.SelectCommand = command
Dim dt as New DataTable
adapter.Fill(dt)
adapter.Dispose()
command.Dispose()
sqlConnection1.Close()
Return dt
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Using
Return Nothing 'a function has to return something from all possible code paths, even if it's Nothing :)
End Function
然后你这样称呼它,也许是:
Private Sub findCustomerBTN_Click(sender As Object, e As EventArgs) Handles whateverbutton.Click
'you can set a datatable as a datasource, doesn't have to be the datatable.defaultview
myDataGRidView.DataSource = GetDataTable("Select * from customers where name = 'Smith'")
End Sub
我建议你打开Strict / Explicit等选项,以鼓励更好的编码实践。默认情况下,VB非常宽松,允许您使用尚未声明的变量(自动声明变量名称是另一个变量名称的错误等),自动从函数等返回任何内容 - 这些小汽车' s后来会导致错误和错误。计算机编程是一门精确的艺术;打开所有选项以强制自己尽可能精确
答案 1 :(得分:0)
您可以使用以下代码调用Execute
函数:
Private Sub findCustomerBTN_Click(sender As Object, e As EventArgs) Handles
Execute("Select * from customers where name = 'Smith'")
End Sub
您还必须从Execute
功能
sqlQuery = ("select * from ac_billbook where ref = '900123'")
请按照Steve的建议阅读一本关于VB.NET编程的好书。