SQL Statement

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How can I run a SQL statement from VB.NET

I want to set a text box equal to something retrived from a select statemen

ex

textbox.text=SELECT CAR from AUTOS where AUTO_ID= '" & editAuto_ID.Text & "'

Thanks!
 
Hi Joe,

Have a look at the "sqldatareader" that is for your question the most simple
approach I think.

http://msdn.microsoft.com/library/d...ystemdatasqlclientsqldatareaderclasstopic.asp

If you use oledb it is the same it is only than named oledbdatareader

Cor
Thanks for the resource kit. I am looking through it and it is rather
large. Does anyone know how I can store the results of this type of
statement:
da_Models.SelectCommand.CommandText = "SELECT DESCRIPTION FROM
SERVER_MODEL WHERE MODEL_ID= '" & editMODEL_ID.Text & "'"
 
Joe,

Where is the data? .Net passes SQL select statements to a SQL engine (often
MSDE or SQL Server) but it can be Access or Oracle or any of a number of
others. It is this engine that understands SQL and how to execute a query.
Typically one uses ADO.Net to address the engine so I'd start your search
there.

Tom
 
Is there a way to refernece these fields by name? In VB 6 i could
access the field value with something similar to:

value=rs("Fieldname")


(1) value=rs("Fieldname") is actually value=rs.fields("Fieldname") Note
that you MUST specify all properties in .Net. The fields property is the
DEFAULT property in VB6 ADO. We all got lazy being able to leave this
out.

(2) The ADO recordset object, when persented with a string argument,
traverses itself looking for a match on field name and then using the
index that it located to fetch the value. If you know the index it is
much faster to just go get it. I regularly have tables with well over a
hundred fields. If I have a few thousand rows returned, which is common,
that is a few hundred thousand extra things that needed to be done for me
do get my data.

(3) Don't use SELECT *. I had several customers that applied an update
to their ERP package that rearranged the schema putting all the fields in
alphabetic order. Lots of custom code that assumed the fields to be in a
certain order broke because of SELECT *.

|==========|
| Solution |
|==========|

You can search for the field name yourself:

Dim iIndex as Integer = -1
Dim i as Integer

For i = 0 to myReader.FieldCount -1
If Ucase(sSearchArg) = Ucase(myReader.GetName(i)) Then
iIndex = i
Exit For
End If
Next i

This is what ADO was doing for you in the background anyway.
 
Back
Top