SQL to JSON with C#

This seems to work quite well. If there is a more efficient way please let me know.

string SQLtoJSON(string _sqlCon, string _sqlQuery) 
{
 using (SqlConnection con = new SqlConnection(_sqlCon))
 {
 using (SqlCommand cmd = new SqlCommand(_sqlQuery, con))
 {
 using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
 {

 DataTable sqlData = new DataTable();
 adapter.Fill(sqlData); 

System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
 List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>(); 
Dictionary<string, object> row;

 foreach (DataRow dr in sqlData.Rows) 
{ 
row = new Dictionary<string, object>();
 foreach (DataColumn col in sqlData.Columns)
 {
 row.Add(col.ColumnName, dr[col]);
 } 
rows.Add(row); 
} 
return serializer.Serialize(rows); } } } } 

void SendJSON(string json) {
 Response.Clear();
 Response.ContentType = "application/json; charset=utf-8"; 
Response.Write(json); 
Response.End();
 }

 

php mysql database connection

Connecting to mysql via php.

<?php 
$servername = "hostname";
$username = "username";
$password = "password";
$dbname = "database";
$loggedin = false;

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) 
{
//handle it
} 
?>

this is nice to keep in a separate file (inc/conn.php) which can be included into other pages via;

require_once 'inc/conn.php';