Discuz教程网

DB.ASP 用Javascript写ASP很灵活很好用很easy

[复制链接]
authicon dly 发表于 2011-9-14 08:34:02 | 显示全部楼层 |阅读模式
  1. <%
  2. function getConfig(config, args) {
  3. if (args) {
  4. for (var proto in args) {
  5. config[proto] = args[proto];
  6. }
  7. }
  8. return config;
  9. }
  10. function getConnection() {
  11. return new ActiveXObject("ADODB.Connection");
  12. }
  13. function getRecordset() {
  14. return new ActiveXObject("ADODB.Recordset");
  15. }
  16. var DB = {};
  17. DB.ConnectionString = 'Provider=Sqloledb;User ID=sa;Password=sa;Initial Catalog=T;Data Source=WWW-D17F81FA113\\SQLEXPRESS;';
  18. //添加 一条记录
  19. DB.Add = function (table, keyValueCol) {
  20. var returnID=null;
  21. var Conn = getConnection();
  22. Conn.Open(DB.ConnectionString);
  23. var Rs = getRecordset();
  24. Rs.Open('select * from '+table+' where 1=2', Conn, 3, 2);
  25. Rs.AddNew();
  26. for (var key in keyValueCol) {
  27. Rs.Fields.Item(key).Value = keyValueCol[key];
  28. }
  29. Rs.Update();
  30. Rs.Close();
  31. Rs = null;
  32. Conn.Close();
  33. Conn = null;
  34. return DB.Get("select IDENT_CURRENT('"+table+"') as ID")["ID"];
  35. }
  36. //修改一条记录
  37. DB.Upd = function (sql, keyValueCol) {
  38. var Conn = getConnection();
  39. Conn.Open(DB.ConnectionString);
  40. var Rs = getRecordset();
  41. Rs.Open(sql, Conn, 3, 2);
  42. for (var key in keyValueCol) {
  43. Rs.Fields.Item(key).Value = keyValueCol[key];
  44. }
  45. Rs.Update();
  46. Rs.Close();
  47. Rs = null;
  48. Conn.Close();
  49. Conn = null;
  50. }
  51. //执行 无返回结果的查询
  52. DB.Exe = function (sql) {
  53. var Conn = getConnection();
  54. Conn.Open(DB.ConnectionString);
  55. Conn.Execute(sql);
  56. Conn.Close();
  57. Conn = null;
  58. }
  59. //获得 一个查询记录
  60. DB.Get = function (sql) {
  61. var _record = null;
  62. var Conn = getConnection();
  63. Conn.Open(DB.ConnectionString);
  64. var Rs = getRecordset();
  65. Rs.Open(sql, Conn, 1, 1);
  66. if (!Rs.EOF) {
  67. _record = {};
  68. for (var i = 0; i < Rs.Fields.Count; i++) {
  69. _record[Rs.Fields.Item(i).Name] = Rs.Fields.Item(i).Value;
  70. }
  71. }
  72. Rs.Close();
  73. Rs = null;
  74. Conn.Close();
  75. Conn = null;
  76. return _record;
  77. }
  78. //批量 获得/添加 数据
  79. DB.Batch = function () {
  80. var Conn = getConnection();
  81. var Rs = getRecordset();
  82. var _Batch = this;
  83. var _table = null;
  84. _Batch.Open = function (sql) {
  85. Conn.Open(DB.ConnectionString);
  86. Rs.Open(sql, Conn, 3, 2);
  87. }
  88. _Batch.Add = function (table , keyValueCol) {
  89. Rs.AddNew();
  90. for (var key in keyValueCol) {
  91. Rs.Fields.Item(key).Value = keyValueCol[key];
  92. }
  93. Rs.Update();
  94. return DB.Get("Select IDENT_CURRENT('"+ table +"') as ID")["ID"];
  95. }
  96. _Batch.Get = function () {
  97. var record_arr = [];
  98. while (!Rs.EOF) {
  99. var _record = {};
  100. for (var i = 0; i < Rs.Fields.Count; i++) {
  101. _record[Rs.Fields.Item(i).Name] = Rs.Fields.Item(i).Value;
  102. }
  103. record_arr.push(_record);
  104. Rs.MoveNext();
  105. }
  106. return record_arr;
  107. }
  108. _Batch.Close = function () {
  109. Rs.Close();
  110. Rs = null;
  111. Conn.Close();
  112. Conn = null;
  113. }
  114. }
  115. //获得 sql 的某页的数据
  116. DB.List = function () {
  117. var _Config;
  118. var _List = this;
  119. _List.Page = {
  120. PS : 20,
  121. AP : 1,
  122. PC : 1,
  123. RC : 1
  124. };
  125. _List.Query = function () {
  126. _Config = new getConfig({
  127. fields : " * ",
  128. table : null,
  129. where : " 1=1 ",
  130. sort : " ID desc ",
  131. pk : " ID "
  132. }, arguments[0]);
  133. _List.Page.RC = DB.Get("select count(" + _Config.pk + ") as [count] from " +
  134. _Config.table + " where " + _Config.where).count;
  135. _List.Page.PC = Math.ceil(_List.Page.RC / _List.Page.PS);
  136. if(_List.Page.AP>_List.Page.PC) _List.Page.AP = _List.Page.PC;
  137. }
  138. _List.Get = function (p) {
  139. p = isNaN(p) ? 1 : parseInt(p);
  140. _List.Page.AP = p;
  141. var sql = '';
  142. if (p > 1) {
  143. sql = "select top " + _List.Page.PS + " " + _Config.fields +
  144. " from " + _Config.table + " where " + _Config.where +
  145. " and " + _Config.pk +
  146. " not in(select top " + (p - 1) * _List.Page.PS + " " + _Config.pk +
  147. " from " + _Config.table + " where " + _Config.where +
  148. " order by " + _Config.sort + ") order by " + _Config.sort;
  149. } else {
  150. sql = "select top " + _List.Page.PS + " " + _Config.fields +
  151. " from " + _Config.table + " where " + _Config.where + " order by " + _Config.sort;
  152. }
  153. var return_arr = null;
  154. var Batch = new DB.Batch();
  155. Batch.Open(sql);
  156. return_arr = Batch.Get();
  157. Batch.Close();
  158. return return_arr;
  159. }
  160. }
  161. //sql 只读
  162. DB.Reader = function () {
  163. var Conn = getConnection();
  164. var Rs = getRecordset();
  165. var _Reader = this;
  166. _Reader.EOF = false;
  167. _Reader.Open = function (sql) {
  168. Conn.Open(DB.ConnectionString);
  169. Rs.Open(sql, Conn, 1, 1);
  170. _Reader.EOF = Rs.EOF;
  171. }
  172. _Reader.Read = function () {
  173. if (!Rs.EOF) {
  174. var _record = {};
  175. for (var i = 0; i < Rs.Fields.Count; i++) {
  176. _record[Rs.Fields.Item(i).Name] = Rs.Fields.Item(i).Value;
  177. }
  178. Rs.MoveNext();
  179. return _record;
  180. } else {
  181. _Reader.EOF = true;
  182. }
  183. }
  184. _Reader.Close = function () {
  185. Rs.Close();
  186. Rs = null;
  187. Conn.Close();
  188. Conn = null;
  189. }
  190. }
  191. %>
复制代码




上一篇:js禁止小键盘输入数字功能代码
下一篇:js动态加载以及确定加载完成的代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-8-2 15:06

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表