Returning JSON from dynamic SQL query - c#

In my data access layer when I want to serialize something into JSON and give it to the client I've been doing something like
using (var con = new SqlConnection(connectionString))
{
using (var cmd = new SqlCommand("spGetLengthsOfStay", con))
{
con.Open();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
var los = new LOS();
los.VisitId = (int)rdr["VisitId"];
los.PatientId = (int)rdr["PatientId"];
los.Gender = (string)rdr["Gender"];
los.Age = (int)rdr["Age"];
los.Discharge = (string)rdr["Discharge"];
los.LengthOfStay = (int)rdr["LengthOfStay"];
losList.Add(los);
}
}
}
There are some instances where I need to query the database with a dynamically generated SQL query, so I don't always know the properties to create a single instance of the object, add it to a list, and return the list. What's the preferred method for getting the results from a SQL query back to the client all at one, without using a concrete type using .NET MVC?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Dynamic;
using Newtonsoft.Json;
// microsoft sqlserver
using System.Data.SqlClient;
// oracle
using Oracle.ManagedDataAccess.Client;
namespace InqdWeb
{
public class Dbio
{
//
public static class Consts
{
public const string msgname = "retmsg";
public const string valname = "retval";
public const string jsond = "{ }";
public const string jsonr = "{ \"" + msgname + "\": \"OK\" }";
}
//
//
// »»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»
// core functions
// »»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»
//
//
// with a "sql statement"
// and a connection id,
// prepare the actual sql to get data
// return the result as json
public static string sqljson
(string pi_sql
, string pi_conn
)
{
// empty data
var vd = Consts.jsond;
// success message
var vr = Consts.jsonr;
string msgout = "00";
var ld = new List<dynamic>();
ld = sqlmaster(pi_sql, pi_conn, out msgout);
//
if (msgout.Substring(0, 2) == "00") // not empty and no errors
{
vd = JsonConvert.SerializeObject(ld);
vr = Consts.jsonr.Replace("OK", "00");
}
if (msgout.Substring(0, 2) == "10") // empty and no errors
{
vr = Consts.jsonr.Replace("OK", "10");
}
if (msgout.Substring(1, 1) == "1") // error
{
vd = JsonConvert.SerializeObject(ld);
vr = Consts.jsonr.Replace("OK", msgout);
}
// return json with 2 collections: d with data, r with status and message
var vt = jsonmerge(vd, vr);
return vt;
}
//
//
//
// with a sql
// and a conn id
// return data as dynamic list
public static List<dynamic> sqlmaster
(string pi_sql
, string pi_conn
, out string po_msg
)
{
string sql = " ";
sql = pi_sql;
// result
po_msg = msgout;
// po_msg pos1 empty: 1 has rows: 0
// pos2 error: >0 no error: 0
// pos3... error message
return lista;
}
//
//
// with a sql statement
// and a connection string
// return the result on a dynamic list
// plus a string with
// pos1 error 0-ok 1-error
// pos2 list empty 0-ok 1-list is empty
// pos3... message return code from non-select or error message
public static List<dynamic> sqldo
(string pi_sql
, string pi_connstring
, out string msgout
)
{
// variables
string sql = pi_sql;
var lista = new List<dynamic>();
int retcode;
msgout = "0";
//
string ConnString = pi_connstring;
//
//
//
// Microsoft SqlServer
if (SqlFlavor == "Ms")
{
using (SqlConnection con = new SqlConnection(ConnString))
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
if (sqltype == "R")
{
SqlDataReader reada = cmd.ExecuteReader();
string datatype = "-";
string colname = "-";
while (reada.Read())
{
var obj = new ExpandoObject();
var d = obj as IDictionary<String, object>;
//
for (int index = 0; index < reada.FieldCount; index++)
{
datatype = reada.GetDataTypeName(index);
colname = reada.GetName(index);
bool isnul = reada.IsDBNull(index);
if (!isnul)
{
// add datatypes as needed
switch (datatype)
{
case "int":
d[colname] = reada.GetValue(index);
break;
case "varchar":
d[colname] = reada.GetString(index);
break;
case "nvarchar":
d[colname] = reada.GetString(index);
break;
case "date":
d[colname] = reada.GetDateTime(index);
break;
default:
d[colname] = reada.GetString(index);
break;
}
}
else
{
d[colname] = "";
}
}
lista.Add(obj);
}
reada.Close();
}
}
catch (Exception ex)
{
msgout = "11" + ex.Message.ToString();
}
}
}
//
// Oracle
if (SqlFlavor == "Oa")
{
// Or uses a "
sql = sql.Replace("[", "\"");
sql = sql.Replace("]", "\"");
using (OracleConnection con = new OracleConnection(ConnString))
{
try
{
con.Open();
//
OracleCommand cmd = new OracleCommand(sql, con);
OracleDataReader reada = cmd.ExecuteReader();
string datatype = "-";
string colname = "-";
while (reada.Read())
{
var obj = new ExpandoObject();
var d = obj as IDictionary<String, object>;
// browse every column
for (int index = 0; index < reada.FieldCount; index++)
{
datatype = reada.GetDataTypeName(index);
colname = reada.GetName(index);
bool isnul = reada.IsDBNull(index);
if (!isnul)
{
// add datatypes as needed
switch (datatype)
{
case "Decimal":
d[colname] = reada.GetValue(index);
break;
case "Varchar":
d[colname] = reada.GetString(index);
break;
default:
d[colname] = reada.GetString(index);
break;
}
}
else
{
d[colname] = "";
}
}
lista.Add(obj);
}
reada.Close();
//
}
catch (Exception ex)
{
msgout = "11" + ex.Message.ToString();
}
}
}
//
//
//
return lista;
}
//
//
}
Use it in your controller
string vret = "{'r':{'retval': 'OK' }}";
string sqltxt;
string connt;
connt = ConfigurationManager.ConnectionStrings["<your connection>"].ConnectionString;
sqltxt = "<your select>";
vret = Dbio.sqljson(sqltxt, connt, "MsX"); // MsX for MsSqlServer
return Content(vret, "application/json");

