C#MySQL问题与where子句 [英] c# Mysql problem with where clause
问题描述
我们可以在Windows C#中使用带有C#对象中参数的"where"子句吗?
用于填充datagridview
例如:
Can we use in windows c# form the "where" clause with the parameter from object in c#
for fill a datagridview
example :
SELECT Value, IdCategory, CreateDate, Description, IdUser
FROM profit
WHERE (IdUser = USER)
其中"USER"在c#中为变量
谢谢!
where "USER" is variable in c#
Thank you!
推荐答案
在此处开始 [ ^ ].
您的字符串看起来像这样:
Your string is going to look something like this:
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:
// 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();
}
这是假定您正在使用MySql Connect/NET,因为尚未指定.我基本上只是在评论中给出的链接中提供了文章的简化版本.这是一个很好的很好的例子-在我给你的东西和那篇文章之间,你应该可以完成这项工作.
再次是链接:
将C#连接到MySQL [
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[^]
这篇关于C#MySQL问题与where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!