AngularJS SQL


AngularJS သည် Database တစ်ခုမှဒေတာများကိုပြသရန်အကောင်းဆုံးဖြစ်သည်။ ဒေတာကို JSON ဖော်မတ်ဖြင့် သေချာအောင်လုပ်ပါ။


MySQL လုပ်ဆောင်နေသည့် PHP Server မှ ဒေတာရယူခြင်း။

AngularJS ဥပမာ

<div ng-app="myApp" ng-controller="customersCtrl">

<table>
  <tr ng-repeat="x in names">
    <td>{{ x.Name }}</td>
    <td>{{ x.Country }}</td>
  </tr>
</table>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
  $http.get("customers_mysql.php")
  .then(function (response) {$scope.names = response.data.records;});
});
</script>

ASP.NET ဆာဗာတစ်ခုမှ ဒေတာရယူခြင်း SQL လုပ်ဆောင်ခြင်း။

AngularJS ဥပမာ

<div ng-app="myApp" ng-controller="customersCtrl">

<table>
  <tr ng-repeat="x in names">
    <td>{{ x.Name }}</td>
    <td>{{ x.Country }}</td>
  </tr>
</table>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
  $http.get("customers_sql.aspx")
  .then(function (response) {$scope.names = response.data.records;});
});
</script>


Server Code နမူနာများ

အောက်ဖော်ပြပါ ကဏ္ဍသည် SQL ဒေတာရယူရန် အသုံးပြုသည့် ဆာဗာကုဒ်၏ စာရင်းဖြစ်သည်။

  1. PHP နှင့် MySQL ကိုအသုံးပြုခြင်း။ JSON ပြန်လာခြင်း။
  2. PHP နှင့် MS Access ကိုအသုံးပြုခြင်း။ JSON ပြန်လာခြင်း။
  3. ASP.NET၊ VB နှင့် MS Access ကိုအသုံးပြုခြင်း။ JSON ပြန်လာခြင်း။
  4. ASP.NET၊ Razor နှင့် SQL Lite ကိုအသုံးပြုခြင်း။ JSON ပြန်လာခြင်း။

ဆိုက်ပေါင်းစုံ HTTP တောင်းဆိုမှုများ

မတူညီသောဆာဗာတစ်ခု (တောင်းဆိုနေသည့်စာမျက်နှာမှလွဲ၍) ဒေတာတောင်းဆိုမှုအား ဆိုက်ဖြတ်ကျော် HTTP တောင်းဆိုမှုများဟုခေါ်သည်။

ဝဘ်ဆိုက် ဖြတ်ကျော် တောင်းဆိုမှုများသည် ဝဘ်ပေါ်တွင် များသည်။ စာမျက်နှာများစွာသည် မတူညီသောဆာဗာများမှ CSS၊ ပုံများနှင့် scripts များကို တင်ပါသည်။

ခေတ်မီဘရောက်ဆာများတွင်၊ လုံခြုံရေးအကြောင်းပြချက်များကြောင့် scripts များမှ ဆိုက်ပေါင်းစုံ HTTP တောင်းဆိုမှုများ ကို ဆိုက်တစ်ခုတည်း တွင် ကန့်သတ်ထားသည် ။

ကျွန်ုပ်တို့၏ PHP နမူနာများတွင် အောက်ပါစာကြောင်းကို ဆိုက်ဖြတ်ကျော်ဝင်ရောက်ခွင့်ပေးထားပါသည်။

header("Access-Control-Allow-Origin: *");

1. Server Code PHP နှင့် MySQL

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

$conn = new mysqli("myServer", "myUser", "myPassword", "Northwind");

$result = $conn->query("SELECT CompanyName, City, Country FROM Customers");

$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
  if ($outp != "") {$outp .= ",";}
  $outp .= '{"Name":"'  . $rs["CompanyName"] . '",';
  $outp .= '"City":"'   . $rs["City"]        . '",';
  $outp .= '"Country":"'. $rs["Country"]     . '"}';
}
$outp ='{"records":['.$outp.']}';
$conn->close();

echo($outp);
?>

2. Server Code PHP နှင့် MS Access

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=ISO-8859-1");

$conn = new COM("ADODB.Connection");
$conn->open("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=Northwind.mdb");

$rs = $conn->execute("SELECT CompanyName, City, Country FROM Customers");

$outp = "";
while (!$rs->EOF) {
  if ($outp != "") {$outp .= ",";}
  $outp .= '{"Name":"'  . $rs["CompanyName"] . '",';
  $outp .= '"City":"'   . $rs["City"]        . '",';
  $outp .= '"Country":"'. $rs["Country"]     . '"}';
  $rs->MoveNext();
}
$outp ='{"records":['.$outp.']}';

$conn->close();

echo ($outp);
?>

3. Server Code ASP.NET၊ VB နှင့် MS Access

<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<%
Response.AppendHeader("Access-Control-Allow-Origin", "*")
Response.AppendHeader("Content-type", "application/json")
Dim conn As OleDbConnection
Dim objAdapter As OleDbDataAdapter
Dim objTable As DataTable
Dim objRow As DataRow
Dim objDataSet As New DataSet()
Dim outp
Dim c
conn = New OledbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=Northwind.mdb")
objAdapter = New OledbDataAdapter("SELECT CompanyName, City, Country FROM Customers", conn)
objAdapter.Fill(objDataSet, "myTable")
objTable=objDataSet.Tables("myTable")

outp = ""
c = chr(34)
for each x in objTable.Rows
if outp <> "" then outp = outp & ","
outp = outp & "{" & c & "Name"    & c & ":" & c & x("CompanyName") & c & ","
outp = outp &       c & "City"    & c & ":" & c & x("City")        & c & ","
outp = outp &       c & "Country" & c & ":" & c & x("Country")     & c & "}"
next

outp ="{" & c & "records" & c & ":[" & outp & "]}"
response.write(outp)
conn.close
%>

4. ဆာဗာကုဒ် ASP.NET၊ Razor C# နှင့် SQL Lite

@{
Response.AppendHeader("Access-Control-Allow-Origin", "*")
Response.AppendHeader("Content-type", "application/json")
var db = Database.Open("Northwind");
var query = db.Query("SELECT CompanyName, City, Country FROM Customers");
var outp =""
var c = chr(34)
}
@foreach(var row in query){
if (outp != "") {outp = outp + ","}
outp = outp + "{" + c + "Name"    + c + ":" + c + @row.CompanyName + c + ","
outp = outp +       c + "City"    + c + ":" + c + @row.City        + c + ","
outp = outp +       c + "Country" + c + ":" + c + @row.Country     + c + "}"
}
outp ="{" + c + "records" + c + ":[" + outp + "]}"
@outp