C# Converting a var/string to action - c#

My command system works off a database where the entire command is stored.
In the Command creation, the
.SetAction((t, m, c) => { //code });
is what the command actually does.
Pulling from the db, this comes in as a var.
How would I go about converting this to actual code for the Action to actually handle?
Reading through a couple pages from Google leaves me to believe I'll need to use Reflection which I've only touched on with Interfaces.
Snippet of the SetAction
public Command SetAction(Action<string, Message, DiscordClient> action)
{
this.action = action;
return this;
}
The code I am using looks like this:
using (var command = sql.CreateCommand())
{
command.CommandText = "SELECT * FROM commands";
using (var reader = command.ExecuteReader())
{
var actionCol = reader.GetOrdinal("action");
while (reader.Read())
{
var action = reader.GetValue(acionCol);
cm.AddCommand(new Command()
.SetAction((t, m, c,) => { }));
)
}
}

Related

How to test if a record was created in the database?

I am coming from Laravel and new to ASP.net MVC. In Laravel I used to do this to assert if a record was created in database or not:
public function test_a_user_is_created_in_database()
{
// Arrange
// Act
$this->assertDatabaseHas('users', [
'email' => 'sally#example.com'
]);
}
Is there a way to accomplish the same thing in Xunit?
There is probably a more elegant way to accomplish the goal, but this works fine for my purposes:
public static void AssertDatabaseHas(string table, Dictionary<string, object> filters,
bool assertMissing = false) {
using (MySqlCommand cmd = new MySqlCommand()) {
cmd.Connection = GetDbConnection();
// Assemble the WHERE part of the query
// and add parameters to the command.
var filterStr = "1 = 1";
foreach (KeyValuePair<string, object> item in filters) {
if (string.IsNullOrEmpty(item.Value.ToString())) {
filterStr += " AND " + item.Key + " IS NULL";
} else {
filterStr += " AND " + item.Key + " = #" + item.Key;
cmd.Parameters.AddWithValue(item.Key, item.Value);
}
}
// Put the query together.
cmd.CommandText = string.Format("SELECT 1 FROM {0} WHERE {1}", table, filterStr);
// Execute the query and check the result.
using (MySqlDataReader rdr = cmd.ExecuteReader()) {
if (assertMissing) {
Assert.False(rdr.HasRows, "Undesired record exists.");
} else {
Assert.True(rdr.HasRows, "Desired record does not exist.");
}
}
}
}
A reverse of the function is also easily added:
public static void AssertDatabaseMissing(string table, Dictionary<string, object> filters) {
AssertDatabaseHas(table, filters, assertMissing: true);
}
When both are added to a MyCustomAssertions class, they can be called like this:
public void test_a_user_is_created_in_database()
{
MyCustomAssertions.AssertDatabaseHas("users", new Dictionary<string, object> {
{ "email", "sally#example.com" }
});
MyCustomAssertions.AssertDatabaseMissing("users", new Dictionary<string, object> {
{ "email", "sally#example.com" }, { "id", "10" }
});
}
Note:
The code can be easily adapted for MSTest if you happen to be using that; all you need to change is Assert.False to Assert.IsFalse and the same for True.
This example uses MySQL but can probably be modified for any engine. For Npgsql (PostgreSQL) for example, change MySqlCommand to NpgsqlCommand and MySqlDataReader to NpgsqlDataReader.
That is an integration test.
You probably don’t want to do an integration test, typically in .net and unit test, you would use something like FakeItEasy or Moq to provide the correctly typed data, so that the code under test is supplied with the data matching the scenario you want to test in a unit test. If you are testing whether a user is present, you would set it up so that the called to load the data returns the appropriate response, and if you were testing what happens when the user is present you would provide data and calls that return the data appropriate to the user you want to test.
Integration test might be appropriate when integrating a web service, and you don’t quite know for sure what it will return, but if you are using something like dbcontext and entity framework (and you probably should be) then there’s no question about what successfully loading a user should return.

Pooling MySQL Connections with Microsoft Enterprise Library

My setup is MySql.Data.MySqlClient v6.9.8.0 and Microsoft.Practices.EnterpriseLibrary.Data v6.0.0.
The program is a long running program that runs continuously listening for tasks and then performs the job with some form of database action (depending on what the request was.) Sometimes the requests will be one after the other, sometimes there will be several hours between them.
I've tried using Pooling=true in the connection string but it causes me a lot of problems (not all the time - these are intermittent problems.)
Here is an example:
[MySqlException (0x80004005): Authentication to host 'localhost' for user 'root' using method 'mysql_native_password' failed with message: Reading from the stream has failed.]
Turning off pooling fixes the problem but at the same time it makes the queries slower because we can't reuse connections. I've searched online and a lot of people have this same issue and the only fix/workaround I've found is Pooling=false which I'd rather avoid if possible.
Here is an example of my query code:
Database db = this.GetDatabase(databaseName);
List<dynamic> results = new List<dynamic>();
// Run the sql query
using (DbCommand dbCommand = db.GetSqlStringCommand(query))
{
foreach (var parameter in inParameters)
{
db.AddInParameter(dbCommand, parameter.Key, parameter.Value.Item1, parameter.Value.Item2);
}
foreach (var parameter in outParameters)
{
db.AddOutParameter(dbCommand, parameter.Key, parameter.Value.Item1, parameter.Value.Item2);
}
using (IDataReader dataReader = db.ExecuteReader(dbCommand))
{
IDictionary<string, object> instance;
do
{
// Read each row
while (dataReader.Read())
{
instance = new ExpandoObject() as IDictionary<string, object>;
// Populate the object on the fly with the data
for (int i = 0; i < dataReader.FieldCount; i++)
{
instance.Add(dataReader.GetName(i), dataReader[i]);
}
// Add the object to the results list
results.Add(instance);
}
} while (dataReader.NextResult());
}
return results;
}
Any ideas?
Can you try this? I know, I know. using "using" should mean I don't have to call the dataReader.Close() method...but I still do it. I also slightly altered the dr.Read block.
This guy talks about it.
http://www.joseguay.com/uncategorized/ensure-proper-closure-disposal-of-a-datareader
I know, I know. You shouldn't have to. Even when using Ent library, I do an extra .Close step to try and make sure.
Database db = this.GetDatabase(databaseName);
List<dynamic> results = new List<dynamic>();
// Run the sql query
using (DbCommand dbCommand = db.GetSqlStringCommand(query))
{
foreach (var parameter in inParameters)
{
db.AddInParameter(dbCommand, parameter.Key, parameter.Value.Item1, parameter.Value.Item2);
}
foreach (var parameter in outParameters)
{
db.AddOutParameter(dbCommand, parameter.Key, parameter.Value.Item1, parameter.Value.Item2);
}
using (IDataReader dataReader = db.ExecuteReader(dbCommand))
{
IDictionary<string, object> instance;
while (dataReader.Read())
{
instance = new ExpandoObject() as IDictionary<string, object>;
// Populate the object on the fly with the data
for (int i = 0; i < dataReader.FieldCount; i++)
{
instance.Add(dataReader.GetName(i), dataReader[i]);
}
// Add the object to the results list
results.Add(instance);
}
if (dataReader != null)
{
try
{
dataReader.Close();
}
catch { }
}
}
return results;
}

Invalid Statement Subscribe in SqlDependency

I must to abilitate a Notifier with SqlDependency for some changes in a SQL Database. I have searched all online guides but it doesn't work. I have seen also SqlDependency notification - immediate failure notification after executing query and this https://msdn.microsoft.com/en-us/library/ms181122.aspx solution The database is on SQL Server 2012 and is so structured:
This is the table that I want to notify:
This is my actually db options:
I also have setting Service Broker and permission for user.
My code is so structured:
var info = new SABIntegrationEntities1();
using (var notifer = new EntityChangeNotifier<SpRicezioneSpedizioniLight, SABIntegrationEntities1>(p => p.SPEDIZIONE_STATO_GENERAZIONE != "I"))
{
notifer.Error += (sender, e) =>
{
Console.WriteLine("[{0}, {1}, {2}]:\n{3}", e.Reason.Info, e.Reason.Source, e.Reason.Type, e.Sql);
};
notifer.Changed += (sender, e) =>
{
p.SPEDIZIONE_STATO_GENERAZIONE = "G";
};
}
And the function from another project from internet that works fine are:
public EntityChangeNotifier(Expression<Func<TEntity, bool>> query)
{
_context = new TDbContext();
_query = query;
_connectionString = _context.Database.Connection.ConnectionString;
SafeCountDictionary.Increment(_connectionString, x => { SqlDependency.Start(x); });
RegisterNotification();
}
private void RegisterNotification()
{
_context = new TDbContext();
using (SqlConnection connection = new SqlConnection(_connectionString))
{
using (SqlCommand command = GetCommand())
{
command.Connection = connection;
connection.Open();
var sqlDependency = new SqlDependency(command);
sqlDependency.OnChange += new OnChangeEventHandler(_sqlDependency_OnChange);
// NOTE: You have to execute the command, or the notification will never fire.
using (SqlDataReader reader = command.ExecuteReader())
{
}
}
}
}
When the notification start, the project immediately go on notifier.error with this exception:
> 2016-04-11 09:56:33,340 [12108] ERROR TestWINEOWINE.Form1 - [Invalid, Statement, Subscribe]:
SELECT
[Extent1].[AudOperation] AS [AudOperation],
[Extent1].[AudDateLog] AS [AudDateLog],
[Extent1].[PROGRESSIVO] AS [PROGRESSIVO],
[Extent1].[SOCIETA] AS [SOCIETA],
[Extent1].[SPEDIZIONE_STATO_GENERAZIONE] AS [SPEDIZIONE_STATO_GENERAZIONE],
[Extent1].[SPEDIZIONE_TIPO] AS [SPEDIZIONE_TIPO],
[Extent1].[SPEDIZIONE_ANNO] AS [SPEDIZIONE_ANNO],
[Extent1].[SPEDIZIONE_FILIALE] AS [SPEDIZIONE_FILIALE],
[Extent1].[SPEDIZIONE_NUMERO] AS [SPEDIZIONE_NUMERO],
[Extent1].[TERRA_MARE_AEREO] AS [TERRA_MARE_AEREO],
[Extent1].[IMPORT_EXPORT] AS [IMPORT_EXPORT],
[Extent1].[FILIALE] AS [FILIALE],
[Extent1].[TIPO_SERVIZIO] AS [TIPO_SERVIZIO],
[Extent1].[TIPO_TRASPORTO] AS [TIPO_TRASPORTO],
[Extent1].[PRIORITA] AS [PRIORITA],
[Extent1].[SETTORE] AS [SETTORE],
[Extent1].[RIFERIMENTO_COMMITTENTE] AS [RIFERIMENTO_COMMITTENTE],
[Extent1].[RIFERIMENTO_ORDINE] AS [RIFERIMENTO_ORDINE],
[Extent1].[RIFERIMENTO_INTERNO] AS [RIFERIMENTO_INTERNO],
[Extent1].[RIFERIMENTO_INTERNO_EXTRA] AS [RIFERIMENTO_INTERNO_EXTRA],
[Extent1].[RESA] AS [RESA],
[Extent1].[LINEA] AS [LINEA],
[Extent1].[MERCE_CODICE] AS [MERCE_CODICE],
[Extent1].[MERCE_DESCRIZIONE] AS [MERCE_DESCRIZIONE],
[Extent1].[COMMITTENTE] AS [COMMITTENTE],
[Extent1].[TIPO_VEICOLO_RICHIESTO] AS [TIPO_VEICOLO_RICHIESTO],
[Extent1].[ASSICURAZIONE_VALUTA] AS [ASSICURAZIONE_VALUTA],
[Extent1].[ASSICURAZIONE_IMPORTO] AS [ASSICURAZIONE_IMPORTO],
[Extent1].[PESO_LORDO] AS [PESO_LORDO],
[Extent1].[PESO_NETTO] AS [PESO_NETTO],
[Extent1].[VOLUME] AS [VOLUME],
[Extent1].[METRI_LINEARI] AS [METRI_LINEARI],
[Extent1].[IMBALLI] AS [IMBALLI],
[Extent1].[PALETTE] AS [PALETTE],
[Extent1].[ARRIVO_TIPO] AS [ARRIVO_TIPO],
[Extent1].[ARRIVO_DATA_ORA] AS [ARRIVO_DATA_ORA],
[Extent1].[ARRIVO_CORRISPONDENTE] AS [ARRIVO_CORRISPONDENTE],
[Extent1].[ARRIVO_DEPOSITO] AS [ARRIVO_DEPOSITO],
[Extent1].[ARRIVO_UBICAZIONE] AS [ARRIVO_UBICAZIONE],
[Extent1].[CONSEGNA_TIPO] AS [CONSEGNA_TIPO],
[Extent1].[CONSEGNA_DATA_ORA_RICHIESTA_TIPO] AS [CONSEGNA_DATA_ORA_RICHIESTA_TIPO],
[Extent1].[CONSEGNA_DATA_ORA_RICHIESTA_INIZIO] AS [CONSEGNA_DATA_ORA_RICHIESTA_INIZIO],
[Extent1].[CONSEGNA_DATA_ORA_RICHIESTA_FINE] AS [CONSEGNA_DATA_ORA_RICHIESTA_FINE],
[Extent1].[CONSEGNA_PREAVVISO] AS [CONSEGNA_PREAVVISO],
[Extent1].[CONSEGNA_CORRISPONDENTE] AS [CONSEGNA_CORRISPONDENTE],
[Extent1].[CONSEGNA_DEPOSITO] AS [CONSEGNA_DEPOSITO],
[Extent1].[MITTENTE_RAGIONE_SOCIALE] AS [MITTENTE_RAGIONE_SOCIALE],
[Extent1].[MITTENTE_INDIRIZZO] AS [MITTENTE_INDIRIZZO],
[Extent1].[MITTENTE_CAP] AS [MITTENTE_CAP],
[Extent1].[MITTENTE_LOCALITA] AS [MITTENTE_LOCALITA],
[Extent1].[MITTENTE_SITO] AS [MITTENTE_SITO],
[Extent1].[MITTENTE_PROVINCIA] AS [MITTENTE_PROVINCIA],
[Extent1].[MITTENTE_NAZIONE] AS [MITTENTE_NAZIONE],
[Extent1].[MITTENTE_ZONA] AS [MITTENTE_ZONA],
[Extent1].[MITTENTE_RIFERIMENTO] AS [MITTENTE_RIFERIMENTO],
[Extent1].[DESTINATARIO_RAGIONE_SOCIALE] AS [DESTINATARIO_RAGIONE_SOCIALE],
[Extent1].[DESTINATARIO_INDIRIZZO] AS [DESTINATARIO_INDIRIZZO],
[Extent1].[DESTINATARIO_CAP] AS [DESTINATARIO_CAP],
[Extent1].[DESTINATARIO_LOCALITA] AS [DESTINATARIO_LOCALITA],
[Extent1].[DESTINATARIO_SITO] AS [DESTINATARIO_SITO],
[Extent1].[DESTINATARIO_PROVINCIA] AS [DESTINATARIO_PROVINCIA],
[Extent1].[DESTINATARIO_NAZIONE] AS [DESTINATARIO_NAZIONE],
[Extent1].[DESTINATARIO_ZONA] AS [DESTINATARIO_ZONA],
[Extent1].[DESTINATARIO_RIFERIMENTO] AS [DESTINATARIO_RIFERIMENTO],
[Extent1].[PROVENIENZA_RAGIONE_SOCIALE] AS [PROVENIENZA_RAGIONE_SOCIALE],
[Extent1].[PROVENIENZA_INDIRIZZO] AS [PROVENIENZA_INDIRIZZO],
[Extent1].[PROVENIENZA_CAP] AS [PROVENIENZA_CAP],
[Extent1].[PROVENIENZA_LOCALITA] AS [PROVENIENZA_LOCALITA],
[Extent1].[PROVENIENZA_SITO] AS [PROVENIENZA_SITO],
[Extent1].[PROVENIENZA_PROVINCIA] AS [PROVENIENZA_PROVINCIA],
[Extent1].[PROVENIENZA_NAZIONE] AS [PROVENIENZA_NAZIONE],
[Extent1].[PROVENIENZA_ZONA] AS [PROVENIENZA_ZONA],
[Extent1].[DESTINAZIONE_RAGIONE_SOCIALE] AS [DESTINAZIONE_RAGIONE_SOCIALE],
[Extent1].[DESTINAZIONE_INDIRIZZO] AS [DESTINAZIONE_INDIRIZZO],
[Extent1].[DESTINAZIONE_CAP] AS [DESTINAZIONE_CAP],
[Extent1].[DESTINAZIONE_LOCALITA] AS [DESTINAZIONE_LOCALITA],
[Extent1].[DESTINAZIONE_SITO] AS [DESTINAZIONE_SITO],
[Extent1].[DESTINAZIONE_PROVINCIA] AS [DESTINAZIONE_PROVINCIA],
[Extent1].[DESTINAZIONE_NAZIONE] AS [DESTINAZIONE_NAZIONE],
[Extent1].[DESTINAZIONE_ZONA] AS [DESTINAZIONE_ZONA],
[Extent1].[VERIFICA_MERCE] AS [VERIFICA_MERCE],
[Extent1].[DDT_DATA] AS [DDT_DATA],
[Extent1].[DDT_NUMERO] AS [DDT_NUMERO],
[Extent1].[ID_STAMPANTE] AS [ID_STAMPANTE],
[Extent1].[CONSEGNA_TIPO_SERVIZIO] AS [CONSEGNA_TIPO_SERVIZIO],
[Extent1].[ORDINAMENTO] AS [ORDINAMENTO],
[Extent1].[DESTINATARIO_INTERLOCUTORE_TIPO] AS [DESTINATARIO_INTERLOCUTORE_TIPO],
[Extent1].[DESTINATARIO_INTERLOCUTORE_NOMINATIVO] AS [DESTINATARIO_INTERLOCUTORE_NOMINATIVO],
[Extent1].[DESTINATARIO_INTERLOCUTORE_E_MAIL] AS [DESTINATARIO_INTERLOCUTORE_E_MAIL],
[Extent1].[DESTINATARIO_INTERLOCUTORE_CELLULARE] AS [DESTINATARIO_INTERLOCUTORE_CELLULARE]
FROM (SELECT
[SpRicezioneSpedizioniLight].[AudOperation] AS [AudOperation],
[SpRicezioneSpedizioniLight].[AudDateLog] AS [AudDateLog],
[SpRicezioneSpedizioniLight].[PROGRESSIVO] AS [PROGRESSIVO],
[SpRicezioneSpedizioniLight].[SOCIETA] AS [SOCIETA],
[SpRicezioneSpedizioniLight].[SPEDIZIONE_STATO_GENERAZIONE] AS [SPEDIZIONE_STATO_GENERAZIONE],
[SpRicezioneSpedizioniLight].[SPEDIZIONE_TIPO] AS [SPEDIZIONE_TIPO],
[SpRicezioneSpedizioniLight].[SPEDIZIONE_ANNO] AS [SPEDIZIONE_ANNO],
[SpRicezioneSpedizioniLight].[SPEDIZIONE_FILIALE] AS [SPEDIZIONE_FILIALE],
[SpRicezioneSpedizioniLight].[SPEDIZIONE_NUMERO] AS [SPEDIZIONE_NUMERO],
[SpRicezioneSpedizioniLight].[TERRA_MARE_AEREO] AS [TERRA_MARE_AEREO],
[SpRicezioneSpedizioniLight].[IMPORT_EXPORT] AS [IMPORT_EXPORT],
[SpRicezioneSpedizioniLight].[FILIALE] AS [FILIALE],
[SpRicezioneSpedizioniLight].[TIPO_SERVIZIO] AS [TIPO_SERVIZIO],
[SpRicezioneSpedizioniLight].[TIPO_TRASPORTO] AS [TIPO_TRASPORTO],
[SpRicezioneSpedizioniLight].[PRIORITA] AS [PRIORITA],
[SpRicezioneSpedizioniLight].[SETTORE] AS [SETTORE],
[SpRicezioneSpedizioniLight].[RIFERIMENTO_COMMITTENTE] AS [RIFERIMENTO_COMMITTENTE],
[SpRicezioneSpedizioniLight].[RIFERIMENTO_ORDINE] AS [RIFERIMENTO_ORDINE],
[SpRicezioneSpedizioniLight].[RIFERIMENTO_INTERNO] AS [RIFERIMENTO_INTERNO],
[SpRicezioneSpedizioniLight].[RIFERIMENTO_INTERNO_EXTRA] AS [RIFERIMENTO_INTERNO_EXTRA],
[SpRicezioneSpedizioniLight].[RESA] AS [RESA],
[SpRicezioneSpedizioniLight].[LINEA] AS [LINEA],
[SpRicezioneSpedizioniLight].[MERCE_CODICE] AS [MERCE_CODICE],
[SpRicezioneSpedizioniLight].[MERCE_DESCRIZIONE] AS [MERCE_DESCRIZIONE],
[SpRicezioneSpedizioniLight].[COMMITTENTE] AS [COMMITTENTE],
[SpRicezioneSpedizioniLight].[TIPO_VEICOLO_RICHIESTO] AS [TIPO_VEICOLO_RICHIESTO],
[SpRicezioneSpedizioniLight].[ASSICURAZIONE_VALUTA] AS [ASSICURAZIONE_VALUTA],
[SpRicezioneSpedizioniLight].[ASSICURAZIONE_IMPORTO] AS [ASSICURAZIONE_IMPORTO],
[SpRicezioneSpedizioniLight].[PESO_LORDO] AS [PESO_LORDO],
[SpRicezioneSpedizioniLight].[PESO_NETTO] AS [PESO_NETTO],
[SpRicezioneSpedizioniLight].[VOLUME] AS [VOLUME],
[SpRicezioneSpedizioniLight].[METRI_LINEARI] AS [METRI_LINEARI],
[SpRicezioneSpedizioniLight].[IMBALLI] AS [IMBALLI],
[SpRicezioneSpedizioniLight].[PALETTE] AS [PALETTE],
[SpRicezioneSpedizioniLight].[ARRIVO_TIPO] AS [ARRIVO_TIPO],
[SpRicezioneSpedizioniLight].[ARRIVO_DATA_ORA] AS [ARRIVO_DATA_ORA],
[SpRicezioneSpedizioniLight].[ARRIVO_CORRISPONDENTE] AS [ARRIVO_CORRISPONDENTE],
[SpRicezioneSpedizioniLight].[ARRIVO_DEPOSITO] AS [ARRIVO_DEPOSITO],
[SpRicezioneSpedizioniLight].[ARRIVO_UBICAZIONE] AS [ARRIVO_UBICAZIONE],
[SpRicezioneSpedizioniLight].[CONSEGNA_TIPO] AS [CONSEGNA_TIPO],
[SpRicezioneSpedizioniLight].[CONSEGNA_DATA_ORA_RICHIESTA_TIPO] AS [CONSEGNA_DATA_ORA_RICHIESTA_TIPO],
[SpRicezioneSpedizioniLight].[CONSEGNA_DATA_ORA_RICHIESTA_INIZIO] AS [CONSEGNA_DATA_ORA_RICHIESTA_INIZIO],
[SpRicezioneSpedizioniLight].[CONSEGNA_DATA_ORA_RICHIESTA_FINE] AS [CONSEGNA_DATA_ORA_RICHIESTA_FINE],
[SpRicezioneSpedizioniLight].[CONSEGNA_PREAVVISO] AS [CONSEGNA_PREAVVISO],
[SpRicezioneSpedizioniLight].[CONSEGNA_CORRISPONDENTE] AS [CONSEGNA_CORRISPONDENTE],
[SpRicezioneSpedizioniLight].[CONSEGNA_DEPOSITO] AS [CONSEGNA_DEPOSITO],
[SpRicezioneSpedizioniLight].[MITTENTE_RAGIONE_SOCIALE] AS [MITTENTE_RAGIONE_SOCIALE],
[SpRicezioneSpedizioniLight].[MITTENTE_INDIRIZZO] AS [MITTENTE_INDIRIZZO],
[SpRicezioneSpedizioniLight].[MITTENTE_CAP] AS [MITTENTE_CAP],
[SpRicezioneSpedizioniLight].[MITTENTE_LOCALITA] AS [MITTENTE_LOCALITA],
[SpRicezioneSpedizioniLight].[MITTENTE_SITO] AS [MITTENTE_SITO],
[SpRicezioneSpedizioniLight].[MITTENTE_PROVINCIA] AS [MITTENTE_PROVINCIA],
[SpRicezioneSpedizioniLight].[MITTENTE_NAZIONE] AS [MITTENTE_NAZIONE],
[SpRicezioneSpedizioniLight].[MITTENTE_ZONA] AS [MITTENTE_ZONA],
[SpRicezioneSpedizioniLight].[MITTENTE_RIFERIMENTO] AS [MITTENTE_RIFERIMENTO],
[SpRicezioneSpedizioniLight].[DESTINATARIO_RAGIONE_SOCIALE] AS [DESTINATARIO_RAGIONE_SOCIALE],
[SpRicezioneSpedizioniLight].[DESTINATARIO_INDIRIZZO] AS [DESTINATARIO_INDIRIZZO],
[SpRicezioneSpedizioniLight].[DESTINATARIO_CAP] AS [DESTINATARIO_CAP],
[SpRicezioneSpedizioniLight].[DESTINATARIO_LOCALITA] AS [DESTINATARIO_LOCALITA],
[SpRicezioneSpedizioniLight].[DESTINATARIO_SITO] AS [DESTINATARIO_SITO],
[SpRicezioneSpedizioniLight].[DESTINATARIO_PROVINCIA] AS [DESTINATARIO_PROVINCIA],
[SpRicezioneSpedizioniLight].[DESTINATARIO_NAZIONE] AS [DESTINATARIO_NAZIONE],
[SpRicezioneSpedizioniLight].[DESTINATARIO_ZONA] AS [DESTINATARIO_ZONA],
[SpRicezioneSpedizioniLight].[DESTINATARIO_RIFERIMENTO] AS [DESTINATARIO_RIFERIMENTO],
[SpRicezioneSpedizioniLight].[PROVENIENZA_RAGIONE_SOCIALE] AS [PROVENIENZA_RAGIONE_SOCIALE],
[SpRicezioneSpedizioniLight].[PROVENIENZA_INDIRIZZO] AS [PROVENIENZA_INDIRIZZO],
[SpRicezioneSpedizioniLight].[PROVENIENZA_CAP] AS [PROVENIENZA_CAP],
[SpRicezioneSpedizioniLight].[PROVENIENZA_LOCALITA] AS [PROVENIENZA_LOCALITA],
[SpRicezioneSpedizioniLight].[PROVENIENZA_SITO] AS [PROVENIENZA_SITO],
[SpRicezioneSpedizioniLight].[PROVENIENZA_PROVINCIA] AS [PROVENIENZA_PROVINCIA],
[SpRicezioneSpedizioniLight].[PROVENIENZA_NAZIONE] AS [PROVENIENZA_NAZIONE],
[SpRicezioneSpedizioniLight].[PROVENIENZA_ZONA] AS [PROVENIENZA_ZONA],
[SpRicezioneSpedizioniLight].[DESTINAZIONE_RAGIONE_SOCIALE] AS [DESTINAZIONE_RAGIONE_SOCIALE],
[SpRicezioneSpedizioniLight].[DESTINAZIONE_INDIRIZZO] AS [DESTINAZIONE_INDIRIZZO],
[SpRicezioneSpedizioniLight].[DESTINAZIONE_CAP] AS [DESTINAZIONE_CAP],
[SpRicezioneSpedizioniLight].[DESTINAZIONE_LOCALITA] AS [DESTINAZIONE_LOCALITA],
[SpRicezioneSpedizioniLight].[DESTINAZIONE_SITO] AS [DESTINAZIONE_SITO],
[SpRicezioneSpedizioniLight].[DESTINAZIONE_PROVINCIA] AS [DESTINAZIONE_PROVINCIA],
[SpRicezioneSpedizioniLight].[DESTINAZIONE_NAZIONE] AS [DESTINAZIONE_NAZIONE],
[SpRicezioneSpedizioniLight].[DESTINAZIONE_ZONA] AS [DESTINAZIONE_ZONA],
[SpRicezioneSpedizioniLight].[VERIFICA_MERCE] AS [VERIFICA_MERCE],
[SpRicezioneSpedizioniLight].[DDT_DATA] AS [DDT_DATA],
[SpRicezioneSpedizioniLight].[DDT_NUMERO] AS [DDT_NUMERO],
[SpRicezioneSpedizioniLight].[ID_STAMPANTE] AS [ID_STAMPANTE],
[SpRicezioneSpedizioniLight].[CONSEGNA_TIPO_SERVIZIO] AS [CONSEGNA_TIPO_SERVIZIO],
[SpRicezioneSpedizioniLight].[ORDINAMENTO] AS [ORDINAMENTO],
[SpRicezioneSpedizioniLight].[DESTINATARIO_INTERLOCUTORE_TIPO] AS [DESTINATARIO_INTERLOCUTORE_TIPO],
[SpRicezioneSpedizioniLight].[DESTINATARIO_INTERLOCUTORE_NOMINATIVO] AS [DESTINATARIO_INTERLOCUTORE_NOMINATIVO],
[SpRicezioneSpedizioniLight].[DESTINATARIO_INTERLOCUTORE_E_MAIL] AS [DESTINATARIO_INTERLOCUTORE_E_MAIL],
[SpRicezioneSpedizioniLight].[DESTINATARIO_INTERLOCUTORE_CELLULARE] AS [DESTINATARIO_INTERLOCUTORE_CELLULARE]
FROM [aud].[SpRicezioneSpedizioniLight] AS [SpRicezioneSpedizioniLight]) AS [Extent1]
WHERE 'I' = [Extent1].[SPEDIZIONE_STATO_GENERAZIONE]
I don't know what to do.

MonoTouch: Can't create DbParameterCollection?

I'm a vb.net guy trying to figure out MonoTouch c#.
I made this data helper:
public static void ExecuteCommand (SqliteConnection cnn, string command, System.Data.Common.DbParameterCollection parameters)
{
using (var c = cnn.CreateCommand()) {
c.CommandText = command;
c.CommandType = CommandType.Text;
foreach (var p in parameters)
{
c.Parameters.Add (p);
}
c.ExecuteNonQuery ();
}
}
And now I want to call the ExecuteCommand...
var parameters = new System.Data.Common.DbParameterCollection();
parameters.Add("#1", DbType.String).Value = "test";
DataConnection.ExecuteCommand ("INSERT INTO mytest (name) VALUES (#)", parameters);
But MonoTouch says...
var parameters = new System.Data.Common.DbParameterCollection(); <-- "Cannot create an instance of the abstract class or interface 'System.Data.Common.DbParameterCollection'"
parameters.Add("#1", DbType.String).Value = "test"; <-- "A local variable 'parameters' cannot be used before it is declared."
I'm sure the answer is pretty easy, but comming from a VB.Net world, this is not obvious to me.
System.Data.Common.DbParameterCollection is abstract as such you cannot create it. You should be creating a (concrete( collection that inherits from it. In SQLite case it would be Mono.Data.Sqlite.SqliteParameterCollection.
Your second error is likely related to the first, since parameters could not be compiled correctly.

Reading and writing data into sql server simultaneously

I have a service which continuously writes data in a separate thread into SQL database.Now from the same service if i am trying to read from the same table, since i already am writing into it,I get this exception : There is already an open DataReader associated with this Command which must be closed first.
So can anyone help me how to do this simultaneously?
Here s my code for reading data:
public Collection ReadData(string query)
{
{
_result = new Collection<string[]>();
string[] tempResult;
SqlDataReader _readerRead;
using (_command = new SqlCommand(query, _readConnection))
{
_readerRead = _command.ExecuteReader();
while (_readerRead.Read())
{
tempResult = new string[4];
tempResult[0] = _reader[0].ToString();
tempResult[1] = _reader[1].ToString();
tempResult[2] = _reader[2].ToString();
tempResult[3] = _reader[3].ToString();
_result.Add(tempResult);
//Console.WriteLine("Name : {0} Type : {1} Value : {2} timestamp : {3}", _reader[0], _reader[1], _reader[2], _reader[3]);
}
if (_readerRead != null)
{
_readerRead.Close();
}
_readConnection.Close();
return _result;
}
}
}
and here it is for writing to it :
public void WriteData(Collection<TagInfo> tagInfoList)
{
int i = 0;
for (i = 0; i < tagInfoList.Count; i++)
{
using( _command = new SqlCommand(insert statement here)
{
_command.Parameters.AddWithValue("Name", tagInfoList[i].Name);
_command.Parameters.AddWithValue("Type", tagInfoList[i].TagType);
_command.Parameters.AddWithValue("Value", tagInfoList[i].Value);
_reader = _command.ExecuteReader();
if (_reader != null)
{
_reader.Close();
}
}
}
}
You need a different SQLConnection to the database for your writer. You cannot use the same db connection for both.
Although its possible to do, using a separate connection I would question why you need to do this.
If you are reading and writing data to one table in the same service you will be placing unnecessary load on one SQL table, and depending on the number of queries you intend to make this could cause you problems. If you already have this data (in a different thread) why not Marshall the data from the background thread to where you need it as you write it into the database, and you don't need to read the data anymore.
However.... it is difficult to give an fair answer without seeing the code/what you are looking to achieve.

Categories

Resources