asp.net2.0中的些許技巧和文章

 

      水晶報表的關鍵代碼   

 

    sqlstr = "select * from opensrc";
        //得到DataTable
        DataTable dt = dbas.CurrentDt(sqlstr);
        //數據綁定到crs
        crsrc.ReportDocument.Load(Server.MapPath("crp.rpt"));
        crsrc.ReportDocument.SetDataSource(dt);
        crsrc.DataBind();
   //綁定到crv
        crview.ReportSource = crsrc;
        crview.DataBind();

webconfig 配置 

   <appSettings>
     <add key="CrystalImageCleaner-AutoStart" value="true" />
     <add key="CrystalImageCleaner-Sleep" value="60000" />
     <add key="CrystalImageCleaner-Age" value="120000" />
 </appSettings>
 <connectionStrings>
  <!--鏈接SQL Server數據庫的鏈接字符串-->
  <add name="testConn" connectionString="data Source=nethawk;database=test;user id=sa;pwd=hawk001" providerName="System.Data.SqlClient"></add>
 </connectionStrings>

 

  • 轉換運算符定義了一種從一個類類型到另外一個類類型的轉換
  • 隱式轉換自動調用,顯式轉換需要通過一個強制轉換來調用
  • 隱式轉換隻用於不會發生錯誤的情況
    1. 不能發生丟失信息(截斷、溢出、或丟失符號等)
    2. 轉換不會拋出異常
  • 使用轉換運算符的限制
    1. 不能創建內建的轉換。例如,不可以重定義從doubleint的轉換。
    2. 不能定義源類型或目標類型是object的轉換
    3. 不能對源或目標類型同時進行implicitexplicit的轉換
    4. 不能定義一個從基類到派生類的轉換
    5. 不能定義源類型或目標類型是一個接口的轉換
  • 使用方法:
    public static operator implicit target-type(source-type v) { return value; }

    public static operator explicit target-type(source-type v) { return value; }

 

 

  • 代碼

    using System;
    namespace Convert
    {
          class ThreeD
          {
                int x, y, z;
                public ThreeD()
                {
                      x = y = z = 0;
                }
                public ThreeD(int a, int b, int c)
                {
                      this.x = a;
                      this.y = b;
                      this.z = c;
                }
                public static ThreeD operator +(ThreeD d1,ThreeD d2)
                {
                      ThreeD result = new ThreeD();
                      result.x = d1.x + d2.x;
                      result.y = d1.y + d2.y;
                      result.z = d1.z + d2.z;
                      return result;
                }
                public static implicit operator int(ThreeD op1)
                {
                      return op1.x * op1.y * op1.z;
                }
                public void show()
                {
                      Console.WriteLine(x +"," + y + ","+z);
                }
     }
     class Class1
     {
                static void Main(string[] args)
                {
                      ThreeD a = new ThreeD(1,2,3);
                      ThreeD b = new ThreeD(10,10,10);
                      ThreeD c = new ThreeD();
                      int i;
                      a.show();
                      b.show();
                      //將A和B相加
                      c = a + b;
                      c.show();
                      //轉化爲int
                      i = a;
                      Console.WriteLine("Result of i = a :" + i);
                      //轉化爲int
                      i = a * 2 -b;
                      Console.WriteLine("Result of i = a * 2 - b :" + i);    
                }
     }
    }

  •  
  • 命名空間定義了一個範圍

    using 命名空間;
    namespace 命名空間 {
    //成員
    }

  • using

    using name;

    using alias = name;

  • 命名空間是可以添加的,就是說可以聲明2個名字一樣的命名空間。這樣,一個命名空間可以分佈在一個項目的多個文件中。
  • 命名空間是可以嵌套,就是A可以包含B,如果要用B裏的類,必須要用A做限定。 
  • 在C#中,事件是什麼,用什麼關鍵字來創建。
    事件是程序中某個變化的通知。關鍵字event來創建事件。
  • 代理和事件的聯繫
    使用代理可以指定一個事件處理程序。
  • 事件可以多播嗎?
    是的,事件可以多播。
  • 聲明

    event event-delegate-name object-name;

 

  • 代碼

    using System;
    using System.Collections.Generic;
    using System.Text;
    namespace EventDemo
    {   //爲事件聲明一個代理,注意返回是void
        delegate void EventHandle();
        //聲明事件類
        class myEvent
        {
            //聲明一個事件
            public event EventHandle activate;
            //調用fire()方法來觸發activate這個事件
            public void fire()
            {
                if (activate != null)
                    activate();
            }       
        }
        class Program
        {
            //與代理返回結果和參數一致,可以被添加到事件鏈表中
            static void Handle()
            {
                Console.WriteLine("Event occured!");
            }
            static void Main(string[] args)
            {
                //創建事件實例對象
                myEvent evt = new myEvent();
                //把Handle()添加到事件鏈中
                evt.activate += new EventHandle(Handle);
                //觸發事件
                evt.fire();
            }
        }
    }

  •  
  • 什麼是代理?使用代理有什麼好處?
    代理是引用方法的對象。
    只有在運行時,才能使用代理調用已知方法。
  • 申明代理:

    delegate 返回值 代理名稱(參數列表);

  • 多播
    在一次代理調用方法鏈上的所有方法。
  • 代碼:

    using System;
    namespace ConsoleApplication
    {
         delegate string del(string str);
         class A
         {
              public string instance(string str)
             {
                    return str + " is called by Instance.";
              }
           }
           class Class1
           {
                   static string method(string str)
                   {
                        return str + " is called by Method.";
                   }
                   static void Main(string[] args)
                   {     //方法代理,申明代理實例時候要加初始化方法名
                         del dMethod = new del(method);
                         Console.WriteLine(dMethod("woody"));
                         //實例代理,先創建實例,再申明代理時指明方法名(實例.方法名)
                         A b = new A();
                         del dInstance = new del(b.instance);  
                         Console.WriteLine(dInstance("Woody"));
                    }
            }
    }