Related

Web api call with param object[] as parametar

Is it possible to call api with params object[] as parameter
[HttpGet("{procName}/{parametarList}")]
public ActionResult<string> Get(string procName , params object[] parametarList)
{
string JSONString = string.Empty;
using (var ado = new ADO())
{
var ds = ado.ExecuteAndReturnDS("execute " + procName " #0, #1, #2,#3,#4,#5", parametarList);
JSONString = JsonConvert.SerializeObject(ds.Tables[0]);
return new JsonResult(JSONString);
}
}
public DataSet ExecuteAndReturnDS(string upit, params object[] parametri)
{
try
{
_Conn();
command = new SqlCommand(upit, _Conn);
command.CommandTimeout = 200;
_ds = new DataSet();
_sqlda = new SqlDataAdapter(command);
for (int i = 0; i <= parametri.Count() - 1; i++)
{
if (parametri[i] == null)
{
command.Parameters.AddWithValue("#" + i, DBNull.Value);
}
else
{
command.Parameters.AddWithValue("#" + i, parametri[i]);
}
}
_sqlda.Fill(_ds);
if (_Conn.State == ConnectionState.Open)
{
_sqlda.Dispose();
_Conn.Dispose();
_Conn.Close();
}
return _ds;
}
catch (Exception ex)
{
return null;
}
}
Like this
/api/values/myProcedure/param1/param2/param3/param4/etc/
I can see 2 ways you can do it.
The first one is to pass in the parameterlist as just a string and then split it into an array of objects in the method.
Here is an example of how you can easily split a string in C#: https://learn.microsoft.com/en-us/dotnet/csharp/how-to/parse-strings-using-split
The second is to use a POST request instead of a GET request, this would allow you to pass in more complicated objects.

SaveChanges() doesn't insert any records to database

