Their Difference: Dynamic Link Library
Their Difference: Dynamic Link Library
net and
their Difference
VB.Net
Dim constring As String =
ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constring)
Using cmd As New SqlCommand("SELECT Name, City FROM Persons", con)
cmd.CommandType = CommandType.Text
con.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
While dr.Read()
Dim name As String = dr("Name").ToString()
Dim city As String = dr("City").ToString()
Response.Write("Name: " & name)
Response.Write("City: " & city)
End While
con.Close()
End Using
End Using
DataAdapter
DataAdapter is used to execute SQL statements and is used to populate the results of SQL Query into a
DataSet or DataTable.
DataAdapter gets all the rows of the executed SQL statement at once and populates into DataSet or
DataTable in memory and hence DataAdapter is bit slower compared to DataReader.
Since the DataAdapter populates all rows in DataSet or DataTable it can be traversed in both Forward
and Backward directions.
DataAdapter makes use of the Fill function to populate the rows of SQL statement into a DataSet or
DataTable.
DataAdapter manages the connection internally and does not require to open or close connections
explicitly and this feature is termed as Disconnected Architecture.
Example would be fetching Name City for all records in the Person Table using DataAdapter.
C#
string constring =
ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT Name, City FROM Persons", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
sda.Fill(ds);
foreach (DataRow row in ds.Tables[0].Rows)
{
string name = row["Name"].ToString();
string city = row["City"].ToString();
Response.Write("Name: " + name);
Response.Write("City: " + city);
}
}
}
}
VB.Net
Dim constring As String =
ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constring)
Using cmd As New SqlCommand("SELECT Name, City FROM Persons", con)
cmd.CommandType = CommandType.Text
Using sda As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
sda.Fill(ds)
For Each row As DataRow In ds.Tables(0).Rows
Dim name As String = row("Name").ToString()
Dim city As String = row("City").ToString()
Response.Write("Name: " & name)
Response.Write("City: " & city)
Next
End Using
End Using
End Using
DataSet
DataSet is in simple terms set of Data i.e. set of DataTables or collection of DataTables i.e. it can hold
one or multiple DataTables.
DataSet is mainly used to fetch and hold the records for one or more tables into memory.
A DataAdapter is used to populate DataSet from records returned from an SQL statement and also a
DataSet can be created in memory and tables and data can be added to it.
DataSet can also be converted and saved as XML file.
Example would be fetching Name City for all records in the Person Table into a DataSet.
string constring =
ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT Name, City FROM Persons", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
sda.Fill(ds);
foreach (DataRow row in ds.Tables[0].Rows)
{
string name = row["Name"].ToString();
string city = row["City"].ToString();
Response.Write("Name: " + name);
Response.Write("City: " + city);
}
}
}
}
VB.Net
Dim constring As String =
ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constring)
Using cmd As New SqlCommand("SELECT Name, City FROM Persons", con)
cmd.CommandType = CommandType.Text
Using sda As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
sda.Fill(ds)
For Each row As DataRow In ds.Tables(0).Rows
Dim name As String = row("Name").ToString()
Dim city As String = row("City").ToString()
Response.Write("Name: " & name)
Response.Write("City: " & city)
Next
End Using
End Using
End Using
DataTable
A DataTable can hold records of a single Table consisting of rows and columns. A DataTable can be
reside within a DataSet.
DataTable is mainly used to fetch and hold the records of one single table into memory.
A DataAdapter is used to populate DataTable from records returned from an SQL statement and also a
DataTable can be created in memory and data can be added to it.
Example would be fetching Name City for all records in the Person Table into a DataTable.
C#
string constring =
ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT Name, City FROM Persons", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
foreach (DataRow row in dt.Rows)
{
string name = row["Name"].ToString();
string city = row["City"].ToString();
Response.Write("Name: " + name);
Response.Write("City: " + city);
}
}
}
}
VB.Net
Dim constring As String =
ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constring)
Using cmd As New SqlCommand("SELECT Name, City FROM Persons", con)
cmd.CommandType = CommandType.Text
Using sda As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
sda.Fill(dt)
For Each row As DataRow In dt.Rows
Dim name As String = row("Name").ToString()
Dim city As String = row("City").ToString()
Response.Write("Name: " & name)
Response.Write("City: " & city)
Next
End Using
End Using
End Using
Variables can be declared using the WithEvents construct. This construct is available so
that a programmer may select an object from the Class Name drop down list and then
select a method from the Declarations drop down list to have the Method signature
automatically inserted
Auto-wireup of events. VB.NET has the Handles syntax for events, which connects
event handlers to object variables rather than to objects.
Firing of events is done with the RaiseEvent keyword, giving the IDE the chance to
show a list of available events to pick from. RaiseEvent implicitly checks if there are
any event handlers wired up. (in C# raising an event is syntactically identical to calling a
procedure, and it requires an additional line of code to check for wired event handlers)
Delegates for events don't need to be declared. They are implicitly declared in the
declaration of the events.
Referring to an object using an unqualified dot reference, using the With ... End With
structure
XML Literals
Inline date declarations, e.g. #12/31/2000#
Module (although C#'s static classes with additional semantics, but each field must be
individually declared as static)
Members of Modules imported to the current file, can be accessed with no preceding
container accessor
The My namespace.
COM components and interoperability was more powerful in VB.NET, as the Object type
is bound at runtime;however, C# 4.0 added the dynamic type, which functions as a late-
bound form of Object
Namespaces can be imported at the project level, so that they don't have to be imported
into each individual file, as in C#
Definition of conditional compiler constants
Property methods may take parameters
Properties can be passed to methods with ByRef parameters (ref parameters in C#). In
C# you have to write three additional instructions: Declare a variable, copy the property
value into the variable and copy the variable back to the property after the method call.
Enums can be defined inside interfaces
Case statements may contain inequality expressions, like Is >= 3. (in C# this can be
mimicked by nested Else and If statements)
Overloads keyword specifies that a property or procedure redeclares one or more
existing properties or procedures with the same name within the same class or the base
class. (the lack of this keyword in C# might lead to inadvertent overloading)
Implements keyword to indicate which interfaces a class member implements. In C# a
similar syntax exist, but it is optional and it can only be applied if the member
implements a single interface.
Like operator for pattern comparison of strings in a much simpler way than using regular
expressions. (in C# the same is available with the
Microsoft.VisualBasic.CompilerServices.LikeOperator.LikeString method, but not as a
handy language key word)
Return statement is not required. Return can also be done by assigning the value to the
function
Visual basic has built in constants like vbCrLf and vbTab
No out parameter modifier exists, because in VB all variables are automatically
initialised.
The MyClass keyword behaves like an object variable referring to the current instance of
a class as originally implemented. MyClass is similar to Me, but all method calls on it are
treated as if the method were NotOverridable.
MyBase.New is used to explicitly call a base class constructor from a derived class
constructor.
The My feature provides easy and intuitive access to a number of .NET Framework
classes, enabling the Visual Basic user to interact with the computer, application, settings,
resources, and so on.
Local variables (i.e. variables declared inside of a procedure) are automatically
initialized.
Local variables can be declared with the Static modifier in order to preserve their value
between calls to the procedure.
The Default declaration makes a property an index and able to use the shorter syntax for
collection retrevals like MyCollection(5). C# has a similar construct but it can only
declare a single default indexer. In VB one could, for instance, have two indexers
MyCollection(5) or MyCollection("Bob") on a collection with Integer keys and
String values.
C# lacks the DirectCast (mapping to a single CLR instruction), strict type conversion
can be achieved by the as operator which includes an additional runtime error protection.
C# lacks the End statement which abruptly terminates an application.
Multi-line comments. In VB this is handled in the Visual Studio IDE editor, which adds
comment markers to selections.
Static classes (classes which cannot contain any non-static members, although VB.NET's
Modules are essentially static classes with additional semantics)
Can use checked and unchecked contexts for fine-grained control of overflow/underflow
checking
Iterative for-loops can contain multiple conditionals, such as for(int i = 0; i < 10
&& somethingTrue; i++). This is a legacy of C, where the for statement is basically
syntactic sugar for a while statement.
The getter and setter of a property may implement separate interfaces. In VB you'd have
to define two properties instead: a read-only property implementing one interface, and a
write-only property implementing the other interface.
Implicit interface implementation
Can use the coalesce operator ?? to return the first non-null value (ex. null ?? null ??
1 returns 1). VB.NET would have to use nested If operators.
Pointers (in the unsafe context)