我有一个小要求,我请求任何人通过提供相同的源代码来帮助我。
要求如下:
您能否通过传递参数来调用存储过程,并根据存储过程返回的结果在VB.NET中填充CheckListBox,请帮助我。
此致 乔治
答案 0 :(得分:2)
Imports System.Data
Imports System.Data.SqlClient
导入该命名空间并将以下代码写入某个方法并根据您的数据库和过程字段更改您的代码
'Set up Connection object and Connection String for a SQL Client
Using SQLCon As New SqlClient.SqlConnection
SQLCon.ConnectionString = "Data Source=Server;User ID=User;Password=Password;"
SQLCon.Open()
Dim lDataTable As New DataTable
Using SQLCmdAs New SqlCommand()
SQLCmd.CommandText = "GetAuthors" ' Stored Procedure to Call
SQLCmd.CommandType = CommandType.StoredProcedure
SQLCmd.Connection = SQLCon
SQLCmd.Parameters.Add("AuthorName", SqlDbType.VarChar)
Dim daPubs As New SqlDataAdapter
daPubs.SelectCommand = SQLCmd
daPubs.Fill(lDataTable)
End Using
End Using
CheckBoxList1.DataSource = lDataTable;
CheckBoxList1.DataTextField = "Name";
CheckBoxList1.DataValueField = "ID"
CheckBoxList1.DataBind();
Using Stored Procedures in Conjuction with DataAdapter
How to call SQL Server stored procedures in ASP.NET by using Visual Basic .NET