C # Homemade database operation class Code

Source: Internet
Author: User

The source code is as follows:

Using system;using system.data;using system.data.oledb;using system.io;using system.windows.forms;namespace Test{ Class Database {public static bool Database_exists () {if (!            File.exists (Application.startuppath + "\\DataBase.mdb")) {return false;            } else {return true;  }} public OleDbConnection get_conn () {try {string conn_string                = ""; conn_string = "Data source= ' Database.mdb ';                provider=microsoft.ace.oledb.12.0; ";                OleDbConnection db_conn = new OleDbConnection (conn_string);                Db_conn.open ();            return db_conn;                } catch (Exception e) {error.database_error_conn = E.message.tostring ();            return null; }} public OleDbDataReader Select (string select_string) {TRy {if (get_conn () = = null) {return null;                } OleDbCommand db_cmd = new OleDbCommand (select_string, Get_conn ());                OleDbDataReader db_reader = Db_cmd.executereader ();                Db_reader.read ();            return db_reader;                } catch (Exception e) {error.database_error_select = E.message.tostring ();            return null; }} public int action_the_sql (string sql_string) {try {if (Get                _conn () = = null) {return 0;                } OleDbCommand db_cmd = new OleDbCommand (sql_string, Get_conn ());                int line = Db_cmd.executenonquery ();            return line;          } catch (Exception e) {error.database_error_action_the_sql = E.message.tostring ();      return 0;                }} public DataSet GetDataSet (string sql_string, String tableName) {try {                if (get_conn () = = null) {return null;                } OleDbDataAdapter Adapter = new OleDbDataAdapter (sql_string, Get_conn ());                DataSet DS = new DataSet ();                Adapter.fill (DS, tableName);            return DS;                } catch (Exception j) {error.database_error_getdataset = J.message.tostring ();            return null; }} public string GetData (string sql_string) {try {if (Get_con                N () = = null) {return null;                } OleDbCommand db_cmd = new OleDbCommand (sql_string, Get_conn ());                String data = Convert.ToString (Db_cmd.executescalar ());    return data;        } catch (Exception e) {error.database_error_action_the_sql = E.message.tostr                ING ();            return null; }        }    }}


The source code for the wrong (error) class It refers to IS as follows:

Namespace test{    class Error    {public        static string database_error_select;        public static string Database_error_conn;        public static string Database_error_action_the_sql;        public static string Database_error_getdataset;}    }

C # Homemade database operation class Code

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.