如何使用 SqlDataReader 从 select 中获取值而不知道它在 Visual Studio 上的位置
How to use SqlDataReader to get a value from a select without knowing its position on Visual Studio
我已经将一些东西从 VB 翻译成 C#,但有些东西我还没有弄清楚如何做...
这是整个函数
Function get_projects_by_user(Optional uid As String = "", Optional filter As String = "%") As String()
Dim returnArray(1) As String
Dim count As Integer = 1
Dim SQLConn As New SqlConnection() 'The SQL Connection
Dim SQLCmd As New SqlCommand() 'The SQL Command
Dim SQLdr As SqlDataReader 'The Local Data Store
returnArray(1) = ""
SQLConn.ConnectionString = ConStr 'Set the Connection String
SQLConn.Open() 'Open the connection
SQLCmd.Connection = SQLConn 'Sets the Connection to use with the SQL Command
SQLCmd.CommandText = Replace(Replace(ASK_PROJECTS_BY_USERS, "PAR1", uid), "PAR2", filter) 'Sets the SQL String
SQLdr = SQLCmd.ExecuteReader 'Gets Data
While SQLdr.Read()
count = count + 1
ReDim Preserve returnArray(count)
returnArray(count) = SQLdr("codigo") & " - " & SQLdr("nomeProjeto")
End While
SQLdr.Close() 'Close the SQLDataReader
SQLConn.Close() 'Close the connection
Return returnArray
End Function
我遇到问题的部分是:
returnArray(count) = SQLdr("codigo") & " - " & SQLdr("nomeProjeto")
我不知道如何在 C# 中执行此操作(从名称而非位置获取值)。
VB.NET 使用圆括号访问 default property of an object, whereas C# uses square brackets to access the indexer property(索引器本质上是 VB 默认属性的 C# 等价物)。因此,在 VB.NET 中,您可以使用 SQLdr("codigo")
按名称访问列的值,而在 C# 中,等效项是 SQLdr["codigo"]
。访问数组中的项目存在相同的语法差异。
在这种情况下,SqlDataReader
class 的 indexer/default 属性 是 Item
属性。这意味着在 VB.NET 中,您可以使用默认 属性 语法或 Item
属性 显式访问 属性:
'Using the default property syntax
Dim value As Object = SQLdr("codigo")
'Using the Item property explicitly
Dim value As Object = SQLdr.Item("codigo")
在 C# 中,Item
属性 是隐藏的,只能通过索引器语法访问。
作为旁注,您还可以使用 GetOrdinal
方法获取给定名称的列的索引。例如:
int index = SQLdr.GetOrdinal("codigo");
或 VB.NET
Dim index As Integer = SQLdr.GetOrdinal("codigo")
while(SQLdr.Read())中的一些用法:
int: int.Parse(reader["Id"].ToString()),
timestamp: (byte[])(reader["TimeStampOperation"]),
DateTime: DateTime.Parse(reader["DateTime"].ToString()),
string: reader["FirstName"].ToString();
reader-方括号(如 Id、FirstName...)之间的 ["string"] 是 SQL-[ 的 table- 字段=21=]
希望对你有所帮助...
我已经将一些东西从 VB 翻译成 C#,但有些东西我还没有弄清楚如何做...
这是整个函数
Function get_projects_by_user(Optional uid As String = "", Optional filter As String = "%") As String()
Dim returnArray(1) As String
Dim count As Integer = 1
Dim SQLConn As New SqlConnection() 'The SQL Connection
Dim SQLCmd As New SqlCommand() 'The SQL Command
Dim SQLdr As SqlDataReader 'The Local Data Store
returnArray(1) = ""
SQLConn.ConnectionString = ConStr 'Set the Connection String
SQLConn.Open() 'Open the connection
SQLCmd.Connection = SQLConn 'Sets the Connection to use with the SQL Command
SQLCmd.CommandText = Replace(Replace(ASK_PROJECTS_BY_USERS, "PAR1", uid), "PAR2", filter) 'Sets the SQL String
SQLdr = SQLCmd.ExecuteReader 'Gets Data
While SQLdr.Read()
count = count + 1
ReDim Preserve returnArray(count)
returnArray(count) = SQLdr("codigo") & " - " & SQLdr("nomeProjeto")
End While
SQLdr.Close() 'Close the SQLDataReader
SQLConn.Close() 'Close the connection
Return returnArray
End Function
我遇到问题的部分是:
returnArray(count) = SQLdr("codigo") & " - " & SQLdr("nomeProjeto")
我不知道如何在 C# 中执行此操作(从名称而非位置获取值)。
VB.NET 使用圆括号访问 default property of an object, whereas C# uses square brackets to access the indexer property(索引器本质上是 VB 默认属性的 C# 等价物)。因此,在 VB.NET 中,您可以使用 SQLdr("codigo")
按名称访问列的值,而在 C# 中,等效项是 SQLdr["codigo"]
。访问数组中的项目存在相同的语法差异。
在这种情况下,SqlDataReader
class 的 indexer/default 属性 是 Item
属性。这意味着在 VB.NET 中,您可以使用默认 属性 语法或 Item
属性 显式访问 属性:
'Using the default property syntax
Dim value As Object = SQLdr("codigo")
'Using the Item property explicitly
Dim value As Object = SQLdr.Item("codigo")
在 C# 中,Item
属性 是隐藏的,只能通过索引器语法访问。
作为旁注,您还可以使用 GetOrdinal
方法获取给定名称的列的索引。例如:
int index = SQLdr.GetOrdinal("codigo");
或 VB.NET
Dim index As Integer = SQLdr.GetOrdinal("codigo")
while(SQLdr.Read())中的一些用法:
int: int.Parse(reader["Id"].ToString()),
timestamp: (byte[])(reader["TimeStampOperation"]),
DateTime: DateTime.Parse(reader["DateTime"].ToString()),
string: reader["FirstName"].ToString();
reader-方括号(如 Id、FirstName...)之间的 ["string"] 是 SQL-[ 的 table- 字段=21=]
希望对你有所帮助...