简体   繁体   English

我如何在C#中使用concat sql函数?

[英]how can i use the concat sql function in c#?

i used this statement to combine 2 column values. 我使用此语句来组合2列值。

select concat(l_name," ",f_name) as "Full Name" from students;

i tried this statement in dreamcoder and it worked fine but when i used this statement in my c#, it gave me an error of no method overloading. 我在dreamcoder中尝试了此语句,但效果很好,但是当我在C#中使用此语句时,它给我一个没有方法重载的错误。 it seems that the concat() is considered a method overload when i put it in c#. 当我将concat()放在c#中时,似乎concat()被认为是方法重载。 is there any possible way that i could use this statement in my c#? 有什么办法可以在我的C#中使用此语句?

public DataTable ExecuteQuery(string cmdtext)
{
 DataTable table = new DataTable();
 var refDataAdapter = new MySqlDataAdapter(new MySqlCommand(cmdtext, CN));
 refDataAdapter.Fill(table);
 return table;
}
dataGridView1.DataSource = ExecuteQuery("select concat(l_name,", ",f_name) as FullName from students");

i used this code to fill show the table on my datagridview 我使用此代码填充了在datagridview上显示表格

I think the problem is the delimiter. 我认为问题是分隔符。 In SQL, you should be using single quotes anyway for a constant string, so you can use: 在SQL中,对于常量字符串,无论如何都应使用单引号,因此可以使用:

"select concat(l_name, ', ', f_name) as FullName from students"

for your query. 供您查询。

That would be: 那将是:

dataGridView1.DataSource = ExecuteQuery("select concat(l_name, ', ', f_name) as FullName from students");

You can also use this : 您也可以使用:

SELECT l_name+' '+f_name AS FullName FROM [Students];  

Its better to save query in string and pass string to Respected Function ie 最好将查询保存为字符串并将字符串传递给受尊重的函数,

string query = @"SELECT l_name+' '+f_name AS FullName FROM [Students]";   
dataGridView1.DataSource = ExecuteQuery(query);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM