如何用C#编写标准代码? [英] How To Write Standard Code In C#?

查看:72
本文介绍了如何用C#编写标准代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有6个月的C#经验.

但是我不知道如何用C#编写标准代码.每一次,我都在为诸如添加更新,删除之类的操作编写连接编码.

我的示例代码.


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...

推荐答案

一种改善代码的方法是使用 ^ ].它可以以某种方式阻止您进行SQL注入攻击.
One way to improve your code is to use Parameters[^]. It somehow prevents you from SQL injection attacks.


您可以将其抽象到一个类中,该类完成打开连接并实际运行指定查询的大部分工作,但是您会发现它由于每个查询都将是唯一的,因此很难抽象出实际的查询以及您可能想要设置的任何参数.您可以通过派生新类并使用对您有用的方法来减轻这种痛苦,但是这种类通常对于每个应用程序都是唯一的.
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.


我有自最近2年以来一直使用.NET 2008.我可以举一个使用连接的代码示例,因为我在许多应用程序中都使用过它.可以使用未绑定控制执行诸如添加更新删除之类的操作.只是尝试按照我写的东西.如果遇到任何其他困难,您可以通过ritwesh.chatterjee@gmail.com向我发送邮件.
这是我的示例:
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 :
/* 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.
}



上面的代码建立与Microsoft Access数据库的连接.每个程序的连接编码都相同.但是要执行诸如update,add,delete之类的操作,您必须在命令文本中使用它们各自的sql编码.我在下面为您提供了此类sql编码的列表:

用于将行添加到数据库表中



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

cmd.CommandText="Insert into [table_name] values ([values_for_inserting_as_per_columns_from_left_to_right.]";
cmd.ExecuteNonQuery();


在上面的编码中,您必须按照表中的列从左到右插入值.是的,一件重要的事.您必须给每个值以逗号(,)分隔,并将字符串值括在单引号('''')中;对于数字值,请不要使用任何引号,但请确保将数据保留为数字格式与数据表列中的格式完全相同,并在写入值后使用.ToString().其他所有编码均应遵循此格式.

删除行:


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 :

cmd.CommandText = "Delete from [table_name] where [column_for_criteria] = [value]";
cmd.ExecuteNonQuery();



希望对您有帮助.



I hope this helps you.


这篇关于如何用C#编写标准代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