Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi all,

I am 6th month experience in C#.

But i don't know how to write Standard Code In C#. Each and evey time i am writing connection coding for operations like add update delete.

my Code for Example.


string MyConString = ConfigurationManager.ConnectionStrings["College_Management_System.Properties.Settings.cmsConnectionString"].ConnectionString;
                MySqlConnection connection = new MySqlConnection(MyConString);
                MySqlCommand command = connection.CreateCommand();
                MySqlDataReader Reader;
                command.CommandText = "select * from " + datatable + " where code ='" + rtvalue + "'";
                connection.Open();
                Reader = command.ExecuteReader();
                while (Reader.Read())
                {
                    textBox1.Text = Reader[1].ToString();
                    textBox2.Text = Reader[2].ToString();
                }
                connection.Close();


Help me...
Posted

One way to improve your code is to use Parameters[^]. It somehow prevents you from SQL injection attacks.
 
Share this answer
 
You could abstract that out into a class that does most of the work of opening the connection and actually running the specified query, but you will find it difficult to abstract out the actual query and any parameters you might want to set since each query is going to be unique. You could ease that pain by deriving from your new class and having methods that do THAT work for you, but such a class is generally going to be unique to every application.
 
Share this answer
 
Comments
walterhevedeich 30-Jul-11 8:16am    
Excellent advice.
Hi, I have been using .NET 2008 since the last 2 yrs. I can give you the example of a code using connection as i have used it in many of my applications. Actions like add update delete can be per formed using unbound control. Just try to follow what I write. In case any more difficulty you can mail me at 'ritwesh.chatterjee@gmail.com'.
Here is my example :
C#
/* this is what my variables stand for 
OledbConnection con;
OledbCommand cmd;
OledbDataReader dr;
*/
con = new OledbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data source = [as required]");
con.open();
cmd=con.CreateCommand();
cmd.CommandText="Selct * from [table_name]";
dr=cmd.ExecuteReader();
if(dr.Read())
{
  /*Type here what you want to do after the connection is established for example: this.maskedTextBox1.Text=dr[0].ToString(); */
}
else
{
  //Type code as required.
}


The above code establishes connection with the data base of microsoft access. The connection coding will be same for every program. But to perform operations like update add delete you have to use their respective sql coding in the Command Text. I am giving you below a list of such sql coding :

FOR ADDING ROWS INTO THE DATABASE TABLE
C#
cmd.CommandText="Insert into [table_name] values ([values_for_inserting_as_per_columns_from_left_to_right.]";
cmd.ExecuteNonQuery();

In the above coding you have to insert values from left to right as per your columns in the table. Yes, one important thing. You have to give every value separated by a comma(,)and enclose the string values in single quotes(' ') and for numeric values don't use any quotes but make sure that you keep the data in the numeric format exactly as your format in the column of the data table and use .ToString() after writing the value. This format is to be followed for all other coding.

FOR DELETING A ROW :
C#
cmd.CommandText = "Delete from [table_name] where [column_for_criteria] = [value]";
cmd.ExecuteNonQuery();


I hope this helps you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900