c# - Check the duplicate entering -


I am using this code to enter.

  Private Zero Button 1_Click (Object Sender, EventArgs E) {String Coonstring = "datasource = localhost; port = 3306; username = root; password = ****; charset = UTF 8" ; String cmd = "Enter in project.name_registry (name) values ​​('" + this.txt.Text + "' ');"; MySql Connection Connection Database = New MySqlConnection (Coonstring); MySqlCommand cmddata = New MySqlCommand (CMD, connectionDatabase); MySqlDataReader myreader; Try {connectionDatabase.Open (); Myreader = cmddata.ExecuteReader (); MessageBox.Show ("full"); While (Myrider. Reid ()) {}} Hold (Exception Pre) {Message Box. Show (ex.Message); }}  

I need to press on this button to see if the name of the joining is found, the message box appears, then tell that my name is present and stop adding. If I have not been involved then tell me how I can do this.

Regards

The best place to check this is in the database only All databases can set a unique barrier on a field. If you set the name column to be unique in name_registry , DBMS will not add another entry with the same name, and the exception will be thrown. This will usually be the best way.

If the DB is not in your hands and you can not set the column to be unique, then you can suggest that @Fransissukert and others have the name given to DB, and only , if it returns the result of 0:

  SELECT COUNT (*) name _ registry WHERE [name] = 'TheName'  

Note, however there is no need to call ExecuteReader , not for the selection of this single-result statement, nor to the above INSERT For the statement - you should call ExecuteScalar , which will return a single-value result without loading a full-value DataReader that you do not really need.

And lastly, in addition to this, I can not tell you well at the discretion that you should go to ADO.NET at least directed to use parametric queries, Which not only helps prevent the attacks of SQL injection, but also helps to clean code and make it more readable, in my opinion. There are many


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -