Parameters
- i
- The zero-based column ordinal.
Return Value
A string that is the name of the specified column.
public void GetFields(SqlConnection myConnection) { SqlCommand cmd = new SqlCommand("SELECT * FROM Test.Dept"); cmd.Connection = myConnection; myConnection.Open(); try { SqlDataReader reader = cmd.ExecuteReader(); reader.Read(); for (int i=0;i < reader.FieldCount;i++) { string fieldType = reader.GetDataTypeName(i); string fieldName = reader.GetName(i); string fieldType2 = reader.GetFieldType(i).FullName; Console.WriteLine(fieldName+"\t"+ fieldType+"\t"+ fieldType2); } reader.Close(); } finally { myConnection.Close(); } }
Public Sub GetFields(ByVal myConnection As SqlConnection Dim cmd As SqlCommand = New SqlCommand("SELECT * FROM Test.Dept") cmd.Connection = myConnection myConnection.Open() Try Dim reader As SqlDataReader = cmd.ExecuteReader() reader.Read() Dim i As Integer For i = 0 To reader.FieldCount - 1 Dim fieldType As String = reader.GetDataTypeName(i) Dim fieldName As String = reader.GetName(i) Console.WriteLine(fieldName & " " & fieldType) Next i reader.Close() Finally myConnection.Close() End Try End Sub
Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2