SqlBuilder() for C# compare datetime in Where condition - c#

var builder = new SqlBuilder();
var sqlQuery = #"Select * From xxxxTable /**where**/";
var template = builder.AddTemplate(sqlQuery);
builder.Where("xxx1= #xxx1", new { xxx1= xxx1});
builder.Where("convert(date,xxx2) < convert(date,GETDATE())"); // compare with sql server current date is Okay!
builder.Where("convert(date,xxx3) < convert(date,#xxx3)", new {xxx3 = xxx3}); // compare with parameter date is not okay!
using (var connection = Db.GetConnection(xx_ConnectionString_xx))
{
return connection.Query<xxModel>(template.RawSql,template.Parameters).FirstOrDefault();
}
Any idea for SqlBuilder usage for comparing datetime column value.
Thank you ....

Related

Filter string date time in MongoDB returns nothing

I want to get the data from the Azure CosmosDB MongoDB older than yesterday. The property date is string "2022-08-03T13:12:24.455271". The below code returns nothing.
var date = DateTime.Today.AddDays(-1);
var page = 1;
var limit = 10;
var subfilter = new BsonDocument("$expr",
new BsonDocument("$lte",
new BsonArray
{
new BsonDocument("$toDate", "$upload_time"), date
}
)
);
var value = await collection.Find(subfilter).Skip((page - 1) * limit).Limit(limit).ToListAsync();
Using Mongosh I can retrieve the data using this
db.uploads.find({"$expr": {"$lte": [{ "$toDate": "$upload_time" }, ISODate("2022-08-03T00:00:00Z")]}}).skip(0).limit(2)
The count of value above is always zero, means it failed to get the data.
If the upload_time is not string then it will be easily using:
var value = await collection.Find(w => w.upload_time < date).Skip((page - 1) * limit).Limit(limit).ToListAsync();

Query taking longer than expected odbc sage

