Query update without parameters dosent affect rows in oracle database - c#

I have an update query that work in plsqldevelper but when i pass it in query using dapper doesn't work thats the query :
using (var connection = new OracleConnection(_connectionString))
{
var sql = #"UPDATE CS_AO_FIN_DRDAS
SET CODE_ETAT = 3 , DATE_DERNR_MAJ = SYSDATE
WHERE CODE_ETAT = 0";
var rows = await connection.QueryAsync(sql);
}
thank you
i tried both QueryAsync and ExecuteAsync with parameters and without parameters but no changes in database and not show me any errors
PS : when I execute the Api and close the plSqlDevelopper it worked when i late it opened dosent work its little strange.

Related

What is the right way to run parameterized queries for flex tables with odbc driver?

I'm pretty much new to Vertica and its flex tables concept. Need to insert data into the flex table with help of odbcParameter like that
string query = "INSERT INTO flexTable SELECT MapJSONExtractor(#flexDataToInsert) as event"
var connection = GetConnection(); // returns OdbcConnection
var command = GetCommand(connection, queryString.ToString()); // returns OdbcCommand
OdbcParameter param = command.Parameters.Add("#flexDataToInsert", OdbcType.NText, 2048);
param.Value = data; // long json to insert
await command.ExecuteNonQueryAsync()
but get the exception "OdbcException: ERROR [42703] ERROR 2624: Column "flexDataToInsert" does not exist". After struggling for hours still can't realize what's wrong with it

Error from SQL query

Currently I'm working on cleaning up some code on the backend of an application I'm contracted for maintenance to. I ran across a method where a call is being made to the DB via Oracle Data Reader. After examining the SQL, I realized it was not necessary to make the call to open up Oracle Data Reader seeing how the object being loaded up was already within the Context of our Entity Framework. I changed the code to follow use of the Entity Model instead. Below are the changes I made.
Original code
var POCs = new List<TBLPOC>();
Context.Database.Connection.Open();
var cmd = (OracleCommand)Context.Database.Connection.CreateCommand();
OracleDataReader reader;
var SQL = string.Empty;
if (IsAssociate == 0)
SQL = #"SELECT tblPOC.cntPOC,INITCAP(strLastName),INITCAP(strFirstName)
FROM tblPOC,tblParcelToPOC
WHERE tblParcelToPOC.cntPOC = tblPOC.cntPOC AND
tblParcelToPOC.cntAsOf = 0 AND
tblParcelToPOC.cntParcel = " + cntParcel + " ORDER BY INITCAP(strLastName)";
else
SQL = #"SELECT cntPOC,INITCAP(strLastName),INITCAP(strFirstName)
FROM tblPOC
WHERE tblPOC.cntPOC NOT IN ( SELECT cntPOC
FROM tblParcelToPOC
WHERE cntParcel = " + cntParcel + #"
AND cntAsOf = 0 )
AND tblPOC.ysnActive = 1 ORDER BY INITCAP(strLastName)";
cmd.CommandText = SQL;
cmd.CommandType = CommandType.Text;
using (reader = cmd.ExecuteReader())
{
while (reader.Read())
{
POCs.Add(new TBLPOC { CNTPOC = (decimal)reader[0],
STRLASTNAME = reader[1].ToString(),
STRFIRSTNAME = reader[2].ToString() });
}
}
Context.Database.Connection.Close();
return POCs;
Replacement code
var sql = string.Empty;
if (IsAssociate == 0)
sql = string.Format(#"SELECT tblPOC.cntPOC,INITCAP(strLastName),INITCAP(strFirstName)
FROM tblPOC,tblParcelToPOC
WHERE tblParcelToPOC.cntPOC = tblPOC.cntPOC
AND tblParcelToPOC.cntAsOf = 0
AND tblParcelToPOC.cntParcel = {0}
ORDER BY INITCAP(strLastName)",
cntParcel);
else
sql = string.Format(#"SELECT cntPOC,INITCAP(strLastName), INITCAP(strFirstName)
FROM tblPOC
WHERE tblPOC.cntPOC NOT IN (SELECT cntPOC
FROM tblParcelToPOC
WHERE cntParcel = {0}
AND cntAsOf = 0)
AND tblPOC.ysnActive = 1
ORDER BY INITCAP(strLastName)",
cntParcel);
return Context.Database.SqlQuery<TBLPOC>(sql, "0").ToList<TBLPOC>();
The issue I'm having right now is when the replacement code is executed, I get the following error:
The data reader is incompatible with the specified 'TBLPOC'. A member of the type 'CNTPOCORGANIZATION', does not have a corresponding column in the data reader with the same name.
The field cntPOCOrganization does exist within tblPOC, as well as within the TBLPOC Entity. cntPOCOrganization is a nullable decimal (don't ask why decimal, I myself don't get why the previous contractors used decimals versus ints for identifiers...). However, in the past code and the newer code, there is no need to fill that field. I'm confused on why it is errors out on that particular field.
If anyone has any insight, I would truly appreciate it. Thanks.
EDIT: So after thinking on it a bit more and doing some research, I think I know what the issue is. In the Entity Model for TBLPOC, the cntPOCOrganization field is null, however, there is an object tied to this Entity Model called TBLPOCORGANIZATION. I'm pondering if it's trying to fill it. It too has cntPOCOrganization within itself and I'm guessing that maybe it is trying to fill itself and is what is causing the issue.
That maybe possibly why the previous contractor wrote the Oracle Command versus run it through the Entity Framework. I'm going to revert back for time being (on a deadline and really don't want to play too long with it). Thanks!
This error is issued when your EF entity model does not match the query result. If you post your entity model you are trying to fetch this in, the SQL can be fixed. In general you need to use:
sql = string.Format(#"SELECT tblPOC.cntPOC AS <your_EF_model_property_name_here>,INITCAP(strLastName) AS <your_EF_model_property_name_here>,INITCAP(strFirstName) AS <your_EF_model_property_name_here>
FROM tblPOC,tblParcelToPOC
WHERE tblParcelToPOC.cntPOC = tblPOC.cntPOC
AND tblParcelToPOC.cntAsOf = 0
AND tblParcelToPOC.cntParcel = {0}
ORDER BY INITCAP(strLastName)",
cntParcel);

Can Entity Framework Core run non-query calls?

Unfortunately my EF application has to call stored procedures I am unable to change. While this is not ideal I can normally get around it. However, I have a stored proc that does return anything. How does EF core deal with this? I know in previous versions you could run ExecuteNonQuery but I haven't been able to find anything similar in EF Core.
I normally run my queries through a helper as such, where T is a class that maps to a return type EF can serialize to:
context.Set<T>()
.AsNoTracking()
.FromSql(query, args)
.ToListAsync();
However it looks like Set always requires a type as does .Query. Nothing else I've seen off of context would allow you to make a non-queryable call. Am I missing something?
I am using Microsoft.EntityFrameworkCore: 1.2.0
You can use the DbContext.DatabaseExecuteSqlCommand method
using(var context = new SampleContext())
{
var commandText = "INSERT Categories (CategoryName) VALUES (#CategoryName)";
var name = new SqlParameter("#CategoryName", "Test");
context.Database.ExecuteSqlCommand(commandText, name);
}
Or you can revert to ADO.NET calls off the Context:
using (var context = new SampleContext())
using (var command = context.Database.GetDbConnection().CreateCommand())
{
command.CommandText = "DELETE From Table1";
context.Database.OpenConnection();
command.ExecuteNonQuery();
}

Open a connection to DB using EF to execute adhoc query

I'm an asp.net guy hard-core and EF + MVC = ME + WTF.
I have a task to open a connection to each of our production database servers. Run a simple query "Select top 1 * from t1 where f1 > 1".
I thought I could just use system.data.sqlclient build the conn string, open the conn and execute a query.
That doesn't seem to be working. Each connection takes forever.
How would I get an instance of our db object to do this with EF. I've seen tons of dbcontext examples but I don't even know how to get that and what it is.
I need to connect to 20 seperate DB1,TBL1,FLD1 and execute the query above. If they all succeed return an int 200 to an MVC view if even one fails just return a 503.
Thanks!
You can get a reference to the underlying DbConnection in your EF using Database.Connection.
For example:
var dbConn = context.Database.Connection;
var cmd = dbConn.CreateCommand();
cmd.CommandText = "SELECT TOP 1 * from t1 WHERE f1 > 1";
var results = cmd.ExecuteReader();
More on Raw SQL Queries with Entity Framework
From Microsoft doc - Sending Raw Commands To The Database
A- If you do not know the entity
using (var context = new BloggingContext())
{
var blogNames = context.Database.SqlQuery<string>(
"SELECT Name FROM dbo.Blogs").ToList();
}
B- If you know the entity
using (var context = new BloggingContext())
{
var blogs = context.Blogs.SqlQuery("SELECT * FROM dbo.Blogs").ToList();
}

dapper.net multiple delete issue

I'm trying to delete from two tables with two different vairables within a c# class, but I get the following error message:
When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id
Parameter name: splitOn
The sql statement executes fine when capturing command via SQL Profiler, so I'm stumped.
The dapper code is:
public void DeleteListCode(string listCodeId)
{
using (var block = new TransactionBlock())
{
// Get the code first
const string sql = "SELECT ListCode from ListCodes WHERE id =#listCodeId";
var code = TransactionBlock.Connection.Query<string>(sql, new {listCodeId}, TransactionBlock.Transaction)
.FirstOrDefault();
if (string.IsNullOrEmpty(code)) return;
const string sql2 = "delete from Lists WHERE ListCode = #code " +
"delete from ListCodes where Id = #listCodeId";
TransactionBlock.Connection.Query(sql2, new {listCodeId, code}, TransactionBlock.Transaction);
block.Commit();
}
}
I've successfully managed to use a multi select statement, but this is slightly different in the sense that I use two annonomous parameters.
The second operation should use Execute, not Query. It isn't a query, basically. That should be all you need.

Categories