using System;
namespace delegateMuti
{     //多重代理只能用於返回值爲void的方法
      delegate void del();
      class Class1
      {
            static void Microsoft()
            {
                  Console.Write("Microsoft ");
            }
            static void Visual()
            {
                  Console.Write("Visual ");
            }
            static void C()
            {
                  Console.Write("C# ");
            }
            static void Net()
            {
                  Console.Write(".NET");
            }
            static void Main(string[] args)
            {
                  del del1 = new del(Microsoft);
                  del del2 = new del(Visual);
                  del del3 = new del(C);
                  del del4 = new del(Net);
                  del plus = del1;
                  plus += del2;
                  plus += del3;
                  plus += del4;
                  del sub = plus -del4 - del3 -del2;
                  del1();
                  del2();
                  del3();
                  del4();
                  Console.WriteLine("/n");
                  plus();
                  Console.WriteLine("/n");
                  sub();
                  Console.WriteLine("/n");
                  }
              }
}

DataReader對象
      使用DataReader對象檢索Mircosoft Access的“地址薄”數據庫的“家庭成員”數據表

using System;
//自己添加數據庫操作引用
using System.Data.OleDb;
namespace UsingDataReader
{
 class Class1
 {
  static void Main(string[] args)
  {   
    string myConStr = "Provider = Microsoft.Jet.OLEDB.4.0;";
    myConStr    += "Data Source = addrbook.mdb;";
    OleDbConnection myCon = new OleDbConnection(myConStr);
    myCon.Open();
    string myComStr = "Select * from 家庭成員";
    OleDbCommand myCom = new OleDbCommand(myComStr,myCon); 
    OleDbDataReader myReader = myCom.ExecuteReader();   
    while(myReader.Read())
    {
     Console.WriteLine(myReader.GetInt32(0).ToString()+"/t"+myReader.GetString(2)+"/t"+myReader.GetString(3));
    }
    myReader.Close();
    myCon.Close();    
  }
 }
}

      同時檢索Microsoft SQL Server 的Northwind數據庫中的customers表和employees表