I'm using Entity Framework to add new records to the database, everything goes ok without any errors, but I don't see the new record in the database.
Update code works, but insert code doesn't work. No errors appear, but no records are inserted into the database.
My code :
var DBs2 = ConnectionTools.OpenConn();
DBs2.Configuration.AutoDetectChangesEnabled = false;
DBs2.Configuration.ValidateOnSaveEnabled = false;
var resList = CreatedSerials.Where(u => u.Item4 == VID).ToList();
foreach (var r in resList)
{
// if serial id ==0 => new then add it as new
if ( r.Item7 == 0)
{
try
{
var purchasesItemSerials = new purchases_item_seriels()
{
pitem_ID = pitem_ID,
stitems_ID = r.Item1,
pmain_ID = PurchasesID,
pitem_virtualID = r.Item4,
pis_CustomSerial = r.Item2,
pis_ExpireDate = r.Item3,
pis_Statues = 0,
ss_StoreID = Convert.ToInt32(gridView1.GetRowCellValue(ItemImdex, "storeID")),
Purchases_Price = Convert.ToDecimal(gridView1.GetRowCellValue(ItemImdex, "item_NetSmallestUnitPrice")),
};
ss.Add(purchasesItemSerials);
}
catch (Exception eeeeee)
{
Msg.Show("", eeeeee.ToString(), 0);return;
}
}
else
{
var DBs350 = ConnectionTools.OpenConn();
var UpdateSerial = DBs350.purchases_item_seriels.Find(r.Item7);
UpdateSerial.pitem_ID = pitem_ID;
UpdateSerial.stitems_ID = r.Item1;
UpdateSerial. pmain_ID = PurchasesID;
UpdateSerial.pitem_virtualID = r.Item4;
UpdateSerial.pis_CustomSerial = r.Item2;
UpdateSerial.pis_ExpireDate = r.Item3;
UpdateSerial.pis_Statues = r.Item6;
UpdateSerial.ss_StoreID = Convert.ToInt32(gridView1.GetRowCellValue(ItemImdex, "storeID"));
UpdateSerial.Purchases_Price = Convert.ToDecimal(gridView1.GetRowCellValue(ItemImdex, "item_NetSmallestUnitPrice"));
DBs350.SaveChanges();
}
}
try
{
DBs2.purchases_item_seriels.AddRange(ss);
DBs2.SaveChanges();
}
catch (Exception eeeeee)
{
Msg.Show("", eeeeee.ToString(), 0);return;
}
I also tried :
DBs2.Configuration.AutoDetectChangesEnabled = true;
DBs2.Configuration.ValidateOnSaveEnabled = true;
but again: no data is inserted, no errors appear
I also tried :
int returnCode = DBs2.SaveChanges();
and returnCode = 0
**I also tried : inserting just a single item then SaveChanges **
// if this serial is new
var NewSerialresList = CreatedSerials.Where(u => u.Item4 == VID && u.Item7 == 0).ToList();
if (NewSerialresList.Count() > 0)
{
var ss = new List<purchases_item_seriels>();
foreach (var r in NewSerialresList)
{
try
{
mrsalesdbEntities DBs002 = new mrsalesdbEntities();
var purchasesItemSerials = new purchases_item_seriels()
{
pitem_ID = pitem_ID,
stitems_ID = r.Item1,
pmain_ID = PurchasesID,
pitem_virtualID = r.Item4,
pis_CustomSerial = r.Item2,
pis_ExpireDate = r.Item3,
pis_Statues = 0,
ss_StoreID = Convert.ToInt32(gridView1.GetRowCellValue(ItemImdex, "storeID")),
Purchases_Price = Convert.ToDecimal(gridView1.GetRowCellValue(ItemImdex, "item_NetSmallestUnitPrice")),
};
//ss.Add(purchasesItemSerials);
DBs002.purchases_item_seriels.Add(purchasesItemSerials);
DBs002.SaveChanges();
}
catch (Exception ex)
{
Msg.Show("", ex.ToString(), 0);
}
}
int CC = ss.Count();
}
i use this function to change the connection variables at run-time :
public static mrsalesdbEntities OpenConn()
{
mrsalesdbEntities MrSalesContext = new mrsalesdbEntities();
MrSalesContext.ChangeDatabase
(
initialCatalog: myconn.database,
port: Convert.ToUInt32( myconn.port),
userId: myconn.uid,
password: myconn.password,
dataSource: myconn.server
);
return MrSalesContext;
}
public static void ChangeDatabase(
this DbContext source,
string initialCatalog = "",
uint port = 3307,
string dataSource = "",
string userId = "",
string password = "",
bool integratedSecuity = true,
string configConnectionStringName = "mrsalesdbEntities")
/* this would be used if the
* connectionString name varied from
* the base EF class name */
{
try
{
// use the const name if it's not null, otherwise
// using the convention of connection string = EF contextname
// grab the type name and we're done
var configNameEf = string.IsNullOrEmpty(configConnectionStringName)
? source.GetType().Name
: configConnectionStringName;
// add a reference to System.Configuration
var entityCnxStringBuilder = new EntityConnectionStringBuilder
(System.Configuration.ConfigurationManager
.ConnectionStrings[configNameEf].ConnectionString);
// init the sqlbuilder with the full EF connectionstring cargo
var sqlCnxStringBuilder = new MySqlConnectionStringBuilder
(entityCnxStringBuilder.ProviderConnectionString);
// only populate parameters with values if added
if (!string.IsNullOrEmpty(initialCatalog))
sqlCnxStringBuilder.Database = initialCatalog;
if ((port) != 0)
sqlCnxStringBuilder.Port = port;
if (!string.IsNullOrEmpty(dataSource))
sqlCnxStringBuilder.Server = dataSource;
if (!string.IsNullOrEmpty(userId))
sqlCnxStringBuilder.UserID = userId;
if (!string.IsNullOrEmpty(password))
sqlCnxStringBuilder.Password = password;
// set the integrated security status
//sqlCnxStringBuilder.IntegratedSecurity = integratedSecuity;
// now flip the properties that were changed
source.Database.Connection.ConnectionString
= sqlCnxStringBuilder.ConnectionString;
}
catch (Exception ex)
{
// set log item if required
}
}
}

