Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can we use in windows c# form the "where" clause with the parameter from object in c#
for fill a datagridview
example :
SQL
SELECT         Value, IdCategory, CreateDate, Description, IdUser
FROM            profit
WHERE        (IdUser = USER)



where "USER" is variable in c#



Thank you!
Posted
Updated 1-Aug-12 9:55am
v2
Comments
[no name] 1-Aug-12 15:57pm    
What is the problem exactly?
kespinko 1-Aug-12 16:02pm    
the problem is : I want to select only records with userId = user witch user is in form app right now (like a session).I already can insert data with UserId from logged User but can't view only needed records.I see All users Records.I want see only logged user's records.
Understand me :) . I'm a beginner
sorry for bad explanation
[no name] 1-Aug-12 16:18pm    
You complete;y missed the point. What is the exact prolem that you are having? You cannot connect to your database? You cannot construct a query? You do not know how to pass a variable to your query? You do not know how to create a parameterized query? Where is the code that demonstrates your problem?
obp42 1-Aug-12 16:11pm    
Have you actually connected your database yet? If so, what are you using to make the connection?

If you haven't, here's a link that should provide a good starting place:
http://www.codeproject.com/Articles/43438/Connect-C-to-MySQL
kespinko 1-Aug-12 16:24pm    
I have connection to my DB.Like i told i can insert records.I don't how to select them with where clause.I know how to create Usual select. But don't know how to get a variable from c# and use with "clause" like a parameter name. the code Up in page is the code from gridview "add query".

My connection is :

class ConnectionString
{

public string getString()
{
string server = "localhost";
string database = "HFM";
string uid = "root";
string password = "";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

return connectionString;
}


But i think i dont Have any problem with connection.

Start here[^].
 
Share this answer
 
Your string is going to look something like this:
C#
String query = "SELECT Value, IdCategory, CreateDate, Description, IdUser FROM"
  + " profit WHERE IdUser = '" + USER + "'";

Make sure you add the single quotes around your user variable like I did.

Then you will want to do something like:
C#
// your connection (conn) has been made at this point
using(MySqlCommand cmd = new MySqlCommand(query, conn))
{
  cmd.CommandText = query;
  MySqlDataReader reader = cmd.ExecuteReader();

  while(reader.Read())
  {
    // assign results to a variable, probably an array of strings
    // using reader["Value"], reader["IdCategory"], etc...
  }

  reader.Close();
  conn.Close();
}


This is assuming you're using MySql Connect/NET, since you haven't specified. I have basically just provided a simplified version of the article in the link I gave in the comments. It's a good pretty good example - between what I've given you and that article you should be able to get this done.

Here's the link again:
Connect C# to MySQL[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900