using System;
//自己添加的數據庫操作引用
using System.Data.SqlClient;
namespace UsingDataReader2tables
{
 class Class1
 {
  static void Main(string[] args)
  {
   string Str = "Server = woodycmp; database = Northwind;";
    Str   += "user id = sa; password = sa;";
   SqlConnection Con = new SqlConnection(Str);
   string comStr = "select Address, city from customers where city = 'Paris';";
      comStr += "select lastname, firstname from employees where Hiredate < '1993-5-1'";
   SqlCommand Com = new SqlCommand(comStr, Con);
   Con.Open();
   SqlDataReader rd = Com.ExecuteReader();
   do
   {
    Console.WriteLine("/n");
    Console.WriteLine("/t{0}/t{1}",rd.GetName(0),rd.GetName(1));
   while(rd.Read())
   Console.WriteLine("/t{0}/t{1}",rd.GetString(0),rd.GetString(1));
   }while(rd.NextResult());
   rd.Close();
   Con.Close();
  }
 }
}

      關鍵字:ADO.NET對象,ADO.NET控件,數據綁定和數據綁定控件。
     
      使用ADO.NET開發數據庫應用程序應遵循一下步驟:

  1. 選擇.NET Framework數據提供程序;
  • SQL Server .NET Framework數據提供程序   System.Data.SqlClient   >= Sql Server 7.0

       應該算是第二個星期了,我上次和公司的經理在開發日誌裏說我要進入公司的軟件開發部,現在已經是了,2個星期了,做了什麼,我實在是有點擔心,因爲自己草率的學了下C#,缺的是實際的開發經驗,還有所謂的的自己來寫代碼,公司做的多半是信息管理類的系統,說白了就是操作數據庫,但是我連連接數據庫也不會,確切的說,不是不會,是項目如果好幾個人一起做的話,就要有一定的規範,不是說你總自己獨立的寫個類來連接數據庫,有個公共的類,讓大家去調用,我還沒來得及去研究它是怎麼樣構造的,我要開始寫代碼了。
      先是靜態的,能做的先做,比方說一個數據庫表的字段編輯,給你的是幾個現成的窗體,所謂的沒有代碼的,只有界面,我就把下拉的固定字段先填好,這樣就是打字員,我還用可憐的智能ABC。據說,高手基本是不用5筆的,我就是討厭用5筆,因爲是自己老是有幾個字打不出來,切換來切換去,還不如直接用智能ABC,況且我的語文基礎是頂刮刮的好。
      一個字段一個字段的驗證,就是重複的寫if語句,判斷是否爲空嗎?如果是數字,是不是數字?如果是年份,是不是是4位的數字?如果是覆蓋度,那就要判斷是0-100不?還有所謂的鬱閉度,是不是0-1不?然後就是複製,粘貼,複製,粘貼,寫代碼原來就是這樣。真是TMD幸福。
      我連基本的字符轉數值也不會,不過現在會了。
      轉的話,有什麼所謂,隱式轉(範圍小轉到範圍大的),顯式轉(範圍大的轉到小的,可能溢出),還有強制轉。
     

Convert.ToDouble(strnig value);


      裏面有18個參數,基本能轉就轉了,而我用的基本是控件的Text屬性。
      就是什麼:
     

Convert.ToDouble(this.txt_nd.Text.Trim().ToString());


      反正值沒變,外面的條件限制給它變了。
      還有就是數據庫的連接,基本步驟是什麼 建立連接,寫執行命令,然後開通,執行,關閉連接。這個要好好研究了,我寫的東西,還基本是人家寫的,失敗。
      別急嘛,寫照貓畫虎啊。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章