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