Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Proper syntax to retrieve field from SQL table?

In Code Behind, What is proper select statement syntax to retrieve the @BName field from a table?
Using Visual Studio 2003
SQL Server DB

I created the following parameter:
Dim strName As String
Dim parameterBName As SqlParameter = New SqlParameter("@BName", SqlDbType.VarChar, 50)
parameterBName.Value = strName
myCommand.Parameters.Add(parameterBName)

I tried the following but get error:
Dim strSql As String = "select @BName from Borrower where BName= DOROTHY V FOWLER "

error is:
Line 1: Incorrect syntax near 'V'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Line 1: Incorrect syntax near 'V'.

Source Error:

Line 59:
Line 60:
Line 61: myCommand.ExecuteNonQuery() 'Execute the query
[1056 byte] By [kenn_rosie] at [2007-11-11 8:26:29]
# 1 Re: Proper syntax to retrieve field from SQL table?
First of all, it looks like @BName is the name of a parameter, not a field; I don't think SQL Server allows field names that begin with '@'. Second, you're trying to execute a query (a SQL statement that returns one or more records), but you're calling ExecuteNonQuery to do it. ExecuteNonQuery is intended for SQL statements (such as INSERT, UPDATE and DELETE commands) which do not return records.

See if this helps: http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson06.aspx
Phil Weber at 2007-11-11 21:47:27 >
# 2 Re: Proper syntax to retrieve field from SQL table?
Sorry: To further clarify
My database table name is borrower and two of the fields are BName and TaxID, I am trying to select a particular borrower (BName) with a taxid OF 767, lets say in this case THE BORROWER IS Bill Jones

I declared the following variables:
Dim strName As String
Dim parameterBName As SqlParameter = New SqlParameter("@BName", SqlDbType.VarChar, 50)
parameterBName.Value = strName
myCommand.Parameters.Add(parameterBName)
Dim strTaxID As String
Dim ParameterTaxID As SqlParameter = New SqlParameter("@TaxID", SqlDbType.VarChar, 15)
ParameterTaxID.Value = strTaxID
myCommand.Parameters.Add(ParameterTaxID)

I inserted the folowing command:

Dim strSql As String = "select BName from Borrower where TaxID= @333-00-1492 "

I get error:
Must declare the variable '@333'.
kenn_rosie at 2007-11-11 21:48:30 >
# 3 Re: Proper syntax to retrieve field from SQL table?
Did you read the tutorial to which I linked in my previous post? What do you notice about the relationship between the parameter names and the SQL query?
Phil Weber at 2007-11-11 21:49:29 >
# 4 Re: Proper syntax to retrieve field from SQL table?
I wish I knew...I am doing this in VB and there is a difference between C# and VB. Thanks for trying to help me
kenn_rosie at 2007-11-11 21:50:29 >
# 5 Re: Proper syntax to retrieve field from SQL table?
The SQL syntax has nothing to do with C# or VB; it's the same for both.
Phil Weber at 2007-11-11 21:51:34 >