我正在尝试获取sql server中的所有数据行以打印所有值。这是我已经尝试过的代码,但它仍然无效。谁能告诉我这里有什么问题?我是vb.net的新手
For i = 1 To 100
Dim testsection As String = e.Item.DataItem("sectionName")
e.Item.Cells(4).Text = strSection & testsection
Next
我不太明白它是如何获取数据的。我是一名新毕业生。但是他们已经把这个预先任务分配给了我这是我的DBFunction中的代码。
Public Function GetUserList(ByVal strUserLogin As String, ByVal strName As String, _
ByVal intCompanyID As Integer, ByVal tblName As String) As DataSet
Dim oConn As SqlConnection = Nothing
Dim SQLStr As String = ""
Dim SubSQL As String = ""
Dim CMD As SqlCommand = Nothing
Dim DS As New DataSet
Dim DA As New SqlDataAdapter
Try
oConn = New SqlConnection(ConnectionString)
oConn.ConnectionString = ConnectionString
oConn.Open()
CMD = oConn.CreateCommand
CMD.CommandType = CommandType.StoredProcedure
SQLStr = "sp_tblUser_Get"
'CMD.Parameters.Add("@CompanyID", SqlDbType.Int).Value = intCompanyID
CMD.Parameters.Add("@LoginID", SqlDbType.VarChar, 50).Value = strUserLogin
CMD.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = strName
'If strUserLogin <> "" Then
' SubSQL = " AND u.UserLogin = " & SQLS(strUserLogin)
'End If
'If intCompanyID <> 0 Then
' SubSQL = " AND u.CompanyId = " & SQLN(intCompanyID)
'End If
'SQLStr = "select u.ID, u.UserLogin, u.Name, c.CompanyName, u.CreateDate from tblUser u " & _
' " inner join tblCompany c on u.CompanyId = c.ID WHERE u.ID <> 0 " & SubSQL
CMD.CommandText = SQLStr
DA.SelectCommand = CMD
DA.Fill(DS, tblName)
CMD = Nothing
DA = Nothing
oConn.Close()
oConn = Nothing
Catch ex As System.Exception
DS = Nothing
Finally
If Not oConn Is Nothing Then
oConn.Close()
oConn = Nothing
End If
If Not CMD Is Nothing Then
CMD = Nothing
End If
End Try
Return DS
End Function
答案 0 :(得分:0)
基本上,从Dim oConn As SqlConnection = Nothing
到DA.SelectCommand = CMD
的行用于创建与数据库的连接,并且具有调用名为sp_tblUser_Get的过程的步骤。
然后使用行
来设置此过程的所有必需参数CMD.Parameters.Add("@LoginID", SqlDbType.VarChar, 50).Value = strUserLogin
CMD.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = strName
之后使用SqlDataAdapter的Fill方法数据集填充了输出。 数据集DS可以包含单个DataTable或多个依赖于热存储过程的数据表。
我假设它包含单个数据表,然后如果要查看数据,则可以使用。
DataTable dt= DS.Tables[0];
此dt Datatablw将包含所有数据。 要显示该数据,您可以使用如下所述的循环。
DataTable dt = DS.Tables[0];
foreach (DataRow dr in dt.Rows)
{
String Name = dr["FirstName"].ToString();
int Age = Convert.ToInt32(dr["Age"]);
}