日期:2014-05-17  浏览次数:20708 次

c#新手学习如何使用类
写了一小代码,因为没有学习过OOP,请教大大们,如果这段代码改为用类方法来编写,我应该如何写,两个函数定义为两种方法,如何使用,我应该如何来定义类成员。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
    class Program
    {
        static int getMonth1(DateTime date1, DateTime date2) 
        { 
            int year1 = date1.Year; 
            int year2 = date2.Year; 
            int month1 = date1.Month; 
            int month2 = date2.Month; 
            int a=year2-year1;
            int months;
            if (a == 0)
            {
                months = month2 - month1;
            }
            else 
            {
                months = month2;
            }
            return months; 
        }

        static string mysql (string date1)
        {
            DateTime starttime=Convert.ToDateTime(date1);
            DateTime endtime=DateTime.Now;
            string sql = "select * from zy_bhjfyz" + Convert.ToString(starttime.Year) + starttime.Month.ToString("D2");
            int m,i;
            m = getMonth1(starttime, endtime);
            if (endtime.Year - starttime.Year == 0)
            {
                for (i = 1; i <= m; i++)
                {
                    sql = sql + "union all " + "select  * from zy_bhjfyz" + Convert.ToString(starttime.Year) + starttime.AddMonths(i).Month.ToString("D2");