清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.OleDb; using System.Data; namespace AccessOperator { class AccessHelper { private string conn_str = null ; private OleDbConnection ole_connection = null ; private OleDbCommand ole_command = null ; private OleDbDataReader ole_reader = null ; private DataTable dt = null ; /// <summary> /// 构造函数 /// </summary> public AccessHelper() { conn_str = @ "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='D:\我的文档\Visual Studio 2008\Projects\AccessOperator\AccessOperator\bin\Debug\myDb.mdb'" ; InitDB(); } private void InitDB() { ole_connection = new OleDbConnection(conn_str); //创建实例 ole_command = new OleDbCommand(); } /// <summary> /// 构造函数 /// </summary> /// <param name="db_path">数据库路径</param> public AccessHelper( string db_path) { conn_str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + db_path + "'" ; InitDB(); } /// <summary> /// 转换数据格式 /// </summary> /// <param name="reader">数据源</param> /// <returns>数据列表</returns> private DataTable ConvertOleDbReaderToDataTable( ref OleDbDataReader reader) { DataTable dt_tmp = null ; DataRow dr = null ; int data_column_count = 0; int i = 0; data_column_count = reader.FieldCount; dt_tmp = BuildAndInitDataTable(data_column_count); if (dt_tmp == null ) { return null ; } while (reader.Read()) { dr = dt_tmp.NewRow(); for (i = 0; i < data_column_count; ++i) { dr[i] = reader[i]; } dt_tmp.Rows.Add(dr); } return dt_tmp; } /// <summary> /// 创建并初始化数据列表 /// </summary> /// <param name="Field_Count">列的个数</param> /// <returns>数据列表</returns> private DataTable BuildAndInitDataTable( int Field_Count) { DataTable dt_tmp = null ; DataColumn dc = null ; int i = 0; if (Field_Count <= 0) { return null ; } dt_tmp = new DataTable(); for (i = 0; i < Field_Count; ++i) { dc = new DataColumn(i.ToString()); dt_tmp.Columns.Add(dc); } return dt_tmp; } /// <summary> /// 从数据库里面获取数据 /// </summary> /// <param name="strSql">查询语句</param> /// <returns>数据列表</returns> public DataTable GetDataTableFromDB( string strSql) { if (conn_str == null ) { return null ; } try { ole_connection.Open(); //打开连接 if (ole_connection.State == ConnectionState.Closed) { return null ; } ole_command.CommandText = strSql; ole_command.Connection = ole_connection; ole_reader = ole_command.ExecuteReader(CommandBehavior.Default); dt = ConvertOleDbReaderToDataTable( ref ole_reader); ole_reader.Close(); ole_reader.Dispose(); } catch (System.Exception e) { Console.WriteLine(e.ToString()); } finally { if (ole_connection.State != ConnectionState.Closed) { ole_connection.Close(); } } return dt; } /// <summary> /// 执行sql语句 /// </summary> /// <param name="strSql">sql语句</param> /// <returns>返回结果</returns> public int ExcuteSql( string strSql) { int nResult = 0; try { ole_connection.Open(); //打开数据库连接 if (ole_connection.State == ConnectionState.Closed) { return nResult; } ole_command.Connection = ole_connection; ole_command.CommandText = strSql; nResult = ole_command.ExecuteNonQuery(); } catch (System.Exception e) { Console.WriteLine(e.ToString()); return nResult; } finally { if (ole_connection.State != ConnectionState.Closed) { ole_connection.Close(); } } return nResult; } static void Main( string [] args) { AccessHelper Helper = new AccessHelper(); DataTable dt = Helper.GetDataTableFromDB( "select * from test" ); foreach (DataRow dr in dt.Rows) { Console.WriteLine(dr[0].ToString()+ " " +dr[1].ToString()); } Console.WriteLine(Helper.ExcuteSql( "insert into test(test) values ('hello')" )); } } } |