Answered by:
Best method in c# to check if a column of a database table contains data

Question
-
User1036972001 posted
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
Thursday, July 23, 2020 9:15 AM
Answers
-
User475983607 posted
The ExecuteScalar() method solves this programming problem. ExecuteScalar() returns the first item the results set.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, July 23, 2020 12:48 PM -
User-939850651 posted
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:
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
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, July 24, 2020 3:10 AM
All replies
-
User475983607 posted
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?
Thursday, July 23, 2020 12:00 PM -
User1036972001 posted
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 }
I ask if there is a method other than this...
Thursday, July 23, 2020 12:45 PM -
User475983607 posted
The ExecuteScalar() method solves this programming problem. ExecuteScalar() returns the first item the results set.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, July 23, 2020 12:48 PM -
User-939850651 posted
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:
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
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, July 24, 2020 3:10 AM