Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL-Server-2008
I can not find a WORKING example using a variable data element in the sql select from where clause.
 
example select * from table where fielda = "123" is not what I want to do.
 
string myselection = "123";
 
select * from table where fielda = myselection is what I want
 
I am using C# and visual studio 2008 and sql server 2008.
 
thanks.
 
[email removed for your protection]
 
[Modified: First, I added the C# tag...second, I removed your email...if someone posts a response to this, you'll get an email...posting your email only servers to help spammers know where to send stuff]
Posted 30-Mar-10 10:22am
Edited 30-Mar-10 10:41am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

One simpler method for it looks like you need:
SET @mySelection = '123'
SELECT * FROM myTable WHERE fielda = @mySelection
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

ok...so do
 
string SQL = "SELECT * FROM table WHERE fielda='" & myselection & "';"
 
or
 
SqlCommand nonqueryCommand = thisConnection.CreateCommand();
 
nonqueryCommand.CommandText = "SELECT * FROM table WHERE fielda=@fielda;";
nonqueryCommand.Parameters.Add("@fielda", SqlDbType.VarChar, 30);
 
[Updated: sorry, I missed a parameter in the Add. It should read
nonqueryCommand.Parameters.Add("@fielda",SqlDbType.Int).Value = 123;
//or something similar
 
And what's with the downvoting?]
  Permalink  
v3
Comments
CS2011 at 19-May-11 2:27am
   
Not sure why some one will down vote this answer.Seems good enough to me. my 5+
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

select * from table where fielda = '" & myselection & "' ";
 
generates the following error that I can not get rid of.
 
field initializer cannot reference the non-static field, method, or property 'Client_Maintenance.AddressLoad.myselection' C:\TARE SYSTEM\Client Maintenance\AddressLoad.cs 49 164 Client Maintenance
 
I appreciate your help.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

string myselection = "123";
 
// fielda is char,varchar,nvarchar,etc...
string SQL = String.Format("select * from table where fielda = '{0}'", myselection); 
 
// fielda is numeric...
string SQL = String.Format("select * from table where fielda = {0};", myselection);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Use this
 
string SQL = @"SELECT * FROM table WHERE fielda='" + myselection + "'"
 

This sould work....
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 225
1 Sergey Alexandrovich Kryukov 167
2 _Amy 155
3 CHill60 130
4 Manfred R. Bihy 130
0 OriginalGriff 7,320
1 Sergey Alexandrovich Kryukov 6,043
2 Maciej Los 3,754
3 Peter Leow 3,448
4 CHill60 2,572


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 18 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100