Your question is not clear. A column can have many rows and I'm not sure what a "first query select" is. Can you share the C# code, DDL and DML?
I need to verify if the column `col_name` from table of MySQL database is empty or data contains
MySqlCommand cmd = new MySqlCommand("select `col_name` from `dotable` where id = 148", con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
int i = ds.Tables[0].Rows.Count;
if (i > 0) {
// Exist
// display the data of `col_name` in a textbox with the possibility to modify it
}
else {
// Not exist
// show the empty and ready to compile texbox
}
According to your description, I can confirm that the field allows NULL values, have you tried to use the
IFNULL() Function in mysql?
It can query the field value of NULL and replace it with the value you want, such as an empty string.
Something like this:
select IFNULL(`col_name`,'') col_value FROM `dotable` where id= 148
Determine whether the queried value is equal to the empty string, and then make the corresponding processing.
I think it will meet your requirements.
Best regards,
Xudong Peng
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
22 Points
29 Posts
Best method in c# to check if a column of a database table contains data
Jul 23, 2020 09:15 AM|Koopers|LINK
Hi,
I'm trying to figure out which is the fastest and best method in c# to check if a column of a database table contains data
if the column contains data, I have to display this data in a textbox with the possibility to modify it
else I have to show the empty and ready to compile texbox
to do this in the past I have always used a first query select and it depends on the result I see or not the data
is this the only method?
I use MySql RDBMS 8.0.12
thanks in advance for any suggestion
All-Star
53131 Points
23682 Posts
Re: Best method in c# to check if a column of a database table contains data
Jul 23, 2020 12:00 PM|mgebhard|LINK
Your question is not clear. A column can have many rows and I'm not sure what a "first query select" is. Can you share the C# code, DDL and DML?
Member
22 Points
29 Posts
Re: Best method in c# to check if a column of a database table contains data
Jul 23, 2020 12:45 PM|Koopers|LINK
I need to verify if the column `col_name` from table of MySQL database is empty or data contains
I ask if there is a method other than this...
All-Star
53131 Points
23682 Posts
Re: Best method in c# to check if a column of a database table contains data
Jul 23, 2020 12:48 PM|mgebhard|LINK
The ExecuteScalar() method solves this programming problem. ExecuteScalar() returns the first item the results set.
https://dev.mysql.com/doc/dev/connector-net/8.0/html/M_MySql_Data_MySqlClient_MySqlCommand_ExecuteScalar.htm
Contributor
2120 Points
676 Posts
Re: Best method in c# to check if a column of a database table contains data
Jul 24, 2020 03:10 AM|XuDong Peng|LINK
Hi Koopers,
According to your description, I can confirm that the field allows NULL values, have you tried to use the IFNULL() Function in mysql?
It can query the field value of NULL and replace it with the value you want, such as an empty string.
Something like this:
Determine whether the queried value is equal to the empty string, and then make the corresponding processing.
I think it will meet your requirements.
Best regards,
Xudong Peng