I am doing a simple select with a date filter on it with a months range where only 32 records are present however its taking 15 seconds to query and return the data I am using sage 50 as you can probally tell and c#. I am using odbc to create the query the seem speeds can be found if i use the odbc query tool.
This is for a stright forward select and it should not be taking that long to return the data through odbc.
String SQL = string.Format("SELECT 'ORDER_NUMBER', 'ORDER_OR_QUOTE',
'ANALYSIS_1','ACCOUNT_REF','ORDER_DATE','NAME',
'COURIER_NUMBER','COURIER_NAME','CUST_TEL_NUMBER'
,'DESPATCH_DATE','ACCOUNT_REF', 'DEL_NAME', 'DEL_ADDRESS_1',
'DEL_ADDRESS_2', 'DEL_ADDRESS_3', 'DEL_ADDRESS_4', 'DEL_ADDRESS_5',
'INVOICE_NUMBER','ORDER_DATE','INVOICE_NUMBER_NUMERIC',
'CONTACT_NAME','CONSIGNMENT', 'NOTES_1', 'ITEMS_NET'
,'ITEMS_GROSS','QUOTE_STATUS' FROM SALES_ORDER WHERE ORDER_DATE
='{0}' and ORDER_DATE <='{1}'", fromD, toD);
public List<SalesOrders> GetSalesOrders()
{
List<SalesOrders> _salesOrdersList = new List<SalesOrders>();
try
{
string sageDsn = ConfigurationManager.AppSettings["SageDSN"];
string sageUsername = ConfigurationManager.AppSettings["SageUsername"];
string sagePassword = ConfigurationManager.AppSettings["SagePassword"];
//int totalRecords = GetSalesOrdersount();
int counter = 0;
//using (var connection = new OdbcConnection("DSN=SageLine50v24;Uid=Manager;Pwd=;"))
using (var connection = new OdbcConnection(String.Format("DSN={0};Uid={1};Pwd={2};", sageDsn, sageUsername, sagePassword)))
{
connection.Open();
//string sql = string.Format(getInvoiceSql, customerCode, DateTime.Today.AddMonths(-1).ToString("yyyy-MM-dd"));
string fromD = dtpFrom.Value.ToString("yyyy-MM-dd");
string toD = dtpTo.Value.ToString("yyyy-MM-dd");
String SQL = string.Format("SELECT 'ORDER_NUMBER', 'ORDER_OR_QUOTE', 'ANALYSIS_1','ACCOUNT_REF','ORDER_DATE','NAME', 'COURIER_NUMBER','COURIER_NAME','CUST_TEL_NUMBER' ,'DESPATCH_DATE','ACCOUNT_REF', 'DEL_NAME', 'DEL_ADDRESS_1', 'DEL_ADDRESS_2', 'DEL_ADDRESS_3', 'DEL_ADDRESS_4', 'DEL_ADDRESS_5', 'INVOICE_NUMBER','ORDER_DATE','INVOICE_NUMBER_NUMERIC', 'CONTACT_NAME','CONSIGNMENT', 'NOTES_1', 'ITEMS_NET' ,'ITEMS_GROSS','QUOTE_STATUS' FROM SALES_ORDER WHERE ORDER_DATE >='{0}' and ORDER_DATE <='{1}'", fromD, toD);
using (var command = new OdbcCommand(SQL, connection))
{
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
counter++;
backgroundWorker1.ReportProgress(counter);
var salesOrders = new SalesOrders();
salesOrders.ACCOUNT_REF = Convert.ToString(reader["ACCOUNT_REF"]);
salesOrders.RecordIdentifier = "";
salesOrders.ShipmmentId = Convert.ToString(reader["ORDER_NUMBER"]);
salesOrders.OrderDate = Convert.ToDateTime(reader["ORDER_DATE"]);
salesOrders.OrderNumber = Convert.ToString(reader["ORDER_NUMBER"]);
salesOrders.Company = "";
salesOrders.Carrier = Convert.ToString(reader["COURIER_NUMBER"]);
salesOrders.CarrierService = Convert.ToString(reader["COURIER_NAME"]);
salesOrders.CustomerName = Convert.ToString(reader["NAME"]);
salesOrders.ShipToAddress1 = Convert.ToString(reader["DEL_ADDRESS_1"]);
salesOrders.ShipToAddress2 = Convert.ToString(reader["DEL_ADDRESS_2"]);
salesOrders.ShipToAddress3 = Convert.ToString(reader["DEL_ADDRESS_3"]);
salesOrders.ShipToAddress4 = Convert.ToString(reader["DEL_ADDRESS_4"]);
salesOrders.ShipToAddress5 = Convert.ToString(reader["DEL_ADDRESS_5"]);
salesOrders.ShiptoAttention = Convert.ToString(reader["DEL_NAME"]);
salesOrders.ShiptoPhoneNo = Convert.ToString(reader["CUST_TEL_NUMBER"]);
salesOrders.Country = Convert.ToString(reader["ANALYSIS_1"]);
salesOrders.ShiptoEmail = "";
salesOrders.MakeAddressDefault = "Y";
bool isProcessed = _sqlManager.hasbeenProcessed(salesOrders.OrderNumber);
if (isProcessed == true)
salesOrders.Exported = true;
_salesOrdersList.Add(salesOrders);
}
}
}
}
return _salesOrdersList.OrderByDescending(o => o.OrderDate).ToList();
}
don't use {0}, {1} for embedding values in strings... ADD via Parameters
String SQL =
#"SELECT
ORDER_NUMBER,
ORDER_OR_QUOTE,
ANALYSIS_1,
ACCOUNT_REF,
ORDER_DATE,
`NAME`,
COURIER_NUMBER,'
OURIER_NAME,
CUST_TEL_NUMBER,
DESPATCH_DATE,
ACCOUNT_REF,
DEL_NAME,
DEL_ADDRESS_1,
DEL_ADDRESS_2,
DEL_ADDRESS_3,
DEL_ADDRESS_4,
DEL_ADDRESS_5,
INVOICE_NUMBER,
ORDER_DATE,
INVOICE_NUMBER_NUMERIC,
CONTACT_NAME,
CONSIGNMENT,
NOTES_1,
ITEMS_NET,
ITEMS_GROSS,
QUOTE_STATUS
FROM
SALES_ORDER
WHERE
ORDER_DATE >= ?
and ORDER_DATE <= ?
ORDER BY
ORDER_DATE DESC";
using (var command = new OdbcCommand(SQL, connection))
{
// assuming fields are actually date data types fields
command.Parameters.Add( "parmFromDate", fromD );
command.Parameters.Add( "parmToDate", toD );
The "?" in the query are place-holders for the parameter values which are handled by the ODBC process. The Parameters being added within the using() portion are added in the same ordinal position as their respective place-holder parts. I just assigned the parameter name to give context to whoever is looking at it after.
The query itself SHOULD be very quick depending on the date range you are pulling. Even added the SQL Order by descending order so it is pre-pulled down in the order you intended it too.

Convert Date to Shamsi in linq query

I want to convert Gregorian date to Shamsi in Linq query
This is my query
Contex = new RfidReaderEntities1();
var selectall = from myreads in Contex.V_DriverWithReadTags select new { myreads.RfidTagId, myreads.CarType, myreads.Plate, myreads.VisiteDate, myreads.Lname,myreads.shamsidate};
dataGridView1.DataSource = selectall.ToList();
I want to convert myreads.VisiteDate to Shamsi date.
First download FarsiLibrary.Utils because it convert Gregorian to Shamsi without any mistake add it's DLL files to your project to more information go Here.
then try this:
var selectall = from myreads in Contex.V_DriverWithReadTags select new {
RfidTagId = myreads.RfidTagId,
CarType = myreads.CarType,
Plate = myreads.Plate,
VisiteDate = PersianDateConverter.ToPersianDate(myreads.VisiteDate),
Lname = myreads.Lname,
shamsidate = myreads.shamsidate
};
Contex = new RfidReaderEntities1();
var selectall = from myreads in Contex.V_DriverWithReadTags select new {
RfidTagId = myreads.RfidTagId,
CarType = myreads.CarType,
Plate = myreads.Plate,
VisiteDate = PersianDateConverter.ToPersianDate((DateTime)myreads.VisiteDate),
// VisiteDate = myshamsifun.GetShamsiDateTime((DateTime)myreads.VisiteDate),
Lname = myreads.Lname,
};
dataGridViewlisttoday.DataSource = selectall.ToList();

ServiceStack OrmLite bind variables appears to be hurting performance

I appears that using bind variables in my queries is hurting performance, some examples by as much as a factor of 5. The following example takes on average about 0.5 seconds to complete.
string strId = "abcd";
using (var db = _conn.OpenDbConnection())
{
var sql = "SELECT count(*) FROM table WHERE idNum= :ID";
var r = db.QueryScalar<int>(sql, new { ID = strID.ToUpper() });
return r >= 1;
}
When I run the following code it takes around 0.1 seconds to complete.
string strId = "abcd";
using (var db = _conn.OpenDbConnection())
{
var sql = string.Format("SELECT count(*) FROM table WHERE idNum= '{0}'", strID.ToUpper());
var r = db.QueryScalar<int>(sql);
return r >= 1;
}
Am I using something incorrectly here that is causing the performance issue?
This is in C# against an Oracle database using version 3.9.71.0 of ServiceStack.OrmLite

Independent subqueries in SQL to Linq statement (hit DB only one time)

Having something similar to:
SELECT (SELECT COUNT(*) from Table1),(SELECT COUNT(*) from Table2 )
How do I write it in linq? Or is it simple not possible?
Limitations:
Can only hit the database one time:
var result = new {
Sum1 = db.Table1.Count(),
Sum2 = db.Table2.Count()
}); // is not valid.....
I do not want to use something similar to (using a "helping" table):
var result = (from t3 in db.Table3
select new {
Sum1 = db.Table1.Count(),
Sum2 = db.Table2.Count()
}).firstOrDefault();
//In order to get only the first row
//but it will not return nothing if the table 3 has no entries......
Not using db.Database.ExecuteSqlCommand
I cannot see a solution which solves all your limitations. This is one of the caveats with using an ORM-mapper, you are not in control of the generated SQL.
In this case, if it is utterly unacceptable for you to send more than one query to the database, the harsh truth is that you will have to write the query yourself.
Update
I got curious and created an extension method that can do this! Of course it constructs its own SQL command, and it just works for Linq2SQL. Also massive disclaimer: It's fairly dirty code, if I have some time I'll fix it up in the weekend :)
public static TOut CountMany<TContext, TOut>(this TContext db, Expression<Func<TContext, TOut>> tableSelector)
where TContext: DataContext
{
var newExpression = (NewExpression) tableSelector.Body;
var tables =
newExpression.Arguments.OfType<MethodCallExpression>()
.SelectMany(mce => mce.Arguments.OfType<MemberExpression>())
.ToList();
var command = new string[tables.Count];
for(var i = 0; i < tables.Count; i++)
{
var table = tables[i];
var tableType = ((PropertyInfo) table.Member).PropertyType.GetGenericArguments()[0];
var tableName = tableType.GetCustomAttribute<TableAttribute>().Name;
command[i] = string.Format("(SELECT COUNT(*) FROM {0}) AS T{1}", tableName, i);
}
var dbCommand = db.Connection.CreateCommand();
dbCommand.CommandText = string.Format("SELECT {0}", String.Join(",", command));
db.Connection.Open();
IDataRecord result;
try
{
result = dbCommand.ExecuteReader().OfType<IDataRecord>().First();
}
finally
{
db.Connection.Close();
}
var results = new object[tables.Count];
for (var i = 0; i < tables.Count; i++)
results[i] = result.GetInt32(i);
var ctor = typeof(TOut).GetConstructor(Enumerable.Repeat(typeof(int), tables.Count).ToArray());
return (TOut) ctor.Invoke(results);
}
the code is called like this:
var counts = dbContext.CountMany(db => new
{
table1Count = db.Table1.Count(),
table2Count = db.Table2.Count()
//etc.
});

Categories

Resources