c# - How To Save A Single SQL Entry As A Variable? -


lets have following database

table name: users


id |  name  |  surname  | 1  |  peter |  parker   | 2  |  hulk  |  hogan    | 

        using (sqlconnection connection = new sqlconnection("..."))         using (sqlcommand command = connection.createcommand())         {             command.commandtext = "select users id='2'";              //......         } 

the following command select hulk hogan entry.

if wanted save surname/name string, how have it?

string surname = ....; surnametextbox.text = surname; // displays - hogan 

thank in advance.

if want multi column use sqldatareader this:

string name=""; string surname="";  using (sqlconnection connection = new sqlconnection("...")) {    connection.open();//don't forget line else "invalidoperationexception"    using (sqlcommand command = connection.createcommand())    {               command.commandtext = "select name,surname users id=2";       using (sqldatareader reader = command.executereader())       {          reader.read();          name = reader.getstring(0);          surname= reader.getstring(1);       }    } }  nametextbox.text = name; surnametextbox.text = surname; 

also if want 1 row 1 column value query. need use executescalar this:

string name="";  using (sqlconnection connection = new sqlconnection("...")) {    using (sqlcommand command = connection.createcommand())    {       connection.open();        command.commandtext = "select name users id=2";        name = command.executescalar().tostring();    } } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -