Three universal code

zhaozj2021-02-16  54

public SqlConnection connection_database (string server, string database, string userid, string password) {string str_con; str_con = "server =" server ";" "database =" database ";" "user id =" userid " " "

Password = " password; try {sqlconnection sqlcon = new sqlConnection (STR_CON); sqlcon.open ();

Return Sqlcon;} catch {messagebox.show ("Database connection is not successful! Please fill in the database settings correctly," Error ", MessageBoxButtons.

OK, MessageBoxicon.Stop; Application.exit (); // Application Exit Return Null;}}}}}}}}}} Returns the view of the corresponding table. {IF (Sqlconn.State == ConnectionState .Closed) sqlconn.Open (); SqlDataAdapter myCommand = new SqlDataAdapter (logString, sqlconn); DataSet ds = new DataSet (); try {myCommand.Fill (ds, " tablename ");} catch {MessageBox.Show ( " ");} DataView DV = new dataView (); DV = DS.TABLES [" Tablename "]. defaultview; return DV;} public void execute_sql (string sql, sqlconnection sqlconn) // General execution SQL function {//// Open the database connection if (sqlconn.state == connectionState.Open) sqlconn.close (); sqlConn.open (); sqlcommand cmd = new sqlcommand (sql, sqlconn); try {cmd.executenonQuery ();} catch (SQLException ae ) {MessageBox.show (ae.Message.Tostring ());} cmd.dispose ();} public void show_listview_data (string select_str, string tablename, listview lv) // universal return

Tree view function {// lv = new listview (); lv.clear (); this.sqldataadapter1 = new sqldataadapter (select_str, this.sqlconn); sqldataadapter1.fill (Dataset21, " TableName "); // Add column name For (int i = 0; i

} // Add line (database data) for (int J = 0; j

Tostring ()); item.text = dataset21.tables [" Tablename "]. Rows [J] [0] .tostring (); for (int i = 1; i

Tostring ());} lv.view = view.details;

}

转载请注明原文地址:https://www.9cbs.com/read-23857.html

New Post(0)