<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js'></script> <script type="text/javascript"> function Calistir() { var conString = $('#txtSqlConString').val(); var queryString = $('#txtQueryString').val(); //alert('@Url.Action("SqlCalistir", "Home")') $.ajax({ type: "POST", url: '@Url.Action("SqlCalistir", "Home")', contentType: "application/json; charset=utf-8", data: "{'sqlCommand':'" + queryString + "','sqlConnection':'" + conString + "'}", dataType: "json", success: function (msg) { alert(msg); }, error: function () { alert('Error!'); } }); } </script>
Server Side Kodları
[HttpPost] public JsonResult SqlCalistir(string sqlCommand,string sqlConnection) { string result = string.Empty; DataTable dt = new DataTable(); using (OleDbConnection con = new OleDbConnection(sqlConnection)) { using (OleDbCommand cmd = new OleDbCommand(sqlCommand, con)) { con.Open(); OleDbDataAdapter da = new OleDbDataAdapter(cmd); da.Fill(dt); result =DataTableToJSONWithJSONNet(dt); con.Close(); } } return Json(result, JsonRequestBehavior.AllowGet); }
private string DataTableToJSONWithJSONNet(DataTable table) { string JSONString = string.Empty; JSONString = Newtonsoft.Json.JsonConvert.SerializeObject(table); return JSONString; }