登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

璀璨星空&旖旎花園gegei.com

★╰→流星劃過夜空,不僅是為了帶來祝福,同時也是為了追求幸福!

 
 
 

日志

 
 

C#生成日志记录和事件  

2007-08-15 10:22:56|  分类: 程序编程 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

 

一、调试工具:生成日志记录和事件查看库,以帮助调试基于 .NET 框架的应用程序

请到微软中文主页看具体的详细说明:

网址:

http://www.microsoft.com/china/MSDN/library/netFramework/netframework/NFLoggingTool.mspx?mfr=true

注意要下载本文的代码:LoggingTool.exe(131KB)

必须安装和解压才可以见到文件和代码。

 

 

二、C#(winForm)写在公共类的几个常用方法

/*****************************************************************************      说明

** 1.获取数据库的连接,返回值需判断是否为null-----------GetSqlConnection

*  2.根据Select--查询语句,返回DataSet-------------------GetDataSet

*  3.使用数据库内容填充DataGrid----------------FillDataGridFromSQLString

*  4.使用数据库内容填充DataGrid----------------FillDataGridFromSQLString(重载)

*  5、返回SQL语句所查询出来的行数-------------------------------GetRowCount

*  6.填充下拉列表------------------------------------------FillComboBox*   

   7.由一条SQL语句生成一个DataReader;返回值需要判断是否为空------GetDataReader

*  8.返回单个查询数据:第一列,第一行的值-------------------GetFirstData

*  9.对数据库中的一条记录操作:增、删、更新---------------ExecuteCommand

*  10.对数据库进行增删改操作-----------------------------ExecuteCommand2

*  11.判断str是不是全是由数字构成-------------------------IsNumeric

*  12.检测含有中文字符串的实际长度------------------------len

***************************************************************************/

using System;

using System.Data.SqlClient;

using System.Data;

using System.Windows.Forms;

namespace Tayside.Common

{

   /// <summary>

   /// DataBase 的摘要说明。

   /// </summary>

   public class DataBase

   {

      public DataBase()

      {

      }

      /// <summary>

      /// 1.获取数据库的连接,返回值需判断是否为null

      /// </summary>

      /// <returns></returns>

      public static SqlConnection GetSqlConnection()

      {

         string strCnn = "Server=192.168.12.136;database=Tayside;user id=sa;password=";

         try

         {

            SqlConnection sqlCnn = new SqlConnection(strCnn);

            sqlCnn.Open();

            return sqlCnn;

         }

         catch(Exception ee)

         {

            string temp=ee.Message;

            return null;

         }

      }

      /// <summary>

      /// 获取SqlCommand对象

      /// </summary>

      /// <returns></returns>

      public static SqlCommand GetSqlCommand()

      {

         SqlConnection sqlCnn = GetSqlConnection();

         if(sqlCnn == null)

            return null;

         else

         {

            SqlCommand sqlCmm = new SqlCommand();

            sqlCmm.Connection = sqlCnn;

            return sqlCmm;

         }

      }

      /// <summary>

      /// 2.根据Select--查询语句,返回DataSet

      /// </summary>

      /// <param name="strSql">Select SQL语句</param>

      /// <returns>返回值需判断是否为空</returns>

      public static DataSet GetDataSet(string strSql)

      {

         try

         {

            using( SqlConnection sqlCnn = GetSqlConnection() )

            {

               SqlDataAdapter dataAdapter = new SqlDataAdapter( strSql, sqlCnn );

               DataSet dataSet = new DataSet();

               dataAdapter.Fill( dataSet );

               return dataSet; 

            }

         }

         catch

         {

            return null;

         }

      }

      /// <summary>

      /// 3.使用数据库内容填充DataGrid

      /// </summary>

      /// <param name="dataGrid">要填充的DataGrid</param>

      /// <param name="strSql">要获取数据库内容的SQL字符串</param>

      /// <returns></returns>

      public static bool FillDataGridFromSQLString( DataGrid dataGrid,string strSql)

      {

         try

         {

            DataSet ds = GetDataSet(strSql);

            dataGrid.SetDataBinding(ds, "");

            return true;

         }

         catch(Exception ee)

         {

            string t=ee.Message;

            return false;

         }

      }

      /// <summary>

      /// 4.使用数据库内容填充DataGrid

      /// </summary>

      /// <param name="dataGrid">要填充的DataGrid</param>

      /// <param name="strSql">要获取数据库内容的SQL字符串</param>

      /// <param name="table">要添充DataGrid的表名</param>

      /// <returns></returns>

      public static bool FillDataGridFromSQLString( DataGrid dataGrid,string strSql,string table)

      {

         try

         {

            DataSet ds = GetDataSet(strSql);

            dataGrid.SetDataBinding(ds, table);

            return true;

         }

         catch(Exception ee)

         {

            string t=ee.Message;

            return false;

         }

      }

