Schema mode for. Net

Source: Internet
Author: User

One: three-tier architecture implemented by ADO

Do not use the three-layer common query notation:

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;            }

Common deletion:

  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 ();

General wording of the modification:

   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 ();                Refresh Data            }            catch (Exception)            {                throw;            }            finally {                Conn. Close ();            }

A simple dbhelper:

 Private StaticSqlConnection conn =NewSqlConnection ("server=.; database=mys,uid=sa;pwd=123456");  Public Static intExecuteSQL (stringstrSQL) {SqlCommand Comm=NewSqlCommand (strSQL, conn); Try{Conn.               Open (); returnComm.           ExecuteNonQuery (); }           Catch(Exception ex) {Throwex; }           finally{Conn.           Close (); }       }        Public Static intGetdatatable (stringstrSQL) {SqlDataAdapter da=NewSqlDataAdapter (strSQL, Conn); DataSet DS=NewDataSet (); Try{da.               Fill (DS); returnDs. tables[0]; }           Catch(Exception ex) {Throwex; }                           }       /// <summary>       ///returns a single value/// </summary>       /// <param name= "strSQL" >The incoming SQL statement (select COUNT (*), select Max (. ))</param>       /// <returns>The object type is returned</returns>        Public Static ObjectGetsingle (stringstrSQL) {SqlCommand Comm=NewSqlCommand (strSQL, conn); Try{Conn.               Open; returnComm.           ExecuteScalar (); }           Catch(Exception ex) {Throwex; }           finally{Conn.           Close (); }       }

Second: three-tier architecture with OOP

Three: The application of abstract Factory + reflection to achieve a common data source design

Schema mode for. Net

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.