web 2.0

VB.NET & System.Data.SqlClient - DataSet()

VB.NET & System.Data.SqlClient - DataSet() - How to learn Connector/NET ADO.NET component  System.Data.SqlClient  namespace is the .NET Framework Data Provider for SQL Server data source, Create a new DataSet and fill it using the DataAdapter from SQL Server (SQL Server 2000,2005,2008 Database)

ShotDev Focus:
- VB.NET & System.Data.SqlClient - DataSet()

Example

DataSet1.aspx

<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Page Language="VB" %>
<script runat="server">

Sub Page_Load(sender As Object, e As EventArgs)
BindData()
End Sub

Sub BindData()

'*** DataSet ***'
Dim ds As DataSet
ds = CreateDataSet()
myRepeater.DataSource = ds.Tables(0).DefaultView
myRepeater.DataBind()

End Sub

'*** DataSet ***'
Function CreateDataSet() As DataSet
Dim objConn As New System.Data.SqlClient.SqlConnection
Dim objCmd As New System.Data.SqlClient.SqlCommand
Dim dtAdapter As New System.Data.SqlClient.SqlDataAdapter

Dim ds As New DataSet
Dim strConnString,strSQL As String

strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
strSQL = "SELECT * FROM customer"

objConn.ConnectionString = strConnString
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
dtAdapter.SelectCommand = objCmd

dtAdapter.Fill(ds)

dtAdapter = Nothing
objConn.Close()
objConn = Nothing

Return ds   '*** Return DataSet ***'

End Function

</script>
<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Repeater id="myRepeater" runat="server">
<HeaderTemplate>
<table border="1">
<tr>
<th>CustomerID</th>
<th>Name</th>
<th>Email</th>
<th>CountryCode</th>
<th>Budget</th>
<th>Used</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td align="center"><%#Container.DataItem("CustomerID") %></td>
<td><%#Container.DataItem("Name") %></td>
<td><%#Container.DataItem("Email") %></td>
<td align="center"><%#Container.DataItem("CountryCode") %></td>
<td align="right"><%#Container.DataItem("Budget") %></td>
<td align="right"><%#Container.DataItem("Used") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>

</form>
</body>
</html>

Screenshot

VB.NET & System.Data.SqlClient - DataSet()
.
.
.
Download this script.
Download
.
.
.
VB.NET & System.Data.SqlClient - DataSet() - How to create a multiple table in  DataSet .

ShotDev Focus:
- VB.NET & System.Data.SqlClient - DataSet()

Example

DataSet2.aspx

<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Page Language="VB" %>
<script runat="server">

Sub Page_Load(sender As Object, e As EventArgs)
BindData()
End Sub

Sub BindData()
'*** DataSet From DataSet ***'
Dim ds As DataSet
ds = CreateDataSet()

Dim i As Integer
Dim dg As DataGrid
Dim lbl As Label
For i = 0 To ds.Tables.Count - 1
dg = New DataGrid
With dg
.DataSource = ds.Tables(i).DefaultView
.AutoGenerateColumns = True
.DataBind()
End With
lbl = New Label
lbl.Text = "<br><b>" & ds.Tables(i).ToString & "</b><br>"
Me.lblText.Controls.Add(lbl)
Me.lblText.Controls.Add(dg)
dg = Nothing
Next

End Sub

'*** DataSet ***'
Function CreateDataSet() As DataSet
Dim objConn As New System.Data.SqlClient.SqlConnection
Dim objCmd As New System.Data.SqlClient.SqlCommand
Dim dtAdapter As New System.Data.SqlClient.SqlDataAdapter

Dim ds As New DataSet
Dim i As Integer
Dim strConnString,strSQL As String

strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"

Dim TablesName(2) As String
TablesName(0) = "customer"
TablesName(1) = "audit"
TablesName(2) = "country"
For i = 0 To TablesName.GetUpperBound(0)
strSQL = "SELECT * FROM " & TablesName(i) & " "
objConn.ConnectionString = strConnString
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
dtAdapter.SelectCommand = objCmd

dtAdapter.Fill(ds,TablesName(i))
Next

dtAdapter = Nothing
objConn.Close()
objConn = Nothing

Return ds   '*** Return DataSet ***'

End Function

</script>
<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label id="lblText" runat="Server"></asp:Label>
</form>
</body>
</html>

Screenshot

VB.NET & System.Data.SqlClient - DataSet()
.
.
.
Download this script.
Download
.
.
.
VB.NET & System.Data.SqlClient - DataSet() - How to use DataSet and adding new Row to DataSet.

ShotDev Focus:
- VB.NET & System.Data.SqlClient - DataSet()

Example

DataSet3.aspx

<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Page Language="VB" %>
<script runat="server">

Sub Page_Load(sender As Object, e As EventArgs)
BindData()
End Sub

Sub BindData()
'*** DataSet  ***'
Dim ds As DataSet
ds = CreateDataSet()

'*** Adding Rows to DataSet ***'
Dim dr As DataRow
dr = ds.Tables("customer").NewRow()
dr.Item("CustomerID") = "C005"
dr.Item("Name") = "Weerachai Nukitram"
dr.Item("Email") = "webmaster@shotdev.com"
dr.Item("CountryCode") = "TH"
dr.Item("Budget") = "2000000"
dr.Item("Used") = "100000"
ds.Tables("customer").NewRow()
ds.Tables("customer").Rows.Add(dr)

'*** Adding Rows to DataSet ***'

Me.myGridView.DataSource = ds.Tables(0).DefaultView
Me.myGridView.DataBind()

End Sub

'*** DataSet ***'
Function CreateDataSet() As DataSet
Dim objConn As New System.Data.SqlClient.SqlConnection
Dim objCmd As New System.Data.SqlClient.SqlCommand
Dim dtAdapter As New System.Data.SqlClient.SqlDataAdapter

Dim ds As New DataSet
Dim strConnString,strSQL As String

strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"

strSQL = "SELECT * FROM customer "
objConn.ConnectionString = strConnString
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
dtAdapter.SelectCommand = objCmd

dtAdapter.Fill(ds,"customer")

dtAdapter = Nothing
objConn.Close()
objConn = Nothing

Return ds   '*** Return DataSet ***'

End Function

</script>
<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView id="myGridView" runat="server"></asp:GridView>
</form>
</body>
</html>

Screenshot

VB.NET & System.Data.SqlClient - DataSet()
.
.
.
Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (No Ratings Yet)
Loading ... Loading ...

Leave a Reply

You must be logged in to post a comment.