The most important knowledge learned over the past two days is how to link the database with C. I learned to abstract all the important code in the main function and encapsulate it into a function. This facilitates future calls and reduces the bloated size of the main function.
First, we know that ADO. Net is a disconnected database, which converts all database objects to C # objects.
I created a my database in the database and a star table in my. The table contains three columns: name, sex, and age.
The following function uses C # To perform operations on the Star table over the past two days. The sqlhelper class can be saved and used later.
Using system; using system. data; using system. data. sqlclient; public class helper {public static void main () {string strcon = "Server = .; database = My; uid = sa; Pwd = 1 "; sqlhelper helper = new sqlhelper (strcon ); // ---------------------------------------------------------------- // non-query no variable string sql1 = "delete from star where name = 'lin Zhiling '"; // non-query variable string SQL = "insert into star values (@ name, @ sex, @ age)"; s Qlparameter [] PS = new sqlparameter [] {New sqlparameter ("@ name", sqldbtype. varchar, 20), new sqlparameter ("@ sex", sqldbtype. varchar, 4), new sqlparameter ("@ age", sqldbtype. int, 4)}; PS [0]. value = "Lin Zhiling"; PS [1]. value = "female"; PS [2]. value = 30; // returns // the query statement without the variable string sql2 = "select * from Star ";//---------------**********------------- -- // The query statement has the variable string sql3 = "select * from star where age = @ age"; sqlparameter PS1 = new sqlparameter ("@ age", sqldbtype. int, 4); ps1.value = 38; sqldatareader reader = helper. executequery (sql3, PS1); Using (Reader) {While (reader. read () {console. writeline (Reader ["name"]. tostring () + reader ["sex"]. tostring () + reader ["Age"]. tostring ());}}//------------------------------------------------------------- ---------------- // Helper. close () ;}} public class sqlhelper {private sqlconnection con = NULL; private sqlcommand cmd = NULL; private string connectionstring; Public String connectionstring {get {return this. connectionstring;} set {This. connectionstring = value ;}} public sqlhelper (string strcon) {con = new sqlconnection (strcon); cmd = new sqlcommand (); cmd. connection = con; this. connectionstring = Strcon;}/* execute non-query statement */Public int executenonquery (string SQL, Params sqlparameter [] PARAM) {preparedcommand (SQL, Param); int I = cmd. executenonquery (); con. close (); return I;} public int executenonquery (string SQL) {preparedcommand (SQL, null); int I = cmd. executenonquery (); con. close (); return I;}/* execute the query statement */Public sqldatareader executequery (string SQL, Params sqlparameter [] PARAM) {preparedc Ommand (SQL, Param); Return cmd. executereader ();} public sqldatareader executequery (string SQL) {preparedcommand (SQL, null); Return cmd. executereader ();}/* is used to prepare (initialize) The command object */Public void preparedcommand (string SQL, Params sqlparameter [] PARAM) {/* sqlcommand cmd1 = NULL; if (CMD! = NULL) {cmd1 = cmd;} else {cmd1 = new sqlcommand ();} */CMD. commandtext = SQL; // clear the parameter CMD in parameters. parameters. clear (); If (Param! = NULL) {foreach (sqlparameter P in PARAM) {cmd. parameters. add (p) ;}} con. open ();}/* Open Database */private void open () {con. open ();}/* close database */Public void close () {cmd. dispose (); con. close ();}}