VB.Net/Database ADO.net/Table Column
Get Colunm Data type
<source lang="vbnet"> Imports System Imports System.Data Imports System.Data.SqlClient
public class MainClass
Shared Sub Main() Dim thisConnection As New SqlConnection("server=(local)\SQLEXPRESS;" & _ "integrated security=sspi;database=MyDatabase") "Sql Query 1 Dim sql As String ="SELECT ID, FirstName, LastName FROM Employee; " "Create Command object Dim thisCommand As New SqlCommand _ (sql, thisConnection) Try " Open Connection thisConnection.Open() Console.WriteLine("Connection Opened") " Execute Query Dim thisReader As SqlDataReader = thisCommand.ExecuteReader() " Get column data types Console.WriteLine("Data types: {0} | {1}", _ thisReader.GetDataTypeName(0).PadLeft(13), _ thisReader.GetDataTypeName(1))
"Close DataReader thisReader.Close() Catch ex As SqlException " Display error Console.WriteLine("Error: " & ex.ToString()) Finally " Close Connection thisConnection.Close() Console.WriteLine("Connection Closed") End Try End Sub
End Class
</source>
Get table column Name
<source lang="vbnet"> Imports System Imports System.Collections Imports System.Data Imports System.IO Imports System.Xml.Serialization Imports System.Windows.Forms Imports System.Data.SqlClient
Public Class MainClass
Shared Dim WithEvents con As SqlConnection Shared Sub Main() con = New SqlConnection("Server=(local)\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=SSPI") Dim str As String = "SELECT ID, FirstName, LastName FROM Employee" Dim cmd As New SqlCommand(str, con) Dim da As New SqlDataAdapter(cmd) Dim ds As New DataSet() da.Fill(ds, "Employee") " Display the column names Dim dc As DataColumn For Each dc In ds.Tables(0).Columns Console.Write("{0,15}", dc.ColumnName) Next " Add a newline after the column headings Console.Write(vbCrLf) " Display the data for each row. Loop through the rows first. Dim dr As DataRow For Each dr In ds.Tables(0).Rows " Then loop through the columns for the current row. Dim i As Integer For i = 1 To ds.Tables(0).Columns.Count Console.Write("{0,15}", dr(i - 1)) Next i " Add a line break after every row Console.Write(vbCrLf) Next End Sub
End Class
</source>
Read Table data by Table Column Name
<source lang="vbnet"> Imports System Imports System.Data Imports System.Data.OleDb Imports System.Data.SqlClient Imports System.Collections Imports System.Windows.Forms Imports System.Resources Public Class MainClass
Shared Sub Main() "Declare variables and objects Dim strConnectionString As String = _ "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=Employee.mdb;" Dim objConnection As New OleDbConnection(strConnectionString) Dim strSQL As String = _ "SELECT FirstName, LastName FROM Employee" Dim objCommand As New OleDbCommand(strSQL, objConnection) Dim objDataAdapter As New OleDbDataAdapter(objCommand) Dim objDataTable As New Data.DataTable("Employee") Dim objDataRow As DataRow Try "Open the database connection objConnection.Open() "Fill the DataTable object objDataAdapter.Fill(objDataTable) "Load the list box on the form For Each objDataRow In objDataTable.Rows Console.WriteLine(objDataRow.Item("FirstName") & " " & _ objDataRow.Item("LastName")) Next Catch OleDbExceptionErr As OleDbException "Write the exception Console.WriteLine(OleDbExceptionErr.Message) Catch InvalidOperationExceptionErr As InvalidOperationException "Write the exception Console.WriteLine(InvalidOperationExceptionErr.Message) End Try "Close the database connection objConnection.Close() "Clean up objDataRow = Nothing objDataTable.Dispose() objDataTable = Nothing objDataAdapter.Dispose() objDataAdapter = Nothing objCommand.Dispose() objCommand = Nothing objConnection.Dispose() objConnection = Nothing End Sub
End Class
</source>
<A href="http://www.vbex.ru/Code/VBDownload/Employee.zip">Employee.zip( 7 k)</a>