.net的架構模式

來源:互聯網
上載者:User

標籤:

一:ADO.NET實現三層架構

     不用三層的普通的查詢寫法:

  

 string sql = string.Format("select * from Studnet where StuName like ‘%{0}%‘", txtName.Text);            string strconn = "server=.;database=MySc;uid=sa;pwd=123456";            SqlConnection conn = new SqlConnection(strconn);            SqlDataAdapter da = new SqlDataAdapter("select * fromStuden", conn);            DataSet ds = new DataSet();            try            {                da.Fill(ds);               // dataGridView1.DataSource = ds.Tables[0];                DataTable dt = ds.Tables[0];                txtName.Text = dt.Rows[0]["Name"].ToString();            }            catch (Exception)            {                throw;            }

 普通刪除的寫法:

  SqlConnection conn = new SqlConnection("server=.;database=MySc;uid=sa;pwd=123456");            string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();            string sql = "delect from Student where stNO=" + id;            SqlCommand comm = new SqlCommand(sql,conn);            conn.Open();

 修改的普通寫法:

   string strsql = @"UPDATE Student SET [StuName]=‘0‘ WHERE [StuNO]={3}";            string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();            string sql = string.Format(strsql, txtName.Text, id);            SqlConnection conn = new SqlConnection("server=.;database=MySc;uid=sa;pwd=123456");            SqlCommand com = new SqlCommand(sql, conn);            try            {                conn.Open();                com.ExecuteNonQuery();                //重新整理資料            }            catch (Exception)            {                throw;            }            finally {                conn.Close();            }

 一個簡單的DbHelper:

 private static SqlConnection conn = new SqlConnection("server=.;database=Mys,uid=sa;pwd=123456");       public static int ExecuteSQL(string strsql)       {           SqlCommand comm = new SqlCommand(strsql, conn);           try           {               conn.Open();               return comm.ExecuteNonQuery();           }           catch (Exception ex)           {               throw ex;           }           finally {               conn.Close();           }       }       public static int GetDataTable(string strsql)       {           SqlDataAdapter da = new SqlDataAdapter(strsql, conn);           DataSet ds = new DataSet();           try           {               da.Fill(ds);               return ds.Tables[0];           }           catch (Exception ex)           {                              throw ex;           }                           }       /// <summary>       /// 返回一個單一的值       /// </summary>       /// <param name="strsql">傳入的是select打頭的sql語句(select count(*),select max(..))</param>       /// <returns>返回的是Object類型</returns>       public static object GetSingle(string strsql)       {           SqlCommand comm = new SqlCommand(strsql, conn);           try           {               conn.Open;               return comm.ExecuteScalar();           }           catch (Exception ex)           {               throw ex;           }           finally {               conn.Close();           }       }

 

 

二:用OOP實現三層架構

三:應用抽象工廠+反射實現通用資料來源的設計

.net的架構模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.