C#可視化程序設計第三章(3,4)

異常處理

1.異常處理介紹程序員

■任何完美的應用程序和技術高明的程序員,都不能保證程序絕對不出差錯。算法

■在代碼中加入異常處理能夠捕獲末知的異常,並對異常進行處理,能夠防止應用程序的崩潰,sql

並向用戶友好地給出錯誤提示。數據庫

 

2.程序中的錯誤能夠分爲三類:ui

■編譯錯誤:spa

在程序編譯時產生的錯誤,一般由錯誤的語法或使用的資源不存在等緣由引發。產生編譯錯誤時程序沒法3d

運行,編譯錯誤能夠經過集成開發環境VS發現。調試

■邏輯錯誤:code

邏輯錯誤,不影響程序的正常運行,但會致使實際運行結果與預期結果不-致。一般是算法出現了錯誤,server

能夠經過調試程序,對比指望值和實際值發現。

■異常:

在程序運行過程當中,干擾程序正常運行時產生的錯誤稱爲異常。異常是沒法避免的,可是可以預計。爲了

使應用程序出現異常時不至於崩潰終止,必須對可能出現異常的代碼進行異常處理。

異常處理結構:

//try...catch結構
//語法:
try{
  //可能出現的異常代碼
}
catch (Exception e){
  //捕獲異常代碼,進行異常處理的代碼
}

//try...catch...finally結構
try{
  //可能出現的異常代碼
}
catch (Exception e){
  //捕獲異常代碼,進行異常處理的代碼
}
finally{
  //釋放代碼
}

//try.......finally結構
try{
  //可能異常代碼
}
finally{
  //釋放代碼
}

其中的Exception類的對象包含異常詳細信息的對象,Exception 對象的經常使用屬性有:

(1) Message: 包含輔助性文字說明,指出拋出異常的緣由。

(2) Source:包含生成異常的程序集名稱。

注意:

■try塊不能省略,catch和finally塊能夠省略, 但不能同時省略

■finally塊不管是否發生異常,都會被執行。

            //定義鏈接字符串
            SqlConnection conn = null;
            SqlDataReader reader = null;
            try
            {
                string sql = "server=.;database=GMP;uid=sa;pwd=985199";//鏈接字符串
                conn = new SqlConnection(sql);//創對象
                conn.Open();//打開
                if (conn.State == System.Data.ConnectionState.Open)
                {
                    Console.WriteLine("鏈接成功");
                    string cz = "select Name,Grader,Emal,Address from Users"; //sql語句
                    //建立command對象
                    SqlCommand cmd = new SqlCommand(cz, conn);
                    reader = cmd.ExecuteReader();//執行sql命令返回sqldatareader對象
                    int i = 1;//設序號
                    Console.WriteLine("ID\t姓名\t性別\t郵箱\t\t地址");
                    while (reader.Read())
                    {
                        Console.WriteLine(i + "\t{0}\t{1}\t{2}\t\t{3}", reader["Name"], reader["Grader"], reader["Emal"], reader["Address"]);
                        i++;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (conn != null)
                {
                    conn.Close();
                }
            }

3.DBHelper類

 

 class DBHelper
    {  
        /// <summary>
        /// 
        /// </summary>
        //類裏面只能是屬性,方法,定義字段
        public static string cons = "";
        public static  SqlConnection conn ;
        //三個方法:鏈接數據庫方法,執行增刪改方法,執行查詢方法
        //1  
        public static bool connect() {
            conn = new SqlConnection(cons);
            conn.Open();
            if (conn.State == System.Data.ConnectionState.Open)
            {             
                return true;
            }
            else {             
                return false;
            }
            
        }
        //2
        public static int Excut(string sqlstr) {
            SqlCommand cmd = new SqlCommand(sqlstr, conn);
            int i = cmd.ExecuteNonQuery();         
            return i;
        }
        //3
        public static SqlDataReader Excutread(string sqlstr) {
            SqlCommand cmd = new SqlCommand(sqlstr,conn);
            SqlDataReader  reader = cmd.ExecuteReader();  
            return reader;
        }
        public static void gbi() {
            conn.Close();
        }
    }

 

 static void Main(string[] args)
        {
            DBHelper.cons = "server=.;database=GMP;uid=sa;pwd=985199";
            string yj = "select Name from Users";
            if (DBHelper.connect()) {
                SqlDataReader reader = DBHelper.Excutread(yj);
                while (reader.Read()) {
                Console.WriteLine(reader["Name"]);
            }
                DBHelper.gbi();
            }
            }

/

相關文章
相關標籤/搜索