JS 调用存储过程传递参数
1 引用 2 #region 程序集 Newtonsoft.Json.dll, v4.5.0.0 3 // E:\Newtonsoft.Json.dll 4 #endregion 5 public DataTable RunProc(string ProcName, string Parameter) 6 { 7 try 8 { 9 db = new DataBaseDoor(GetConnection.getcon()); 10 DataSet ds = new DataSet(); 11 JsonTextReader JR = new JsonTextReader(new StringReader(Parameter)); 12 int i = 0; 13 14 15 while (JR.Read()) 16 { 17 if(JR.Value!=null&&JR.Value.ToString()!=",") 18 { 19 i++; 20 } 21 //JR.TokenType; 22 // JR.Value; 23 // JR.ValueType; 24 } 25 SqlParameter[] para = new SqlParameter[i/2]; 26 27 JsonTextReader JTR = new JsonTextReader(new StringReader(Parameter)); 28 29 int j = 0; 30 string key = ""; 31 string value = ""; 32 while (JTR.Read()) 33 { 34 35 if (JTR.Value != null && JTR.Value.ToString() != ",") 36 { 37 if (j <= i) 38 { 39 if (j % 2 == 0) 40 { 41 42 key = ""; 43 if (JTR.Value != null) 44 { 45 key = JTR.Value.ToString().Replace("!@!", "\""); 46 } 47 } 48 else 49 { 50 value = ""; 51 if (JTR.Value != null) 52 { 53 value = JTR.Value.ToString().Replace("!@!", "\""); 54 para[((j+1)/2)-1] = new SqlParameter("@" + key, value); 55 } 56 } 57 58 } 59 j++; 60 } 61 } 62 db.RunProcedure(ProcName, para, ref ds); 63 64 return ds.Tables[0]; 65 } 66 catch (Exception ex) 67 { 68 throw ex; 69 } 70 finally 71 { 72 db.CloseConnection(); 73 } 74 } 75 //JS调用 以及字符串特殊字符替换规则 76 77 var parameter = "{ ID:" + ID + ",Name:'" + $.trim($('#txt_groupname').val()).replace(/"/ig, '!@!') + "'}"; 78 79 var result = RunNewProc('sp_UserLevel_AddGroupName', parameter); 80 81 82 function RunNewProc(procname, Parameter) { 83 84 if (procname.length > 0) { 85 var returnmsg = ErpAjax("/RunProcedure.aspx?Proc=" + procname + "&Parameter="+Parameter+""); 86 if (returnmsg.length > 0) { 87 return returnmsg; 88 } 89 else { 90 return '操作失败'; 91 } 92 } 93 } 94 95 function ErpAjax(url) { 96 var xmlhttp; 97 if (url.indexOf("?") > 0) { 98 url = url + "&NowDateTimeSign=" + new Date().getTime(); 99 } 100 else { 101 url = url + "?NowDateTimeSign=" + new Date().getTime(); 102 } 103 if (window.ActiveXObject) { 104 xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); 105 } 106 else { 107 xmlhttp = new XMLHttpRequest(); 108 } 109 110 var url1 = url.split('?'); 111 var url2 = url1[1].toString().split('&'); 112 113 var furl = url1[0].toString() + "?"; 114 115 for (var i = 0; i < url2.length; i++) { 116 var j = url2[i].indexOf('='); 117 118 if (i == 0) { 119 furl += url2[i].toString().substring(0, j) + "=" + encodeURIComponent(url2[i].toString().substring(j + 1)); 120 } 121 else { 122 furl += "&" + url2[i].toString().substring(0, j) + "=" + encodeURIComponent(url2[i].toString().substring(j + 1)); 123 } 124 } 125 //encodeURIComponent 126 xmlhttp.open("POST", furl, false); 127 128 xmlhttp.send(); 129 return xmlhttp.responseText; 130 131 }