      /// <summary>

      /// 5、返回SQL语句所查询出来的行数

      /// </summary>

      /// <param name="strSql"></param>

      /// <returns></returns>

      public static int GetRowCount(string strSql)

      {

         DataSet ds = GetDataSet(strSql);

         int count = ds.Tables[0].Rows.Count;

         return count;

      }

      /// <summary>

      /// 6.填充下拉列表

      /// </summary>

      /// <param name="cmBox">要添充的ComboBox</param>

      /// <param name="strSql">查询语句</param>

      /// <returns>是否成功</returns>

      public static bool FillComboBox(ComboBox cmBox,string strSql)

      {

         try

         {

            using(SqlConnection sqlCnn = GetSqlConnection())

            {

               SqlDataReader dr = GetDataReader(strSql);

               while(dr.Read())

               {

                  cmBox.Items.Add(dr.GetValue(0));

               }

               return true;

            }

         }

         catch

         {

            return false;

         }

      }

      /// <summary>

      /// 7.由一条SQL语句生成一个DataReader;返回值需要判断是否为空

      /// </summary>

      /// <param name="strSql">要使用的SQl语句</param>

      /// <returns></returns>

      public static SqlDataReader GetDataReader(string strSql)

      {

         try

         {

            SqlConnection sqlCnn = GetSqlConnection();

            SqlCommand sqlCmm = new SqlCommand(strSql,sqlCnn);

            return sqlCmm.ExecuteReader(CommandBehavior.CloseConnection);

         }

         catch

         {

            return null;

         }

      }

      /// <summary>

      /// 8.返回单个查询数据:第一列,第一行的值

      /// </summary>

      /// <param name="strSql">Select SQL语句</param>

      /// <returns></returns>

      public static string GetFirstData(string strSql)

      {

         try

         {

            using( SqlConnection sqlCnn = GetSqlConnection() )

            {

               SqlCommand sqlCmm = new SqlCommand(strSql,sqlCnn);

               return sqlCmm.ExecuteScalar().ToString();

            }

         }

         catch

         {

            return "";

         }

      }

      /// <summary>

      /// 9.对数据库中的一条记录操作:增、删、更新

      /// </summary>

      /// <param name="strSql">要执行的SQL语句</param>

      /// <returns>返回执行是否成功</returns>

      public static bool ExecuteCommand(string strSql)

      {

         try

         {

            using( SqlConnection sqlCnn = GetSqlConnection())

            {

               SqlCommand sqlCmm = new SqlCommand(strSql,sqlCnn);

               int temp = sqlCmm.ExecuteNonQuery();

               return  temp == 1;

            }

         }

         catch

         {

            return false;

         }

      }

      /// <summary>

      /// 10.对数据库进行增删改操作

      /// </summary>

      /// <param name="strSql"></param>

      /// <returns></returns>

      public static bool ExecuteCommand2(string strSql)

      {

         try

         {

            using( SqlConnection sqlCnn = GetSqlConnection())

            {

               SqlCommand sqlCmm = new SqlCommand(strSql,sqlCnn);

               int temp = sqlCmm.ExecuteNonQuery();

               return  true;

            }

         }

         catch

         {

            return false;

         }

      }

      /// <summary>

      /// 11.判断str是不是全是由数字构成

      /// </summary>

      /// <param name="str"></param>

      /// <returns></returns>

      public static bool IsNumeric(string str) 

      { 

         if (str==null || str.Length==0) 

            return false; 

         foreach(char c in str) 

         { 

            if (!Char.IsNumber(c)) 

            { 

               return false; 

            } 

         } 

         return true; 

      } 

      /// <summary>

      /// 12.检测含有中文字符串的实际长度

      /// </summary>

      /// <param name="str">字符串</param>

      public static int len(string str)

      {

         System.Text.ASCIIEncoding n = new System.Text.ASCIIEncoding();

         byte[] b = n.GetBytes(str);

         int l = 0; // l 为字符串的实际长度

         for (int i=0;i <= b.Length-1;i++)

         {

            if (b[i] ==63) //判断是否为汉字或全脚符号

            {

               l++;

            }

            l++;

         }   

         return l;

      }     }

}

 

2007年08月15日

参考

http://dzh.mop.com/mainFrame.jsp?url=http://dzh.mop.com/topic/readSub_7636086_0_0.html

 

  • 喷鹤网 www.penhe.com ,

    C生成日志记录和事件 - 网际飞星 - 璀璨星空旖旎花園gegei.com鸽给搜索,Pigeons to search (www.Gegei.com), Pigeon Give Search,

  • 作者:網際飛星

  •  

      评论这张
     
    阅读(1274)| 评论(0)

    历史上的今天

    评论

    <#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
     
     
     
     
     
     
     
     
     
     
     
     
     
     

    页脚

    网易公司版权所有 ©1997-2018