Вы находитесь на странице: 1из 6

1.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net.Http;
using System.Web.Http;
using ONGC_WEB_SERVICES.Models;
using MySql.Data.MySqlClient;
using System.Data;
using System.Web;
using System.Configuration;

namespace ONGC_WEB_SERVICES.Controllers
{
public class BOREControllers : System.Web.Mvc.Controller
{
//
// GET: /BORE/

[HttpGet]
public List<BORE_WEB> GetBORE_WEB()
{
string Str="Server=localhost;Port=3022;Database=ongc;Uid=;root;Pwd=root";

MySqlConnection conn = new MySqlConnection( Str ); // C#

MySqlCommand cmd = new MySqlCommand();


cmd.Connection = conn ;
cmd.CommandText = "select INSERT_DATE, nvl(INSERT_USER,' '),
nvl(UPDATE_DATE,' '), nvl(UPDATE_USER,' '), nvl(ID,' '), nvl(NAME,' '),
nvl(BOREHOLE_NUMBER,' '), nvl(BOTTOM_LOCATION_ID,' '), nvl(CURRENT_STATUS_DATE,'
'), nvl(CURRENT_STATUS,' ') from BOREHOLE_1 where ID is not null";
MySqlDataReader dr = cmd.ExecuteReader();

List<BORE_WEB> v = new List<BORE_WEB>();


while (dr.Read())
{
BORE_WEB w = new BORE_WEB { };
w.INSERT_DATE = dr.GetString(0);
w.INSERT_USER = dr.GetString(1);
w.UPDATE_DATE = dr.GetString(2);
w.UPDATE_USER = dr.GetString(3);
w.ID = dr.GetString(4);
w.NAME = dr.GetString(5);
w.BOREHOLE_NUMBER = dr.GetString(6);
w.BOTTOM_LOCATION_ID = dr.GetString(7);
w.CURRENT_STATUS_DATE = dr.GetString(8);
w.CURRENT_STATUS = dr.GetString(9);
v.Add(w);
}
dr.Close();
conn.Dispose();
return v;
}

[HttpPost]
public object ONGC_WEB_SERVICES (HttpRequestMessage RequestMsgData)
{
ApiStatus _apistatus = new ApiStatus();
_apistatus.status = false;
_apistatus.statusmessage = "";
_apistatus.id = null;
string readQuery = "";

try
{
//Take out Posted values
var jsonAsyncString =
RequestMsgData.Content.ReadAsStringAsync().Result;

jsonAsyncString = jsonAsyncString.Replace("=", ":\"");

jsonAsyncString = jsonAsyncString.Replace("&", "\",");

jsonAsyncString = "{"+jsonAsyncString+"\"}";

if (!(String.IsNullOrEmpty(jsonAsyncString)))
{

string tableName = "BOREHOLE_1";


string columnNames = " * ";
string whereClause = "";
//string orderByClause = " ORDER BY DATED DESC ";
//string orderByClause = " ORDER BY LOG_ID DESC ";

//_prepareWhereClause Where clause


whereClause = _prepareReadWhereClause(jsonAsyncString,
"AND");

// whereClause = HttpUtility.UrlDecode(whereClause);

//Prepare Read Query


readQuery = "Select " + columnNames + " From " + tableName
+ whereClause;

//Get DB Resultset
List<object> results = new List<object>();

// MySql.Connection oc = new
.DataSourceConnection().getConn("nr");

oc.Open();

results = (List<object>)_FetchDBResults(oc, readQuery);

if (results.Count > 0)
{
_apistatus.id = results;
_apistatus.status = true;
_apistatus.statusmessage = "Fetched Feedbacks";
}
else
{
_apistatus.statusmessage = "No Feedback Found!";
}

} // End if
else
{
_apistatus.statusmessage = "Error msg" + readQuery;
}

return _apistatus;

}
catch (Exception exp)
{
_apistatus.statusmessage = exp.ToString();
return _apistatus;
}

} // End

//Runs Sql Read Query and Returns resultset


public object _FetchDBResults(MySqlConnection conn, string query)
{
List<object> results = new List<object>();

using (conn)
{
MySqlCommand cmd = conn.CreateCommand();
cmd.CommandText = query;
MySqlDataReader reader = cmd.ExecuteReader();

DataTable dtSchema;
Dictionary<string, object> ValuePair = new Dictionary<string,
object>();
string[] tableColName;

dtSchema = reader.GetSchemaTable();

var rowValues = dtSchema.AsEnumerable()


.Select(x => x.Field<string>("COLUMNNAME"))
.ToList();

tableColName = rowValues.ToArray();

if (reader.HasRows)
{
string tempColName = "";
while (reader.Read())
{
ValuePair = new Dictionary<string, object>();
for (int i = 0; i < reader.FieldCount; i++)
{
tempColName = tableColName[i].ToString();
ValuePair[tempColName] = reader[tempColName];
}

results.Add(ValuePair);
}
}
}

return results;

} // End

//Prepares full Where clause string for READ Query by merging


Dictonary KeyValue pairs
public string _prepareReadWhereClause(string _jsonAsyncString, string
_stringJoiner)
{
Dictionary<string, dynamic> myDict = new Dictionary<string,
dynamic>();
myDict = JsonConvert.DeserializeObject<Dictionary<string,
dynamic>>(_jsonAsyncString);

string wherePartial = "";


string whereClause = "";
string matchmode = "LIKE";

if (myDict.ContainsKey("matchmode"))
{
matchmode = myDict["matchmode"];
myDict.Remove("matchmode");
}

if (myDict.Count > 0)
{
foreach (KeyValuePair<string, dynamic> entry in myDict)
{
// do something with entry.Value or entry.Key

if (matchmode == "EXACT")
{
wherePartial = wherePartial + entry.Key + " = '" +
HttpUtility.UrlDecode(entry.Value) + "' " + _stringJoiner + " ";
}

if (matchmode == "LIKE")
{
wherePartial = wherePartial + entry.Key + " Like '%" +
HttpUtility.UrlDecode(entry.Value) + "%' " + _stringJoiner + " ";
}

}
}

if (wherePartial != "")
{
wherePartial = wherePartial.TrimEnd(' ');
wherePartial = wherePartial.Remove(wherePartial.Length -
_stringJoiner.Length);
whereClause = " Where " + wherePartial;
}
return whereClause;

} // End Class

class ApiStatus
{
public bool status;
public string statusmessage;
public object id;
}

} // End Namespace

2.using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ONGC_WEB_SERVICES.Controllers
{
class ApiStatus1
{
public bool status { get; set; }

public string statusmessage { get; set; }


}
}

3.using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ONGC_WEB_SERVICES.Models
{
public class BORE_WEB
{
public string INSERT_DATE { get; set; }
public string INSERT_USER { get; set; }
public string UPDATE_DATE { get; set; }
public string UPDATE_USER { get; set; }
public string ID { get; set; }
public string NAME { get; set; }
public string BOREHOLE_NUMBER { get; set; }
public string BOTTOM_LOCATION_ID { get; set; }
public string CURRENT_STATUS_DATE { get; set; }
public string CURRENT_STATUS { get; set; }

}
}

4.global.asax.ac
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Routing;

namespace Testapi
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
}