这是读取数据的正确语法吗? [英] Is this correct syntax for reading the data?
问题描述
只是尝试获取用于读取数据的正确语法,以便使其可用,以便我可以调用该函数....
我看过的数据读起来像..
.CompanyName = CStr(nwDataReader.GetString(1))
.ContactName = CStr(nwDataReader.GetString(2))
等
我有大约30列要阅读,我是否必须以上述方式调用函数中的每一行,还是有更好的方法?
上一篇:
我正在创建我的基类,并调用一个函数来检索数据.如果我以以下格式读取数据,是否可以在页面后面的代码中调用该函数?
Just trying to get the correct syntax for reading the data so that it it available so that I can call the function....
I''ve seen the data read like..
.CompanyName = CStr(nwDataReader.GetString(1))
.ContactName = CStr(nwDataReader.GetString(2))
Etc.
I have approx 30 columns to read, do I have to call each line in the function in the above manner or is there a better way?
PREVIOUS POST:
I am creating my base class and calling a function to retrieve the data. If I read the data in the following format, will I be able to call the function in the code behind page?
Public Function GetPortfolioInfo(ByVal Contract As String) As PortfolioInfo
Dim Reader...
Dim PortfolioInfo...
Dim Conn..
Do while Read
If IsDBNull(rd("ContractDesc")) = True Then
_ContractDesc = ""
Else : _ContractDesc = "" & CType(rd("ContractDesc"), String)
End If
If IsDBNull(rd("Status")) = True Then
_Status = ""
Else : _Status = "" & CType(rd("Status"), String)
End If
Return PortfolioInfo1
End Function
推荐答案
您在正确的轨道上.但是,我不确定您的数据源是什么.我假设它是一个数据库,并且我将使用SQLDataAdapter从数据库中获取数据并使用它来填充数据表.填充数据表后,指定所需的行索引和项目索引.在下面的示例中,该行设置为0,这是数据库表中的第一行.该项目还设置为0,这是数据库表中的第一列.如果要查找的项目在第4列中,则将项目值设置为3(例如).
You are on the right track. However, I''m not sure what your data source is. I''ll assume it is a database, and I''ll use an SQLDataAdapter to get the data from a database and use it to fill a datatable. When the datatable is filled, specify the desired row index and the item index. In the example below, the row is set to 0, which is the first row in the database table. The item is also set to 0, which is the first column in the database table. If the item you are looking for is in the 4th column, set the item value to 3 (for example).
Dim SQLQuery as string = "Select * from dbTable"
Dim ConnectionString as string = "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"
Dim DT as DataTable = new system.data.datatable
DT= SQLToDT(ConnectionString, SQLQuery)
CompanyName = DT.rows(0).item(0).tostring
Public Function SQLToDT(ConnectionString As String, SQLQuery As String) As DataTable
Dim myDTable as system.data.datatable
Dim conn As New SqlConnection(ConnectionString )
Dim adapter As New SqlDataAdapter()
adapter.SelectCommand = new SqlCommand(SQLQuery , conn)
adapter.Fill(myDTable)
Return myDTable
End Function
尚不清楚是要从数据库中检索单个记录还是返回所有记录.我认为,到目前为止,最好使用DataReader作为最佳选择,并且所使用的方法非常可靠.检索记录时,我使用以下语法:
It is not clear whether you are trying to retrieve a single record from the database or returning all records. In my opinion using a DataReader as you are trying to do is by far the best option, and the method you are using is pretty robust. When retrieving records I use this syntax:
If Not IsDBNull(dbrProducts(1)) Then .Name = dbrProducts.GetString(1) Else .Name = "Default"
If Not IsDBNull(dbrProducts(2))Then.CostPrice = dbrProducts.GetDecimal(2) Else .CostPrice = 0
If Not IsDBNull(dbrProducts(3)) Then .PackSize = dbrProducts.GetInt32(3) Else .PackSize = 0
If Not IsDBNull(dbrProducts(4)) Then .RetailPrice = dbrProducts.GetDecimal(4) Else .RetailPrice = 0
只是一种不同的表达方式,可能更简洁,更易读.希望这会有所帮助.
快乐编码
just a different way of putting it is all maybe a liitle more concise and readable. Hope this helps.
Happy Coding
这篇关于这是读取数据的正确语法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!