Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting data out of DB - How 1

Status
Not open for further replies.

vpekulas

Programmer
Jan 8, 2002
154
0
0
CA
How can I get a simple return of data from my DB using ASP.NET (VB.NET). In classic ASP I'd do something like this:

Code:
SQL = "SELECT COUNT(ID) AS C_COUNT FROM table"
Set RS = Server.CreateObject("ADODB.Recordset")
RS.Open SQL, MyConn	
	 IF NOT RS.EOF THEN
	 	COUNTER = trim(RS("C_COUNT"))
	 END IF
RS.Close 

SQL = "SELECT SUM(fldNUM) AS S_SUM FROM table"
Set RS = Server.CreateObject("ADODB.Recordset")
RS.Open SQL, MyConn	
	 IF NOT RS.EOF THEN
	 	SUMMARY = trim(RS("S_SUM"))
	 END IF
RS.Close 

Set RS = Nothing


What would be the .NET equivalent to that ?

"Taxes are the fees we pay for civilized society" G.W.
 
I am a total newbie to ASP.net, but i was trying to do the same thing from an access databse into a datagrid, and i found this code...

__________________________________________________________

<%@ Page Language="vb" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDB" %>
<script runat="server">

Sub Page_Load(Source as Object, E as EventArgs)
Dim strConn as string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
server.mappath(".\bla\db1.mdb") & ";"
Dim MySQL as string = "Select field1, field2," & _
"field3 from table1"
Dim MyConn as New OleDBConnection(strConn)
Dim Cmd as New OLEDBCommand(MySQL, MyConn)
MyConn.Open()
MyDataGrid.DataSource = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
MyDataGrid.DataBind()
End Sub

</script>
<html>
<head>
<title>Listing</title>
<meta content="ASP Express 2.0" name="GENERATOR" />
</head>
<body>
<form method="post" runat="server">
<asp:Datagrid id="MyDataGrid" runat="server" GridLines="Both" cellpadding="0" cellspacing="0" Headerstyle-BackColor="#3366CC" Headerstyle-Font-Name="Arial" Headerstyle-Font-Bold="True" Headerstyle-Font-Size="11" BackColor="#CCCCCC" Font-Name="Arial" Font-Bold="True" Font-Size="8" BorderColor="#3366CC"></asp:Datagrid>
</form>
</body>
</html>

__________________________________________________________

Maybe that wont help, but i modded it to meet my needs.
 
Yes, that'd be too easy :)
I'm looking for a solution not to bound it to a control.
There will be only 1 row returned (summary, average, count etc.) so there is no need to use a Datagrid.

&quot;Taxes are the fees we pay for civilized society&quot; G.W.
 
Hi there!
Take a look at OleDbDataReader or SqlDataReader.
Here's a simple example:
Code:
Dim MyConn as SqlConnection = New SqlConnection("...your connection string...")
Dim Cmd as SqlCommand = New SqlCommand(MySQL, "...your sql query...")
Dim MyDr as SqlDataReader
MyConn.Open()
MyDr = Cmd.ExecuteReader();
While MyDr.Read()
 Response.Write(MyDr.Item("...column name/index from db..."))
End While
MyConn.Close()

Hope this helps!

[morning]
 
For single values being returned from a database, you should use OleDbCommand.Scalar()

There is no shortage of examples for this type of operation in the docs and all over the internet.

-p

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top