Reputation: 2288
I have 5 parameters and I want to send them to the method:
public static SqlCommand getCommand(string procedure, SqlParameter[] parameter)
{
Sqlcommand cmd;
return cmd
}
Can I send these paramters at one time like this?
SqlParameterCollection prm;
prm.Add(p1);
prm.Add(p2);
prm.Add(p3);
prm.Add(p4);
prm.Add(p5);
sqlcommand cmd = getCommand(prm);
Upvotes: 14
Views: 70671
Reputation: 331
1.
public IEnumerable<SqlParameter> GetAndSetParameters(List<Tuple<string, string>> parameters){
List<SqlParameter> paramlist = new List<SqlParameter>();
foreach (var item in parameters)
{
paramlist.Add(new SqlParameter(item.Item1, item.Item2));
}
return paramlist;
}
2. pass parameters
public List<Tuple<string, string>> GetUserParameter(){
List<Tuple<string, string>> list = new List<Tuple<string, string>>();
list.Add(new Tuple<string, string>("@User",user.UserID));
return list;
}
3. finally use it:
SqlCommand oCmd = new SqlCommand(oString, myConnection);
oCmd.Parameters.AddRange(GetAndSetParameters(GetUserParameter()).ToArray());
Upvotes: 0
Reputation: 162
Here is my code.You can use all parameter properties like name,value,type etc.
int SelectedListID = 6;
string selectedPrefix = "IP";
string sqlQuery = "select * from callHistory where ImportID=@IMPORTID and Prefix=@PREFIX"
SqlParameter[] sParams = new SqlParameter[2]; // Parameter count
sParams[0] = new SqlParameter();
sParams[0].SqlDbType = SqlDbType.Int;
sParams[0].ParameterName = "@IMPORTID";
sParams[0].Value = SelectedListID;
sParams[1] = new SqlParameter();
sParams[1].SqlDbType = SqlDbType.VarChar;
sParams[1].ParameterName = "@PREFIX";
sParams[1].Value = selectedPrefix;
SqlCommand cmd = new SqlCommand(sqlQuery, sConnection);
if (sParams != null)
{
foreach (SqlParameter sParam in sParams)
{
cmd.Parameters.Add(sParam);
Application.DoEvents();
}
}
Upvotes: 1
Reputation: 11
static private void FunctionCall()
{
string connectionString = "DATA Source=nwind;server=GRAPHICS\SQLEXPRESS;Persist Security Info=False;Integrated Security=SSPI;Connect Timeout=30";
string sSqlQuery;
DataSet ds;
DataTable dt;
// Prepare SQL Query
sSqlQuery = @"
select content " +
"from " +
"[TBL] where id = '000-000'";
SqlParameter[] sqlParams = {
new SqlParameter("",SqlDbType.Int),
new SqlParameter("",SqlDbType.VarChar),
new SqlParameter("",SqlDbType.VarChar)
};
// Read from database
ds = SqlHelper.ExecuteNonQuery(connectionString, sSqlQuery, CommandType.Text, sqlParams);
dt = ds.Tables[0];
}
// Executes a non query
public static int ExecuteNonQuery (string connectionString, string cmdText, CommandType type, SqlParameter[] prms)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(cmdText, conn))
{
cmd.CommandType = type;
if (prms != null)
{
foreach (SqlParameter p in prms)
{
cmd.Parameters.Add(p);
}
}
conn.Open();
return cmd.ExecuteNonQuery();
}
}
}
Upvotes: 0
Reputation: 1165
Using this as inspiration, this code worked for me:
List<SqlCeParameter> parameters = new List<SqlCeParameter>();
parameters.Add(new SqlCeParameter("@Username", NewUsername));
parameters.Add(new SqlCeParameter("@Password", Password));
cmd.Parameters.AddRange(parameters.ToArray());
Upvotes: 6
Reputation: 14057
I don't see what's wrong with that? You do know that, if this is .NET, you need to attach the parameters to the SqlCommand
object?
So:
SqlCommand query = new SqlCommand(sqlString, Connection);
query.Parameters.AddWithValue(parameter,valueToPass);
etc?
Sorry if that's not related, not completely sure on your question? Your method doesn't really do anything, I take you left out the code and just put in a dummy for the purposes of asking the question? You can pass an array as an arguement so you just need to spilt it up?
Upvotes: 7
Reputation: 14286
Or create an array of parameters by hand:
SqlParameter[] parameter = {
new SqlParameter(...),
new SqlParameter(...),
new SqlParameter(...)
};
But I don't see what should be wrong with your approach. It simple, readable and understendable.
Upvotes: 19
Reputation: 103770
Well, that won't compile because in your call to getCommand you're not passing in a string with the procedure, but as far as the array, that should work no problem.
Upvotes: 2