Input - How to get a value from a database cell into a row?

I would like to know how can I get a value from a cell from a sql database? example table name: login Columns:

Name | Birthday Ina 19 string name = "Select Name from login"; string connection= "connection string path"; SqlConnection connect = new SqlConnection(connection); SqlCommand command = new sqlCommand(name,connect); string get = command; 

but I'm sure this is wrong :(

I would like to know how to get the value?

Thanks,

+4
source share
2 answers

Do you want command.ExecuteScalar()

 string get = command.ExecuteScalar().ToString(); 

You may have to be careful with the ability to get a null value from the command.

+3
source

you may need to add a parameter if there are several rows in this table.

 string nameval = string.empty; string name = "Select Name from login"; string connection= "connection string path"; SqlConnection connect = new SqlConnection(connection); SqlCommand command = new sqlCommand(name,connect); using(connect) { var data = command.ExecuteScalar(); if(data!=null) {nameval = data.ToString();} } 
+1
source

Source: https://habr.com/ru/post/1400245/


All Articles