MySQL select result save into c# variable


Can you please check my code and answer me how can i save a mysql select result into a c# string?

try { MySqlDataReader reader = null; string selectCmd = "SELECT * FROM TabelaUtilizatori"; MySqlCommand cmd = new MySqlCommand(selectCmd, _dbConnect.getConnection()); string result = cmd.ExecuteScalar().ToString(); label2.Text = result; } catch (Exception ex) { MessageBox.Show(ex.Message); }

or maybe this way?

using (_dbConnect.getConnection()) { MySqlCommand command = new MySqlCommand( "SELECT NAME FROM TABELAUTILIZATORI WHERE EMAIL = 'asdjaa@asdka.com'" _dbConnect.getConnection()); MySqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { label2.Text= } reader.Close(); }

thanks a lot !


Yes,You can use ExecuteReader() for this..

try { MySqlDataReader reader = null; string selectCmd = "SELECT * FROM TabelaUtilizatori"; MySqlCommand command = new MySqlCommand(selectCmd, _dbConnect.getConnection()); reader = command.ExecuteReader(); while (reader.Read()) { string ColumnName = (string)reader["ColumnName"]; } } catch (Exception ex) { MessageBox.Show(ex.Message); }


You have to use a different api. ExecuteReader is more appropriate here and it will allow you to iterate through all results. An example can be found here :

<a href="https://msdn.microsoft.com/en-us/library/haa3afyz%28v=vs.110%29.aspx" rel="nofollow">https://msdn.microsoft.com/en-us/library/haa3afyz%28v=vs.110%29.aspx</a>

Note that there is no single result with this, there are multiple columns and multiple records, so these don't fit in a single string. Unless you want something like select count(*)

Also note that it is not considered good practice to have code like this select * from ... it is better to specify which columns you really need. Your application will be less likely to break if the datamodel changes.

Also read about using parameters in a query :

<a href="http://www.csharp-station.com/Tutorial/AdoDotNet/lesson06" rel="nofollow">http://www.csharp-station.com/Tutorial/AdoDotNet/lesson06</a>


  • Unit Test for n tier architecture
  • Call SQL Function using ADO .NET
  • ExecuteScalar SQLException: scalar variable not declared
  • Looking for the best way to use ExecuteScalar()
  • Multiple lines of text in UISegmentedControl
  • Jenkinsfile - agent matching multiple labels
  • JavaFX ContextMenu how do I get the clicked Object?
  • Getting value from radiobox in Tkinter - Python
  • adding item to window Extjs 4
  • Basler USB Camera byte buffer to image conversion
  • Why does it draw lines in the wrong place?
  • Converting a WriteableBitmap image ToArray in UWP
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • NHibernate Validation Localization with S#arp Architecture
  • Why value captured by reference in lambda is broken? [duplicate]
  • How can I send an e-mail from a vbs script
  • Why is the size of this struct 32?
  • Volley JsonObjectRequest send headers in GET Request
  • Accessing IRQ description array within a module and displaying action names
  • Controls, properties, events and timers running in design time
  • javascript inside java/jsp code
  • Sending data from AppleScript to FileMaker records
  • MySQL WHERE-condition in procedure ignored
  • Javascript convert timezone issue
  • Web-crawler for facebook in python
  • Android Studio and gradle
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • SQL merge duplicate rows and join values that are different
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • trying to dynamically update Highchart column chart but series undefined
  • Load html files in TinyMce
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Getting Messege Twice Using IMvxMessenger
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • LevelDB C iterator
  • apache spark aggregate function using min value
  • Can't mass-assign protected attributes when import data from csv file
  • How can i traverse a binary tree from right to left in java?
  • java string with new operator and a literal