一印度学生Asp.net源码分享讨论

来源:互联网 发布:淘宝好评大全50字 编辑:程序博客网 时间:2024/05/16 11:08

  本文原标题:Asp.net源码程序分析所感——印度,一个不可轻视的近邻!)www.hsdje.com

  最近在国外Asp.net网站晃悠,搜集到了不少宝贝,觉得不错的就汉化调试一下发到51aspx与大家分享,一般代码美洲和欧洲的朋友发布比较多,这些朋友写的有个特点那就是比较粗狂,用的Asp.net技术比较罕见或者前卫(也许是我掌握的肤浅)。

  一直听说印度的软件业比较发达,而且平民化程度也比较高,我以前其实是不以为然的。直到前天了一个叫做Timmy M.John印度大学生朋友写的Asp.net程序才让我改变了这个看法,也深刻体会到了了印度软件业扎实的基础,还是言归正传看看那个代码吧,是采用Asp.net2.0()开发的一个大学课程管理系统,是现在商用程序http://www.indiastudychannel.com/的一个雏形,主要功能:实现大学课程的搜索,用户注册后可以自行添加课程,可以通过后台管理大学以及所属二级学院等。下面是程序抓图(注:已经liudao汉化调试)

  下面是类结构图hsdje.com

  程序的功能方面实现起来并不是很轻松,但是这位印度朋友(Timmy M.John)实现起来思路清晰、简单明了,不像好多朋友一样一个简单的程序弄得很复杂,云山雾罩的。

  M.John使用的是面向对象开发,我把几个积累代码给大家看看吧,先看一下数据库操作类:

  DataManager.cs

  namespace IndiaStudyChannel.Utils

  {

  /**////

  /// Summary description for DataManager.

  ///

  /// 由 liudao 翻译整理

  /// 该源码自www.51aspx.com(51aspx.com)

  public class DataManager

  {

  public DataManager()

  {

  }

  public static DataTable ExecuteQuery(string query)

  {

  string connectionString = System.Configuration.ConfigurationSettings.AppSettings["connectionString"];

  SqlConnection connection = new SqlConnection(connectionString);

  connection.Open();

  try

  {

  SqlDataAdapter adapter = new SqlDataAdapter(query, connection);

  DataSet ds = new DataSet();

  adapter.Fill(ds);

  return ds.Tables[0];

  }

  finally

  {

  if ( connection.State == ConnectionState.Open )

  connection.Close();

  }

  }

  public static void ExecuteNonQuery(string query)

  {

  string connectionString = System.Configuration.ConfigurationSettings.AppSettings["connectionString"];

  SqlConnection connection = new SqlConnection(connectionString);

  connection.Open();

  try

  {

  SqlCommand cmd = new SqlCommand();

  cmd = connection.CreateCommand();

  cmd.CommandType = CommandType.Text;

  cmd.CommandText = query;

  cmd.ExecuteNonQuery();

  }

  finally

  {

  if ( connection.State == ConnectionState.Open )

  connection.Close();

  }

  }

  public static object ExecuteScalar(string query)

  {

  string connectionString = System.Configuration.ConfigurationSettings.AppSettings["connectionString"];

  SqlConnection connection = new SqlConnection(connectionString);

  connection.Open();

  //www.51aspx.com

  try

  {

  SqlCommand cmd = new SqlCommand();

  cmd = connection.CreateCommand();

  cmd.CommandType = CommandType.Text;

  cmd.CommandText = query;

  return cmd.ExecuteScalar();

  }

  finally

  {

  if ( connection.State == ConnectionState.Open )

  connection.Close();

  }

  }

  }

  }

  把常用的sql方法写成一个类,看起来非常清晰,功能简单。大家常用的的SqlHelper类既有过程又有sql语句实现的方法,让新手一看就晕(我现在偶尔晕晕)~~

  通用函数类(字符串处理等)

  namespace IndiaStudyChannel.Utils

  {

  /**////

  /// Summary description for Utils.

  ///

  /// 由 liudao 翻译整理

  /// 该源码自www.51aspx.com(51aspx.com)

  public class Utils

  {

  public Utils()

  {

  }

  /**////

  /// This method removes some dangerous characters from the word to avoid 'Sql Injection attack'.

  ///

  ///

  ///

  public static string MakeSafeWord(string s)

  {

  if ( s == null )

  return "";

  return s.Replace("'", "").Replace("--", "");

  }

  /**////

  /// This method checks if the passed user id is an adinistrator or if this is same as current user.

  ///

  ///

  ///

  public static bool IsOwner(object userId)

  {

  if ( System.Web.HttpContext.Current.Session["CurrentUser"] == null )

  {

  // There is no userid saved in session. This means user has not logged in.

  return false;

  }

  // Get current user from session.

  string currentUser = System.Web.HttpContext.Current.Session["CurrentUser"].ToString();

  // Get the admin user id from config file.

  string adminUser = System.Configuration.ConfigurationSettings.AppSettings["AdminUser"];

  if ( currentUser == adminUser )

  {

0 0