I need to pass parameters to SQL statement, with Firebird ADO everything works well, but with InterBase ADO there is a problem.
My code:
result = conn.Query<DestClass>(sqlCmd, new
{
stringParam = stringVal,
intParam1 = intVal1,
intParam2 = intVal2
}).Single();
With that I've got FormatException, but when I define parameters with DynamicParameters and setting DbType.AnsiString for stringParam, SQL works well. But I've got many places in my code when I need to pass string and I don't want to change this in all places.
Then I found that I can use Dapper.SqlMapper.AddTypeMap(typeof(String), DbType.AnsiString);
but I can't. I've got Common Language Runtime detected an invalid program.
How to resolve this issue?
EDIT
It looks like problem is solved in Dapper v1.22.
There is a proposal to add an assembly-level attribute of the form:
[assembly:SomeName(blah)]
that would control the default string type for all types coming from that assembly. This would probably achieve most of what you need (although it would push the swing the other way, so you need to tell the other uses what to do). I am currently very actively hacking dapper, so I would expect this to materialize in the short term.
Note that you do not need to use DynamicParameters; you can also use:
stringParam = new DbString { Value = stringVal, IsAnsi = true }
Related
I've created a SQL query that I execute with the following command, and it returns the correct number of entries but these contains all 0:
If I run the same SQL command in my Management Studio, it works correctly.
I also tried it with a Linq statement and it works also correctly:
I hope you guys can help me to solve the problem.
You shouldn't be projecting into a List<T> - the ToList() does that. Basically, simplify:
var data = dbContext.Database.SqlQuery<Tuple<DateTime, string, string>>(...).ToList();
It might also work with value-tuples:
var data = dbContext.Database.SqlQuery<(DateTime, string, string)>(...).ToList();
which would also allow you to conceptually name them:
var data = dbContext.Database.SqlQuery<(DateTime Datum, string Text, string Bemerkung)>
(...).ToList();
Note: concatenating filter is almost certainly a SQL injection vulnerability; if looks like you should be using a SQL parameter there instead.
#Evk notes that EF might not support column-wise binding of tuples. If that is the case, then your best bet would be to create a POCO that matches the column definitions:
class Foo // rename me to something meaningful
{
// note: there may be custom attributes you can use
// to make these names less ugly, i.e.
// [Column("TEXT")] on a property called Text
public DateTime RMA_DATUM {get;set;}
public string TEXT {get;set;}
public string BEMERKUNG {get;set;}
}
and use SqlQuery<Foo>.
Context
I am using crmsvcutil to generate early bind entities. I am also utilizing entity name string constants MyEntity.EntityLogicalName for example in statements like the following (using alternate key):
var reference = new EntityReference(MyEntity.EntityLogicalName, "my_attribute_name", myValue)
Question
I would like to eliminate the "my_attribute_name" string literal in the statement. How can I do this?
Unfortunatelly I can not find it in the generated C# model.
Missed I something? If not, maybe there is an extension to crmsvcutil (similarly to the optionset generator sameple?
To answer the question, you can use the C# 6 nameof() expression to get the name of the property. If you convert the name to lower case, you end up with the logical name:
var logicalName = nameof(MyEntity.MyAttributeValue).ToLower();
With that being said, I rarely find myself having to do something like this. Often you can use Entity.ToEntityReference() for a more strongly typed approach.
Additionally the constructor you use for EntityReference is only meant to be used for alternate keys (otherwise one would just use the constructor that takes a string and a Guid)
I noticed that database queries executed by DevForce encloses database objects in the query with quotes (e.g. select * from "SCHEMA_NAME"."TABLE_NAME"). I'd just like to ask if there is a way for DevForce not to do this.
I'm currently using DevForce Classic 3.7.5.1.
Thanks.
DevForce Classic uses a "provider helper" to determine formatting for a SQL statement. The default helper in use is determined by your connection string, but is usually an instance of OleDbProviderHelper. To change default logic you can subclass either the OleDbProviderHelper, or another of the helpers based on your needs.
In this case, to change formatting for identifiers you can use something similar to the following:
public class CustomProviderHelper : IdeaBlade.Rdb.OleDbProviderHelper {
public override string FormatIdentifier(string identifier) {
return identifier;
}
}
The default logic for this method wraps the identifier with base.IdentifierPrefix and base.IdentifierSuffix values, which you can also override. In the sample above I'm only removing all use of prefixes and suffixes, but you can do whatever works best for you.
DevForce will find your custom implementation if you place the class in an assembly on the server specified as one of the probe assemblies for the RdbKey.
Is it possible to specify fields while doing a FindAndModify, so only one field is returned?
Also, is it possible to do an upsert, to create the object if it doesn't exist.
As per:
http://www.mongodb.org/display/DOCS/findAndModify+Command
I can't see any way of adding the additional arguments
EDIT:
Seems to be some confusion - I am using NoRM (C#)
https://github.com/atheken/NoRM/
I'm afraid it's not possible actually in NoRM. You could fork the project and add an overloaded FindAndModify method to the file NoRM/Collections/MongoCollectionGeneric.cs to support this behavior.
I think you might need to add a field fields in the anonymous object passed to findOne.
var returnValue = cmdColl.FindOne(new
{
findandmodify = this._collectionName,
query = query,
update = update,
sort = sort,
fields = fields
}).Value;
And maybe a pull request :)
Use the fields specifier. e.g.
db.foo.findAndModify({query:{_id:"myid"},
update:{$set:{priority:78}},new:true,fields:{_id:1,priority:1}})
I seem to be gnawing at this issue, and although I can see how it should work, I can't seem to see the wood for the trees..
Basically I have a situation where I have a SQL 2008 stored procedure.. very basic.. simple varchar's and int's etc.
Because I am getting the value/pair data from javascript, of course I don't implicitly know the DbType to set.. I know this sounds a bit off perhaps, and I could technically add some sort of tag to the fieldname to indicate the actual type.
So right now I am trying to do the following:
(I am using the Enterprise Library Data Access Block, but db is the database object/connection object) - also the val/pairs are from a hashtable..
db.DiscoverParameters(cmd);
IDictionaryEnumerator de;
de = PDMdata.GetEnumerator();
while (de.MoveNext())
{
DbType ptype = cmd.Parameters[de.Key.ToString()].DbType;
db.AddInParameter(cmd, de.Key.ToString(), ptype, (ptype)de.Value.ToString());
}
For reference, the db.AddInParameter takes the Command object, param name, param type and param value..
As you can see I am trying to cast the value as correct SQL DbType.. but of course it is trying to use the normal C# Type..
Any ideas of converting the value to the dynamic SQL DbType??
Thanks in advance for your help..
The SqlCommand.Paramaters collection has an AddWithValue(String name, Object value) method... so you can skip the SqlDbType altogether if you're so inclined.
Ok, silly me.. if I am already checking the format/data quality in the frontend/middleware, then I can (because I am using the Enterprise Library 5.0) .SetParameterValue(cmd, paramname, paramvalue) and the parameter discovery will take care of the rest...
So basically instead of using the Explicit .AddInParameter (which needs the DbType), the above solution works for me.. might for you too in this scenario.