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 4

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

This sould work....
  Permalink  
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  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 1,518
1 OriginalGriff 503
2 Manas Bhardwaj 393
3 Tadit Dash 310
4 Damith Weerasinghe 290
0 Sergey Alexandrovich Kryukov 9,575
1 OriginalGriff 5,856
2 Peter Leow 4,405
3 Maciej Los 3,540
4 Abhinav S 3,513


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