Best practices for writing multiple if else conditions using asp.net c#

I am new to programming. I want to write multiple if else conditions using best practices. Here is my code.
public class HomeController : Controller
{
//Creating object of Result class
Result result = new Result();
// GET: Home
public async Task<ActionResult> Index(string key, string value)
{
DBConnection dbConnection = new DBConnection();
dbConnection.MakeConnection();
try
{
dbConnection.oracleCommand.CommandText = "QUERY";
OracleDataReader Reader = dbConnection.oracleCommand.ExecuteReader();
Reader.Read();
count = Reader.GetInt16(0);
for (int i = 0; i < count; i++)
{
//Updating records
dbConnection.oracleCommand.CommandText = "QUERY";
dbConnection.oracleCommand.ExecuteNonQuery();
dbConnection.oracleCommand.CommandText = "QUERY";
OracleDataReader Reader2 = dbConnection.oracleCommand.ExecuteReader();
while (Reader2.Read())
{
//Getting keyname and keyvalue
keyName = (Reader2.GetValue(1)).ToString();
keyValue = (Reader2.GetValue(2)).ToString();
}
dbConnection.oracleCommand.CommandText = "QUERY";
OracleDataReader Reader3 = dbConnection.oracleCommand.ExecuteReader();
while (Reader3.Read())
{
if ((!Reader3.GetValue(1).Equals(System.DBNull.Value)))
{
//Checking api type
if ((Reader3.GetValue(1).ToString() == ("REST")))
{
dbConnection.oracleCommand.CommandText = "QUERY";
OracleDataReader Reader4 = dbConnection.oracleCommand.ExecuteReader();
while (Reader4.Read())
{
if ((!Reader4.GetValue(0).Equals(System.DBNull.Value)))
{
//Getting url
url = Reader4.GetValue(1).ToString();
//Checking whether a get or a post
if ((Reader4.GetValue(0).ToString() == ("GET")))
{
RestCall restCall = new RestCall();
await restCall.RunAsync(keyName, keyValue);
}
}
else if ((Reader4.GetValue(0).ToString() == ("POST")))
{
//"Do a rest call for post method.";
RestCall restCall = new RestCall();
await restCall.RunAsync(keyName, keyValue);
}
else
{
//"Not a get or a post";
errorMessage = "Error";
}
}
}
}
else
{
//"A null value.";
errorMessageNull = "A null value.";
}
}
//Soap
if ((!Reader3.GetValue(1).Equals(System.DBNull.Value)))
{
if ((Reader3.GetValue(1).ToString() == ("SOAP")))
{
dbConnection.oracleCommand.CommandText = "QUERY";
OracleDataReader Reader4 = dbConnection.oracleCommand.ExecuteReader();
while (Reader4.Read())
{
if ((!Reader4.GetValue(0).Equals(System.DBNull.Value)))
{
//Getting url
url = Reader4.GetValue(1).ToString();
//Read function
if ((Reader4.GetValue(0).ToString() == ("READ")))
{
//SoapCall soapCall = new SoapCall();
//soapCall.DoSoapCall();
}
}
//Create function
else if ((Reader4.GetValue(0).ToString() == ("CREATE")))
{
//"Do a soap call for post method.";
}
else
{
//"Not a get or a post";
errorMessage = "Error";
}
}
}
else
{
//"Not a get or a post";
errorMessage = "Error";
}
}
else
{
//"Not a get or a post";
errorMessage = "Error";
}
Reader.Dispose();
Reader3.Dispose();
dbConnection.CloseConnection();
}
}
catch (Exception e)
{
exception = e.Message.ToString();
}
I want to execute these queries if they meet certain conditions. After that I want to do rest and soap calls. Is there any method to write this code without using multiple if else conditions. I mean a more readable way.
When there is ONLY ONE if statement inside an if statement (not even an else clause), then you can combine those two using && (and). For example,
if ((!Reader3.GetValue(1).Equals(System.DBNull.Value)))
{
//Checking api type
if ((Reader3.GetValue(1).ToString() == ("REST")))
{
could be combined as
if(!Reader3.GetValue(1).Equals(System.DBNull.Value) && Reader3.GetValue(1).ToString() == ("REST"))
And if you're checking different conditions of the same variable, you could use switch
switch(Reader3.GetValue(1).ToString())
{
case "REST":
//Do something.
break; //This is important!
case "SOAP":
//Do something!
break;
case "CREATE":
//I SAY DO SOMETHING!!!
break;
default: //This is the "else clause"
//Are you going to do something or not!?
break;
}
Other then that, multiple if statements are unavoidable!
Side note: Try not to use so many parenthesis!
Best regards! Good luck!

C# - "Connection must be valid and open" with multiple Queries and Methods | Connection not closed?

For many hours now I have lost it over this same error no matter what I change.
I'm trying to make a command line application that will retrieve info from a database based off of two argument inputs, the modname and the optional author. The queries are all handled in the jobs.cs file. The entry-point file calls the method beginJob() which gets the target modname and author (if specified) to query. What's different from the hundred or so other posts I've read here is that the first query issued works completely fine, and I'm sure to close the reader and connection. (Won't close connection every time in production, but I did here for debugging purposes). The next time I call the connection it results in this error.
System.InvalidOperationException: Connection must be valid and open.
at MySql.Data.MySqlClient.ExceptionInterceptor.Throw(Exception exception)
at MySql.Data.MySqlClient.MySqlConnection.Throw(Exception ex)
at MySql.Data.MySqlClient.MySqlCommand.CheckState()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
at vmcmod.Job.queryAuthor(String authorUID) in F:\Projects\VisualStudio\VMC-MOD\vmcmod\job.cs:line 235
at vmcmod.Job.StartJob() in F:\Projects\VisualStudio\VMC-MOD\vmcmod\job.cs:line 47
at vmcmod.Job.queryModAndAuthor(String modname, String author) in F:\Projects\VisualStudio\VMC-MOD\vmcmod\job.cs:line 205
at vmcmod.Job.beginJob() in F:\Projects\VisualStudio\VMC-MOD\vmcmod\job.cs:line 27
at vmcmod.VMCMOD.parseArgs() in F:\Projects\VisualStudio\VMC-MOD\vmcmod\vmcmod.cs:line 58
Some posts I've looked at but don't seem to help:
C# Mysql Connection must be valid and open
Connection must be valid and open error
C# Query: 'System.InvalidOperationException' | Additional information: Connection must be valid and open
C# Source Files (Please try and not cringe too hard, this is my first C# application):
jobs.cs [updated]
using MySql.Data.MySqlClient;
using System;
namespace vmcmod
{
class Job
{
public bool checkInput()
{
if (Global.currentJobTargetModname == "undefined")
{
Utils.consoleLog("The mod name is required.", 3);
return false;
}
else
{
return true;
}
}
public void beginJob()
{
string targetModname = Global.currentJobTargetModname.ToLower();
string targetModAuthor = Global.currentJobTargetAuthor.ToLower();
if (targetModAuthor != "undefined")
{
queryModAndAuthor(targetModname, targetModAuthor);
Utils.consoleLog("Job Call. (Author defined)", 4);
}
else
{
queryMod(targetModname);
Utils.consoleLog("Job Call. (Author undefined)", 4);
}
}
private void StartJob()
{
string author = null;
string targetModAuthor = Global.currentJobTargetAuthor.ToLower();
Utils.consoleLog("Mod exists.", 5);
if (targetModAuthor == "undefined")
{
author = "(None; First in row)";
}
else
{
queryAuthor(Global.queryResModAuthorID);
author = "'" + Global.queryResModAuthor + "' (UUID:" + Global.queryResModAuthorID + ") ";
}
var collaborators_obj = Newtonsoft.Json.Linq.JObject.Parse(Global.queryResModCollab);
var tags_obj = Newtonsoft.Json.Linq.JObject.Parse(Global.queryResModTags);
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("MOD INSTALL SPECIFICATIONS:");
Console.BackgroundColor = ConsoleColor.Black;
Console.WriteLine(" Mod Name: '" + Global.queryResModName + "'");
Console.Write(" Mod Author: " + author);
if (Global.queryResModAuthorVerified == true && targetModAuthor != "undefined")
{
Utils.consoleTag("*VERIFIED*", ConsoleColor.Green);
}
if (Global.queryResModAuthorVerified == false && targetModAuthor != "undefined")
{
Utils.consoleTag("*UNVERIFIED*", ConsoleColor.Red);
}
Console.Write("\n");
Console.WriteLine(" Mod Version: " + Global.queryResModVersion);
Console.WriteLine(" Installations: " + Global.queryResModInstalls);
Console.WriteLine(" Description:\n " + Global.queryResDescription);
Console.WriteLine(" Mod UUID: " + Global.queryResModUid);
if (Global.advInfo == true)
{
Console.WriteLine(" Rep. Entry #" + Global.queryResModID);
Console.WriteLine(" Mod Created On: " + Utils.UnixTimeStampToDateTime(Convert.ToDouble(Global.queryResDateCreated)));
Console.WriteLine(" Mod Last Modified On: " + Utils.UnixTimeStampToDateTime(Convert.ToDouble(Global.queryResDateLastModf)));
Console.WriteLine(" Tags: \n " + Convert.ToString(tags_obj["tags"]));
Console.WriteLine(" Collaborators: \n " + Convert.ToString(collaborators_obj["collaborators"]));
}
Utils.consoleSeparator();
bool user_response = Utils.consoleAskYN("Are you sure you want to install this mod?");
if (user_response == true)
{
queryGetModJSON(Global.queryResModUid);
Utils.consoleLog("JSON Data: "+Global.queryResModJSON,5);
//FileIO fio = new FileIO();
//fio.jsonToFiles(fio.jsonToArray(Global.queryResModJSON));
}
else
{
Utils.consoleSetKeyExit();
}
}
private void queryGetModJSON(string uuid)
{
var dbCon = new DBConnection();
if (dbCon.IsConnect() == false)
{
Utils.consoleLog("Unable to query repository or no results.", 3);
Utils.consoleSetKeyExit();
}
else
{
string q = "SELECT data_json FROM vmcmod_repository_data WHERE uid='" + uuid + "' LIMIT 1;";
MySqlCommand cmd = new MySqlCommand(q, dbCon.Connection);
var reader = cmd.ExecuteReader();
if (!reader.HasRows)
{
Utils.consoleLog("Mod data not found.", 3);
reader.Close();
dbCon.Close();
}
else
{
while (reader.Read())
{
Global.queryResModJSON = reader.GetString(0);
}
reader.Close();
dbCon.Close();
}
}
}
private void queryMod(string modname)
{
var dbCon = new DBConnection();
if (dbCon.IsConnect() == false)
{
Utils.consoleLog("Unable to query repository or no results.", 3);
Utils.consoleSetKeyExit();
}
else
{
string q = "SELECT id,uid,author_id,mod_name,status,dependencies_json,description,mod_version,date_created,date_last_modified,collaborators,tags_json,installs FROM vmcmod_repository_info WHERE mod_name LIKE'" + modname + "' LIMIT 1;";
MySqlCommand cmd = new MySqlCommand(q, dbCon.Connection);
var reader = cmd.ExecuteReader();
if (!reader.HasRows)
{
Utils.consoleLog("Mod not found.", 3);
reader.Close();
dbCon.Close();
}
else
{
while (reader.Read())
{
Global.queryResModInstalls = reader.GetInt32(12);
Global.queryResModTags = reader.GetString(11);
Global.queryResModCollab = reader.GetString(10);
Global.queryResDateLastModf = reader.GetInt32(9);
Global.queryResDateCreated = reader.GetInt32(8);
Global.queryResModVersion = reader.GetFloat(7);
Global.queryResDescription = reader.GetString(6);
Global.queryResModDependencies = reader.GetString(5);
Global.queryResModStatus = reader.GetInt16(4);
Global.queryResModName = reader.GetString(3);
Global.queryResModAuthorID = reader.GetString(2);
Global.queryResModUid = reader.GetString(1);
Global.queryResModID = Convert.ToInt32(reader.GetInt32(0));
}
reader.Close();
dbCon.Close();
StartJob();
}
}
}
private void queryModAndAuthor(string modname, string author)
{
var dbCon = new DBConnection();
if (dbCon.IsConnect() == false)
{
Utils.consoleLog("Unable to query repository or no results.", 3);
Utils.consoleSetKeyExit();
}
else
{
string q = "SELECT id,uid,author_id,mod_name,status,dependencies_json,description,mod_version,date_created,date_last_modified,collaborators,tags_json,installs FROM vmcmod_repository_info WHERE mod_name LIKE'" + modname + "' LIMIT 1;";
MySqlCommand cmd = new MySqlCommand(q, dbCon.Connection);
var reader = cmd.ExecuteReader();
if (!reader.HasRows)
{
Utils.consoleLog("Mod not found.", 3);
reader.Close();
dbCon.Close();
}
else
{
while (reader.Read())
{
Global.queryResModInstalls = reader.GetInt32(12);
Global.queryResModTags = reader.GetString(11);
Global.queryResModCollab = reader.GetString(10);
Global.queryResDateLastModf = reader.GetInt32(9);
Global.queryResDateCreated = reader.GetInt32(8);
Global.queryResModVersion = reader.GetFloat(7);
Global.queryResDescription = reader.GetString(6);
Global.queryResModDependencies = reader.GetString(5);
Global.queryResModStatus = reader.GetInt16(4);
Global.queryResModName = reader.GetString(3);
Global.queryResModAuthorID = reader.GetString(2);
Global.queryResModUid = reader.GetString(1);
Global.queryResModID = Convert.ToInt32(reader.GetInt32(0));
}
reader.Close();
dbCon.Close();
StartJob();
}
}
}
private bool queryCheckAuthorExists(string author)
{
var dbCon = new DBConnection();
string q = "SELECT * FROM vmcmod_users WHERE username='"+author+ "' AND is_author=true LIMIT 1;";
MySqlCommand cmd = new MySqlCommand(q, dbCon.Connection);
var reader = cmd.ExecuteReader();
if (reader.HasRows == false)
{
Utils.consoleLog("Author not found.", 3);
reader.Close();
dbCon.Close();
return false;
}
else
{
Utils.consoleLog("Author found.", 4);
reader.Close();
dbCon.Close();
return true;
}
}
private void queryAuthor(string authorUID)
{
var dbCon = new DBConnection();
string q = "SELECT username,is_verified_user FROM vmcmod_users WHERE uid='" + authorUID + "' AND is_author=true LIMIT 1;";
MySqlCommand cmd = new MySqlCommand(q, dbCon.Connection);
var reader = cmd.ExecuteReader();
if (reader.HasRows == false)
{
Utils.consoleLog("Author not found.", 3);
reader.Close();
dbCon.Close();
}
else
{
while (reader.Read())
{
Global.queryResModAuthor = reader.GetString(0);
Global.queryResModAuthorVerified = reader.GetBoolean(1);
}
Utils.consoleLog("Author found.", 4);
reader.Close();
dbCon.Close();
}
}
}
}
dbinterface.cs (this wasn't written by me, rather taken and edited from a post on stackoverflow, here) [updated]
using MySql.Data.MySqlClient;
using System;
namespace vmcmod
{
public class DBConnection
{
public DBConnection()
{
}
public string Password { get; set; }
private MySqlConnection connection = null;
public MySqlConnection Connection
{
get { return connection; }
}
public bool IsConnect()
{
bool result = true;
if (Connection == null)
{
string connString = "Server=...; Port=...; Database=...; Uid=...; Pwd=...;";
connection = new MySqlConnection(connString);
try
{
connection.Open();
Utils.consoleLog("Connected to repository.", 4);
result = true;
}
catch (Exception e)
{
Utils.consoleLog("Error occured while connecting to repository.", 3);
Utils.consoleLog("MySQL Exception: "+e,5);
result = false;
}
}
return result;
}
public void Close()
{
connection.Close();
connection = null;
}
}
}
global.cs (global variable storage)
using System;
namespace vmcmod
{
class Global
{
internal const float version = 0.1F;
internal static string currentJobWorld = "New World";
internal static string currentJobTargetModname = "undefined";
internal static string currentJobTargetAuthor = "undefined";
internal static string currentJobProfile = "default";
internal static int currentJobAccountPIN;
internal static bool verbose = false;
internal static bool debug = false;
internal static bool advInfo = false;
internal static bool queryResModAuthorVerified = false;
internal static string queryResModUid;
internal static string queryResModAuthorID;
internal static string queryResModAuthor;
internal static string queryResModName;
internal static string queryResModDependencies = "{\"dependencies\":[]}";
internal static int queryResModStatus;
internal static float queryResModVersion;
internal static string queryResDescription = "None provided.";
internal static int queryResDateCreated;
internal static int queryResDateLastModf;
internal static int queryResModID;
internal static string queryResModCollab = "{\"collaborators\":[]}";
internal static string queryResModTags = "{\"tags\":[]}";
internal static int queryResModInstalls;
internal static string queryResModJSON = "{}";
}
}
The quick and dirty solution would be to change:
public void Close()
{
connection.Close();
}
to:
public void Close()
{
connection.Close();
connection = null;
}
Also, remove this code:
private static DBConnection _instance = null;
public static DBConnection Instance()
{
if (_instance == null)
_instance = new DBConnection();
return _instance;
}
and rather than use DBConnection.Instance, just use new DBConnection();
And change:
public MySqlConnection Connection
{
get { return connection; }
}
to:
public MySqlConnection Connection
{
get {
IsConnect();
return connection; }
}
and change:
public bool IsConnect()
{
bool result = true;
if (Connection == null)
to:
public bool IsConnect()
{
bool result = true;
if (connection == null)
I see you are using singletne pattern for DBConnection this is not a good idea.
private static DBConnection _instance = null;
public static DBConnection Instance()
{
if (_instance == null)
_instance = new DBConnection();
return _instance;
}
You might want to check this answer out for more information: https://stackoverflow.com/a/814606/8143718
As per #mjwills' response, the DB interface script I'd swiped was quite funky and only allowed one instance at a time.
Working DB Interface
using MySql.Data.MySqlClient;
using System;
namespace vmcmod
{
public class DBConnection
{
public DBConnection()
{
}
public string Password { get; set; }
private MySqlConnection connection = null;
public MySqlConnection Connection
{
get
{
IsConnect();
return connection;
}
}
public bool IsConnect()
{
bool result = true;
if (connection == null)
{
string connString = "Server=...; Port=...; Database=...; Uid=...; Pwd=...;";
connection = new MySqlConnection(connString);
try
{
connection.Open();
Utils.consoleLog("Connected to repository.", 4);
result = true;
}
catch (Exception e)
{
Utils.consoleLog("Error occured while connecting to repository.", 3);
Utils.consoleLog("MySQL Exception: "+e,5);
result = false;
}
}
return result;
}
public void Close()
{
connection.Close();
connection = null;
}
}
}

Callback Requested Query Abort on Transaction

I am using a recursive function that has a transaction in it, however the transaction causes a "callback requested query abort
abort due to ROLLBACK" error. Here is my code:
public Dictionary<int, string> GetFolders(string connectstring)
{
var connection = new SQLiteConnection(connectstring);
connection.Open();
Dictionary<int, string> folderdictionary = new Dictionary<int, string>();
using (var transaction = connection.BeginTransaction())
{
using (SQLiteCommand command = connection.CreateCommand())
{
command.CommandText = "select * from moz_bookmarks AS bookmarks WHERE type=2";
SQLiteDataReader commandread = command.ExecuteReader();
int current_id = 0;
List<string> currentfolders = new List<string>();
while (commandread.Read()) // <--------------- thrown here
{
var title = commandread.GetString(5);
current_id = commandread.GetInt32(0);
var parent = commandread.GetInt32(3);
List<string> folders = new List<string>();
folder = GetChild(connection, (string)commandread["title"], commandread.GetInt32(3), ref folders, true);
currentfolders.Add(folder);
folderdictionary.Add(current_id, folder);
folder = "";
}
}
transaction.Commit();
}
connection.Close();
return folderdictionary;
}
The recursion itself is in the GetChild function:
private static string GetChild(SQLiteConnection sqlite_connection2, string title, int id, ref List<string> folder, bool recursive, SQLiteTransaction transaction)
{
if (recursive)
folder.Add(title);
using (SQLiteCommand folder_get = sqlite_connection2.CreateCommand())
{
folder_get.Connection = sqlite_connection2;
folder_get.CommandText = "SELECT * FROM moz_bookmarks WHERE id = " + id;
SQLiteDataReader get_folders = folder_get.ExecuteReader();
while (get_folders.Read())
{
if (get_folders.GetInt32(1) == 1)
{
break;
}
else if (get_folders.GetInt32(1) == 2 && get_folders.GetInt32(0) != 1)
{
folder.Add(get_folders["title"].ToString());
if (get_folders.GetInt32(3) != 0 && get_folders.GetInt32(0) != 1)
{
GetChild(sqlite_connection2, (string)get_folders["title"], get_folders.GetInt32(3), ref folder, false);
}
break;
}
}
if (recursive)
folder.Reverse();
get_folders.Close();
}
return string.Join("\\", folder);
}
Without the SQLiteTransaction, the GetChild is a huge memory leak and eventually throws a OutOfMemoryException.

Categories

Resources