i was wondering, can a parameter be used more then once in the same query, like this :
MySqlParameter oPar0 = new MySqlParameter("e164", MySqlDbType.String);
oPar0.Value = user.E164;
string sSQL0 = "Delete from callmone.call where (caller=?e164 or called=?e164);";
clsDatabase.ExecuteSQL(sSQL0, oPar0);
Is this possible or should i write 2 parameters?
If the database driver handles named parameters, then you can reuse the parameter.
If the database driver doesn't handle named parameters, the parameter names are ignored and you have to add one parameter values for each use, in the exact order that they are used.
From the code that you presented it looks like the driver supports named parameters. If the code runs without an error, it works. If the driver would not support names parameters, the code would cause an error as there is only one parameter value.
I don't know of any reason why you can't do that.
Related
Due to a huge and hateful database that I'm not allowed to rationalise, I am forced to create a C# Datalayer generator for my app. I'm pretty much done, using T4 to generate Model classes and Insight.Database repos but I needed to create my User Defined Table Types and Stored Procedures via Sql Management Objects.
What I'm interested in is, can I use a short-circuit parameter when creating via SMO? What I want to replicate is something like this:
CREATE PROCEDURE [dbo].[cip_GetLicenses]
#fldIndex int = null
AS
SELECT [fldIndex]
,[fldLicenceData]
FROM [dbo].[tblLicences]
WHERE (#fldIndex is NULL OR (fldIndex = #fldIndex))
I can construct the body of the sproc relatively easily with a string builder and some column iteration, but creating a parameter is done separately.
The StoredProcedureParameter Type does actually have a DefaultValue property but it's a string, and sadly setting it to " = null" simply throws exceptions at run time.
Can anyone advise?
We are writing a custom activity. In this activity it is possible to set a database connection string and a name for a stored procedure. At runtime the stored procedure is executing. Now we have some stored procedures which has input parameters.
Is it possible to generate variables dynamically in WF 4.5 for each input parameter in the stored procedure? Reading the parameters from the stored procedure is not the problem, but I dont have any idea how to generate the variables.
Example:
The user enters a name for the stored procedure to be executed (2 input params #Variable1 and #Variable2). Now in the variables tab should be 2 variables: #Variable1 and #Variable2. If the user changes the name in the stored procedure then in the variables tab should be the new params (for example only #Variable2)...
We spent a lot of time on this issue. But the only thing we have learned is that the activity has to be a NativeActivity and the variables should be added in the CacheMetadata method. But if I add a variable with AddVariable() method nothing happens :(
If you are open to including third-party libraries, you could try using an ORM tool like Dapper to accomplish this. A Dapper query generally takes an anonymous type to supply its parameters. Typical code for creating a custom object from fields in a database would look something like this:
IDbConnection db = New IDbConnection(...);
int id = 527; // normally passed in - using a hard coded value would defeat the purpose...
string myQuery = "SELECT Engine, Transmission, Make, Model, BodyStyle FROM Table WHERE ID = #ID";
Car result = db.Query<Car>(myQuery, new { ID = id }).First();
So I believe that you could use reflection to pass in the type ("Car") using reflection and create an anonymous object or pass in an actual object with an "ID" property at runtime. It will automatically create a custom Car object with the resulting data, assuming that the Car object has properties of Engine, Transmission, Make, Model, BodyStyle, etc.
Note that if you don't supply the type you expect to get back, you get an ExpandoObject: Creating an anonymous type dynamically? - you may also be able to pass one of these in for your input parameters, which would mean that you could create it at runtime.
This guy came up with a generic method for Dapper that may help you: http://www.bradoncode.com/blog/2012/12/creating-data-repository-using-dapper.html
What he came up with is something like this:
IEnumerable<T> items = null;
// extract the dynamic sql query and parameters from predicate
QueryResult result = DynamicQuery.GetDynamicQuery(_tableName, predicate);
using (IDbConnection cn = Connection)
{
cn.Open();
items = cn.Query<T>(result.Sql, (object)result.Param);
}
return items;
I've written a bunch of classes that reach out to an API and get JSON. When compiled, I get nice commands like get-host, get-version, get-app, etc.
each cmdlet takes an argument like -host or -app.
I've got about 160 of these that I can write but I'm now looking to write a more generic cmdlet that can accept arbitrary parameters and values.
From what I can see, I can't declare parameters as such where i can issue
get-info -app "appname"
or
get-info -host "hostname"
or
get-info -version "5.5"
unless i declare all the parameters of which there could be hundreds. Also, if the parameter is not declared, it throws a nice error
Get-Info : A parameter cannot be found that matches parameter name 'host'.
Is there a way I can not declare any parameters and then parse the arguments manually or is there a something in c# to automatically parse the list of arguments and then assign them to variables named appropriately?
for example
get-info -host "hostname" -backup "backupid"
and the variables host and backup would be set automatically?
Accept your arbitrary parameters as a HashTable:
get-mydata #{ Host = "hostname"; version = "1.0" }
That is how most of the built-in cmdlets handle arbitrary KVPs.
I'm implementing some connected ADO.NET functions (no EF, no Linq, no datasets). I've built a set of routines that
1) take an instantiated class object corresponding to a table row ("row object"),
2) use Refection to extract the property types, names and values from the row object and store them in a List,
3) use the List to turn those names into a SQL query string using #Value type parameters,
4) use the List to load row object property values into the SQLCommand.Parameters
corresponding to the #Values.
The purpose of this general approach to be able to use this approach with multiple row object types and not have to create separate insert, update and delete SQLCommand routines for each row object type. In the initial test run, this works fine.
Question: what are potential problems or disadvantages that I might run into by not including the SqlDbType in the SQLCommand.Parameters?
Always avoid usage of SqlParameter that do not specify SqlDbType, unless you are sure that you will set the type later.
Please read this.
i am new to oracle .I have procedure which has a param like this
#param1 title_type.title_type_code%type
title_type is custom type
ok.. now
How do I pass the parameter from c# using ODAC( OracleParameter object)
%type is confusing do you need to pass as object or pass as string
Anyone know please help??
This is just a reference to the type that title_type.title_type_code is using, as an alternative to hardcoding the type in two places.
You need to look up the definition of that table, and pass the parameter as whatever that is (could be VARCHAR, could be NUMBER, I doubt that it is an object).