I have this app. Is connected with one database and sending notification to android devices. I want to keep this app always open and check the database for new record. The only idea i had is to put in one infinity loop like while(true) but i have warning in the line with connection.Open(); about memory and the program is stopping.
namespace AndroidParse
{
class Program
{
static void Main(string[] args)
{
//SqlDataReader reader;
SqlConnection conn = new SqlConnection();
string queryString = "SELECT TOP 1 device_id FROM Temp ORDER BY ID_Requests DESC";
string connectionString = "XXXX";
while (true)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
try
{
while (reader.Read())
{
Console.WriteLine(reader[0]);
bool isPushMessageSend = false;
string postString = "";
string urlpath = "https://api.parse.com/1/push";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(urlpath);
postString = "{\"data\": { \"alert\": \"Finally is working\" },\"where\": { \"device_id\": \"" + reader[0] + "\" }}";
httpWebRequest.ContentType = "application/json";
httpWebRequest.ContentLength = postString.Length;
httpWebRequest.Headers.Add("X-Parse-Application-Id", "XXXX");
httpWebRequest.Headers.Add("X-Parse-REST-API-KEY", "XXXX");
httpWebRequest.Method = "POST";
StreamWriter requestWriter = new StreamWriter(httpWebRequest.GetRequestStream());
requestWriter.Write(postString);
requestWriter.Close();
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
JObject jObjRes = JObject.Parse(responseText);
if (Convert.ToString(jObjRes).IndexOf("true") != -1)
{
isPushMessageSend = true;
}
}
//--------------------------------------------------
SqlConnection sqlConnection1 = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
SqlDataReader reader1;
cmd.CommandText = "delete from Temp where ID_Requests in (select top 1 ID_Requests from Temp order by ID_Requests desc)";
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
reader1 = cmd.ExecuteReader();
// Data is accessible through the DataReader object here.
sqlConnection1.Close();
//--------------------------------------------------
Console.ReadLine();
}
}
finally
{
reader.Close();
}
connection.Close();
}
}
}
private static void println()
{
throw new NotImplementedException();
}
}
}
Using a SqlDependency object like Denis Reznik suggested is a great solution.
A few things to keep in mind:
SqlDependency requires the SQL Server Service Broker service to be running on SQL Server (more details here: https://msdn.microsoft.com/en-us/library/ms172133(v=vs.110).aspx)
The queries that can be used in the SqlCommand are essentially continuously executed on the server... Because of this there is a handful of limitations on what the query can do (e.g. no aggregates). More details in the SO answer from Smudge202 here: What are the limitations of SqlDependency
I have found that using SqlDependency to simply notify of a change, and then acting on that by calling data access methods, etc... is easier than attempting to actually use the query to retrieve data. So, in your example, you may want to let the SqlDependency notify there is a change, then create a separate data access method / sp / etc... to retrieve details like the device_id.
Here is a sample that is sort of based on your code above... It will probably require a few tweaks. Good Luck!
namespace AndroidParse
{
public class DbMonitor
{
private readonly string _connectionString = ConfigurationManager.ConnectionStrings["XXXXX"].ConnectionString;
private SqlDependency _dependency;
private SqlConnection _conn;
private SqlCommand _command;
public void MonitorDatabase()
{
SqlDependency.Start(_connectionString);
// Open DB Connection
using (_conn = new SqlConnection(_connectionString))
{
// Setup SQL Command
using (_command = new SqlCommand("SELECT TOP 1 device_id FROM Temp ORDER BY ID_Requests DESC", _conn))
{
// Create a dependency and associate it with the SqlCommand. *** MAGIC ****
_dependency = new SqlDependency(_command);
// Subscribe to the SqlDependency event.
_dependency.OnChange += HandleDatabaseChange;
// Execute
_command.Connection.Open();
_command.ExecuteReader();
}
}
}
public void Stop()
{
SqlDependency.Stop(_connectionString);
}
private void HandleDatabaseChange(object sender, SqlNotificationEventArgs e)
{
if (e.Info == SqlNotificationInfo.Invalid)
{
Console.WriteLine("The above notification query is not valid.");
}
else
{
Console.WriteLine("Database Changed based on query");
Console.WriteLine("------------------------------------");
Console.WriteLine("Event Details:");
Console.WriteLine("Notification Info: " + e.Info);
Console.WriteLine("Notification source: " + e.Source);
Console.WriteLine("Notification type: " + e.Type);
}
//PushMessage logic here
bool isPushMessageSend = false;
string postString = "";
string urlpath = "https://api.parse.com/1/push";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(urlpath);
// Use Query to get device_id?
postString = "{\"data\": { \"alert\": \"Finally is working\" },\"where\": { \"device_id\": \"" + "deviceID" + "\" }}";
httpWebRequest.ContentType = "application/json";
httpWebRequest.ContentLength = postString.Length;
httpWebRequest.Headers.Add("X-Parse-Application-Id", "XXXX");
httpWebRequest.Headers.Add("X-Parse-REST-API-KEY", "XXXX");
httpWebRequest.Method = "POST";
StreamWriter requestWriter = new StreamWriter(httpWebRequest.GetRequestStream());
requestWriter.Write(postString);
requestWriter.Close();
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
JObject jObjRes = JObject.Parse(responseText);
if (Convert.ToString(jObjRes).IndexOf("true") != -1)
{
isPushMessageSend = true;
}
}
// Resume Monitoring... Requires setting up a new connection, etc.. Reuse existing connection? Tried.
MonitorDatabase();
}
}
}
class Program
{
static void Main(string[] args)
{
try
{
// Start the cheese monitor
DbMonitor dbMonitor = new DbMonitor();
dbMonitor.MonitorDatabase();
Console.WriteLine("Monitoring....Press any key to stop.");
Console.Read();
dbMonitor.Stop();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
finally
{
SqlDependency.Stop(ConfigurationManager.ConnectionStrings["XXXXX"].ConnectionString);
}
}
}
Related
I try to change my database connection method with data from API and want to remove connection to database.
This is my database connection and I want to replace data from database with data from API.
public IEnumerable<AlertLevel> DataBaseConnection(int mapCode)
{
string ConnectionString = "server=192.168.1.1;uid=user;port=3333;pwd=password;database=dbName;";
MySqlConnection Conn = new MySqlConnection(ConnectionString);
var listAlert = new List<AlertLevel>();
try
{
Conn.Open();
//replace(2) with mapCode
string query = "CALL Get_Alert_levels_Station(" + mapCode + ");";
MySqlCommand myCommand = new MySqlCommand(query, Conn);
MySqlDataReader myReader;
myReader = myCommand.ExecuteReader();
try
{
while (myReader.Read())
{
var currentData = new AlertLevel()
{
dateForecast = myReader.GetDateTime(0),
levelForecast = myReader.GetInt32(1)
};
listAlert.Add(currentData);
}
}
finally
{
myReader.Close();
Conn.Close();
}
}
catch (Exception ex)
{
Console.WriteLine("Database Connection", "Not Connected ..." + Environment.NewLine + ex.ToString(), "OK");
}
return listAlert;
}
This is my methods with API data:
string GenerateRequestUri(string endpoint)
{
string requestUri = endpoint;
requestUri += $"?id=16";
return requestUri;
}
string GenerateRequestUriStations(string endpoint)
{
string requestUri = endpoint;
requestUri += $"stations";
return requestUri;
}
public WaterBindingData GetData()
{
var reusult = _restServiceData.GetWaterDataForecast(GenerateRequestUriStations(Constants.EndPoint), GenerateRequestUri(Constants.EndPoint));
foreach (var item in reusult.WaterData.Ardaforecast[0].Items)
{
item.DateTimeForecast.ToString();
item.AlertLevelForecast.ToString();
}
return reusult;
}
I want to put inside in DataBaseConnection method my API logic and want to put item.DateTimeForecast.ToString(); and item.AlertLevelForecast.ToString(); in AlertLevel and also I don't know how to put dynamic variable in GenerateRequestUri(string endpoint): requestUri += $"?id=16"; 16 Have to be mapCode
I assume you want to do something like this
string GenerateRequestUri(string endpoint, mapCode)
{
string requestUri = endpoint;
requestUri += $"?id={mapCode}";
return requestUri;
}
public IEnumerable<AlertLevel> GetDataFromAPI(int mapCode)
{
var listAlert = new List<AlertLevel>();
var reusult = _restServiceData.GetWaterDataForecast(GenerateRequestUriStations(Constants.EndPoint), GenerateRequestUri(Constants.EndPoint), mapCode);
foreach (var item in reusult.WaterData.Ardaforecast[0].Items)
{
var currentData = new AlertLevel()
{
dateForecast = item.DateTimeForecast.ToString(),
levelForecast = item.AlertLevelForecast.ToString()
};
listAlert.Add(currentData);
}
return listAlert;
}
I'm writing a discord bot in C# with Discord.NET API.
I was wondering if I could get a few pointers on how to make my bot post a message in a channel every 6 hours.
Details required by stack overflow:
I have the commands set up. I followed a tutorial and found some code to get this far. I know I have to use a timer, but I'm not sure how to hook it into the discord .NET api.
Thank you.
Here's the code I have so far.
Program.cs
https://pastebin.com/rMjY8MAE
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using SQLitePCL;
namespace PrayerBot
{
class Program
{
static void Main(string[] args) => new Program().RunBot().GetAwaiter().GetResult();
// Creating the necessary variables
public static DiscordSocketClient _client;
private CommandService _commands;
private IServiceProvider _services;
private BotConfig config;
// Runbot task
public async Task RunBot()
{
_client = new DiscordSocketClient(); // Define _client
_commands = new CommandService(); // Define _commands
_services = new ServiceCollection() // Define _services
.AddSingleton(_client)
.AddSingleton(_commands)
.BuildServiceProvider();
config = JsonConvert.DeserializeObject<BotConfig>(File.ReadAllText("config.json"));
string botToken = config.token; // Make a string for the token
_client.Log += Log; // Logging
await RegisterCommandsAsync(); // Call registercommands
await _client.LoginAsync(TokenType.Bot, botToken); // Log into the bot user
await _client.StartAsync(); // Start the bot user
await _client.SetGameAsync(config.game); // Set the game the bot is playing
await Task.Delay(-1); // Delay for -1 to keep the console window opened
}
private async Task RegisterCommandsAsync()
{
_client.MessageReceived += HandleCommandAsync; // Messagerecieved
await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), null); // Add module to _commands
}
private Task Log(LogMessage arg) // Logging
{
Console.WriteLine(arg); // Print the log to Console
return Task.CompletedTask; // Return with completedtask
}
private async Task HandleCommandAsync(SocketMessage arg)
{
string messageLower = arg.Content.ToLower(); // Convert the message to a Lower
var message = arg as SocketUserMessage; // Create a variable with the message as SocketUserMessage
if (message is null || message.Author.IsBot) return; // Checks if the message is empty or sent by a bot
int argumentPos = 0; // Sets the argpos to 0 (the start of the message)
if (message.HasStringPrefix(config.prefix, ref argumentPos) || message.HasMentionPrefix(_client.CurrentUser, ref argumentPos)) // If the message has the prefix at the start or starts with someone mentioning the bot
{
var context = new SocketCommandContext(_client, message); // Create a variable called context
var result = await _commands.ExecuteAsync(context, argumentPos, _services); // Create a veriable called result
if (!result.IsSuccess) // If the result is unsuccessful
{
Console.WriteLine(result.ErrorReason); // Print the error to console
await message.Channel.SendMessageAsync(result.ErrorReason); // Print the error to the channel where the error was caused (e.g "Unknown Command")
}
}
}
}
class BotConfig
{
public string token { get; set; }
public string prefix { get; set; }
public string game { get; set; }
}
}
Commands.cs
https://pastebin.com/1EK3M1Fi
using Discord.Commands;
using Microsoft.Data.Sqlite;
using System;
using System.Threading.Tasks;
namespace PrayerBot
{
public class Commands : ModuleBase<SocketCommandContext>
{
[Command("ping")]
private async Task Ping()
{
await ReplyAsync("Pong! 🏓 **" + Program._client.Latency + "ms**");
}
[Command("help")]
private async Task Help()
{
string returnStr = "Worley Byrd Help\n";
returnStr += "~addprayer prayer - this adds a prayer\n";
returnStr += "~viewprayers - view prayers that haven't been answered\n";
returnStr += "~answerprayer id# - mark prayer# as answered\n";
await ReplyAsync(returnStr);
}
void setKeyValue(string name, string value)
{
string keyQuery = $"select value from keys where name='{name}'";
string strValue = "";
using (var connection = new SqliteConnection("Data Source=prayers.s3db"))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText = keyQuery;
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
strValue = reader.GetString(0);
}
}
}
if (String.IsNullOrEmpty(strValue))
{
//insert
string insertQuery = $"insert into keys(name,value) values('{name}','{value}')";
using (var connection = new SqliteConnection("Data Source=prayers.s3db"))
{
connection.Open();
var cmd = connection.CreateCommand();
cmd.CommandText = insertQuery;
cmd.ExecuteNonQuery();
}
}
else
{
//update
string updateQuery = $"update keys set value='{value}' where name='{name}'";
using (var connection = new SqliteConnection("Data Source=prayers.s3db"))
{
connection.Open();
var cmd = connection.CreateCommand();
cmd.CommandText = updateQuery;
cmd.ExecuteNonQuery();
}
}
}
[Command("serverinfo")]
public async Task Serverinfo()
{
await Context.Channel.SendMessageAsync($"This Discord server's name is {Context.Guild}");
}
[Command("posttochannel")]
[Summary("Which channel to post prayers to")]
public Task SetPostToChannel([Remainder][Summary("Set to this channel")] string channel)
{
string serverName = $"{Context.Guild}";
setKeyValue($"posttochannel.{serverName}", channel);
return ReplyAsync("Post Channel set to " + channel);
}
[Command("addprayer")]
[Summary("Add's a prayer to the prayer DB")]
public Task AddPrayerAsync([Remainder][Summary("The prayer to add")] string prayer)
{
string insertPrayer = string.Format("insert into prayers(prayer) values('{0}')", prayer);
using (var connection = new SqliteConnection("Data Source=prayers.s3db"))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText = insertPrayer;
command.ExecuteNonQuery();
}
return ReplyAsync("Prayer added!");
}
[Command("viewprayers")]
[Summary("Add's a prayer to the prayer DB")]
public Task ListPrayersAsync()
{
string selectPrayers = string.Format("select * from prayers where answered=0");
string resultString = "";
using (var connection = new SqliteConnection("Data Source=prayers.s3db"))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText = selectPrayers;
using (var reader = command.ExecuteReader())
{
while(reader.Read())
{
var id = reader.GetString(0);
var prayer = reader.GetString(1);
resultString += String.Format("Prayer {0}: {1}\n", id, prayer);
}
}
}
return ReplyAsync(resultString);
}
[Command("answerprayer")]
[Summary("Add's a prayer to the prayer DB")]
public Task AnswerPrayerAsync([Remainder][Summary("This prayer has been answered")] string id)
{
string updatePrayer = string.Format("update prayers set answered=1 where id=" + id);
using (var connection = new SqliteConnection("Data Source=prayers.s3db"))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText = updatePrayer;
command.ExecuteNonQuery();
}
return ReplyAsync(String.Format("Prayer {0} has been answered\n",id));
}
}
}
You need to use a simple timer in your code:
myTimer = new System.Timers.Timer(6 * 60 * 60 * 1000); //calculate six hours in milliseconds
myTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
myTimer.Start();
private static void OnTimedEvent(object sender, ElapsedEventArgs e)
{
YourMethod();
}
My web service was working fine. When a file is larger than 2MB, it throws System.AggregateException in System.Private.CoreLib.dll.
can you give me some suggestion?
Below is the exception:
Exception thrown: 'System.AggregateException' in System.Private.CoreLib.dll
An unhandled exception of type 'System.AggregateException' occurred in System.Private.CoreLib.dll
One or more errors occurred.
System.AggregateException
HResult=0x80131500
Message=One or more errors occurred. (The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs.)
Source=System.Private.CoreLib
StackTrace:
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at sharepoint.Program.Main(String[] args) in C:\Users\Qihong.Kuang\source\repos\WebService\sharepoint\Program.cs:line 11
Inner Exception 1:
FaultException: The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs.
static void Main(string[] args)
{
Console.WriteLine("Uploading...");
ServiceReference1.ServiceClient ws = new ServiceReference1.ServiceClient();
//something happened in this Async task;
ws.start_processAsync().Wait();
Console.WriteLine("Upload Finished!");
}
public class Service : IService
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
public void start_process()
{
WebService ws = new WebService();
ws.GetCredentials();
}
}
public class WebService : System.Web.Services.WebService
{
OracleConnection con;
List<int> file_ids = new List<int>();
int file_id2;
string queryString;
OracleCommand cmd;
OracleDataReader dtr;
byte[] g_file = new byte[0];
string file_name;
ClientContext ctx;
public WebService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
public void StartProcess()
{
var startTimeSpan = TimeSpan.Zero;
var periodTimeSpan = TimeSpan.FromSeconds(30);
var timer = new System.Threading.Timer((e) =>
{
GetCredentials();
}, null, startTimeSpan, periodTimeSpan);
}
public void GetCredentials()
{
var siteUrl = "siteURL";
var user = "USER";
var password = "PASSWORD";
var pwd = new SecureString();
string docLib = "testtest";
foreach (var c in password) pwd.AppendChar(c);
var SPOCredentials = new SharePointOnlineCredentials(user, pwd);
var SPCredentials = new NetworkCredential(user, pwd);
string subfolderPath = GetSubFolder();
file_ids = GetFileID();
//string uploadLocation = GetFileName();
foreach (var file_id in file_ids)
{
file_id2 = file_id;
ExecuteType("file_name");
string uploadLocation = file_name;
using (ctx = new ClientContext(siteUrl))
{
try
{
ctx.Credentials = SPOCredentials;
ctx.ExecuteQuery();
}
catch (ClientRequestException)
{
ctx.Credentials = SPCredentials;
ctx.ExecuteQuery();
}
catch (NotSupportedException)
{
ctx.Credentials = SPCredentials;
ctx.ExecuteQuery();
Console.WriteLine("SharePoint On-Premise");
}
var library = ctx.Web.Lists.GetByTitle(docLib);
var fileBytes = new byte[] { };
//fileBytes = ReadData();
ExecuteType("blob");
FileStream fileStream;
fileBytes = g_file;
//fileStream = new FileStream(g_file, FileMode.Open);
var fileCreationInformation = new FileCreationInformation();
uploadLocation = string.Format("{0}/{1}", subfolderPath, uploadLocation);
uploadLocation = string.Format("{0}/{1}/{2}", siteUrl, docLib, uploadLocation);
fileCreationInformation.Content = fileBytes;
fileCreationInformation.Overwrite = true;
fileCreationInformation.Url = uploadLocation;
//Upload the file to root folder of the Document library
library.RootFolder.Files.Add(fileCreationInformation);
ctx.ExecuteQuery();
DeleteRecordAfterUploadToSharePoint();
}
}
}
public void ExecuteType(string executeType)
{
con = new OracleConnection(GetConnectionString());
queryString = GetQueryString();
try
{
con.Open();
OracleCommand cmd = new OracleCommand(queryString, con);
dtr = cmd.ExecuteReader();
while (dtr.Read())
{
if (executeType == "file_name")
{
file_name = Convert.ToString(dtr["file_name"]);
}
else if (executeType == "blob")
{
g_file = (byte[])dtr["actual_file"];
}
}
}
catch (Exception ex)
{
string showError = "Error: " + ex.Message;
}
finally
{
dtr.Close();
con.Close();
}
}
public void DeleteRecordAfterUploadToSharePoint()
{
con = new OracleConnection(GetConnectionString());
string queryString = GetDeleteQueryString();
try
{
con.Open();
cmd = new OracleCommand(queryString, con);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
string showError = "Error: " + ex.Message;
}
finally
{
con.Close();
}
}
public List<int> GetFileID()
{
con = new OracleConnection(GetConnectionString());
string queryString = "select count(file_id), file_id from nfirs.sharepoint_file group by file_id";
OracleDataReader dtr = null;
try
{
con.Open();
OracleCommand cmd = new OracleCommand(queryString, con);
dtr = cmd.ExecuteReader();
while (dtr.Read())
{
file_ids.Add(Convert.ToInt32(dtr["file_id"]));
}
}
catch (Exception ex)
{
string showError = "Error: " + ex.Message;
}
finally
{
dtr.Close();
con.Close();
}
Console.WriteLine(file_ids);
return file_ids;
}
public int GetIndividualFileID()
{
return file_id2;
}
public string GetSubFolder()
{
DateTime dt = Convert.ToDateTime(DateTime.Now);
string year = dt.Year.ToString();
return year;
}
public string GetConnectionString()
{
return "Data Source=(DESCRIPTION =(connectionStringhere)";
}
public string GetQueryString()
{
return "select file_id, file_type, actual_file, file_name, file_mimetype, file_update_dttm, file_charset from nfirs.Sharepoint_File where file_id = " + GetIndividualFileID();
}
public string GetDeleteQueryString()
{
string deleteQuery = "delete from (" + GetQueryString() + ")";
return deleteQuery;
}
public string GetFileName()
{
con = new OracleConnection(GetConnectionString());
queryString = GetQueryString();
//OracleDataReader dtr = null;
//string file_name = "";
try
{
con.Open();
OracleCommand cmd = new OracleCommand(queryString, con);
dtr = cmd.ExecuteReader();
while (dtr.Read())
{
file_name = Convert.ToString(dtr["file_name"]);
}
}
catch (Exception ex)
{
string showError = "Error: " + ex.Message;
}
finally
{
dtr.Close();
con.Close();
}
return file_name;
}
public byte[] ReadData()
{
//OracleConnection con = new OracleConnection(GetConnectionString());
//List<int> file_ids = GetFileID();
//foreach(var file_id in file_ids)
//{
//}
string queryString = GetQueryString();
//OracleDataReader dtr = null;
//byte[] g_file = new byte[0];
try
{
con.Open();
cmd = new OracleCommand(queryString, con);
dtr = cmd.ExecuteReader();
while (dtr.Read())
{
g_file = (byte[])dtr["actual_file"];
}
}
catch (Exception ex)
{
string showError = "Error: " + ex.Message;
}
finally
{
dtr.Close();
con.Close();
}
return g_file;
}
}
Instead of using byte[], use memorystream
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;
}
}
}
So this is a little bit code-ceptionlike.
I have a function that is checking the last ID in a table, this function is called within another function. At the end of that function, I have another function that's opening another datareader.
Error:
There is already an open Datareader associated with this connection which must be closed first.
getLastIdfromDB()
public string getLastIdFromDB()
{
int lastIndex;
string lastID ="";
var dbCon = DB_connect.Instance();
if (dbCon.IsConnect())
{
MySqlCommand cmd2 = new MySqlCommand("SELECT ID FROM `competitor`", dbCon.Connection);
try
{
MySqlDataReader reader = cmd2.ExecuteReader();
while (reader.Read())
{
string item = reader2["ID"].ToString();
lastIndex = int.Parse(item);
lastIndex++;
lastID = lastIndex.ToString();
}
}
catch (Exception ex)
{
MessageBox.Show("Error:" + ex.Message);
}
}
return lastID;
}
This function is later-on used in this function:
private void addPlayerBtn_Click(object sender, EventArgs e)
{
ListViewItem lvi = new ListViewItem(getLastIdFromDB());
.........................................^
... HERE
...
... irrelevant code removed
.........................................
var dbCon = DB_connect.Instance();
if (dbCon.IsConnect())
{
MySqlCommand cmd = new MySqlCommand("INSERT INTO `competitor`(`ID`, `Name`, `Age`) VALUES(#idSql,#NameSql,#AgeSql)", dbCon.Connection);
cmd.Parameters.AddWithValue("#idSql", getLastIdFromDB());
cmd.Parameters.AddWithValue("#NameSql", playerName.Text);
cmd.Parameters.AddWithValue("#AgeSql", playerAge.Text);
try
{
cmd.ExecuteNonQuery();
listView1.Items.Clear();
}
catch (Exception ex)
{
MessageBox.Show("Error:" + ex.Message);
dbCon.Connection.Close();
}
finally
{
updateListView();
}
}
}
What would be the best way for me to solve this problem and in the future be sure to close my connections properly?
UPDATE: (per request, included DB_connect)
class DB_connect
{
private DB_connect()
{
}
private string databaseName = "simhopp";
public string DatabaseName
{
get { return databaseName; }
set { databaseName = value; }
}
public string Password { get; set; }
private MySqlConnection connection = null;
public MySqlConnection Connection
{
get { return connection; }
}
private static DB_connect _instance = null;
public static DB_connect Instance()
{
if (_instance == null)
_instance = new DB_connect();
return _instance;
}
public bool IsConnect()
{
bool result = true;
try
{
if (Connection == null)
{
if (String.IsNullOrEmpty(databaseName))
result = false;
string connstring = string.Format("Server=localhost; database={0}; UID=root;", databaseName);
connection = new MySqlConnection(connstring);
connection.Open();
result = true;
}
}
catch (Exception ex)
{
Console.Write("Error: " + ex.Message);
}
return result;
}
public void Close()
{
connection.Close();
}
}
}
You are trying to have multiple open readers on the same connection. This is commonly called "MARS" (multiple active result sets). MySql seems to have no support for it.
You will have to either limit yourself to one open reader at a time, or use more than one connection, so you can have one connection for each reader.
My suggestion would be to throw away that singleton-like thingy and instead use connection pooling and proper using blocks.
As suggested by Pikoh in the comments, using the using clause indeed solved it for me.
Working code-snippet:
getLastIdFromDB
using (MySqlDataReader reader2 = cmd2.ExecuteReader()) {
while (reader2.Read())
{
string item = reader2["ID"].ToString();
lastIndex = int.Parse(item);
lastIndex++;
lastID = lastIndex.ToString();
}
}
Your connection handling here is not good. You need to ditch the DB_connect. No need to maintain a single connection - just open and close the connection each time you need it. Under the covers, ADO.NET will "pool" the connection for you, so that you don't actually have to wait to reconnect.
For any object that implements IDisposable you need to either call .Dispose() on it in a finally block, or wrap it in a using statement. That ensures your resources are properly disposed of. I recommend the using statement, because it helps keep the scope clear.
Your naming conventions should conform to C# standards. Methods that return a boolean should be like IsConnected, not IsConnect. addPlayerBtn_Click should be AddPlayerButton_Click. getLastIdFromDB should be GetlastIdFromDb or getLastIdFromDatabase.
public string GetLastIdFromDatabase()
{
int lastIndex;
string lastID ="";
using (var connection = new MySqlConnection(Configuration.ConnectionString))
using (var command = new MySqlCommand("query", connection))
{
connection.Open();
MySqlDataReader reader = cmd2.ExecuteReader();
while (reader.Read())
{
string item = reader2["ID"].ToString();
lastIndex = int.Parse(item);
lastIndex++;
lastID = lastIndex.ToString();
}
}
return lastID;
}
Note, your query is bad too. I suspect you're using a string data type instead of a number, even though your ID's are number based. You should switch your column to a number data type, then select the max() number. Or use an autoincrementing column or sequence to get the next ID. Reading every single row to determine the next ID and incrementing a counter not good.