[英]Send an Unspecified Number of Parameters to a Method
I have a program that at the moment is hard coded to send a number of parameters that the user inputs into a DBF file.我有一个程序,目前是硬编码的,用于发送用户输入到 DBF 文件中的一些参数。 I cannot leave the parameters empty (DBF just doesn't seem to allow it) so now I am simply putting in a blank which is not necessarily a problem.
我不能将参数留空(DBF 似乎不允许这样做),所以现在我只是将参数留空,这不一定是问题。
This is the method so far;这是目前为止的方法;
public bool SendToDBF(string name, string town, string pcode)
{
int id, querytype, personID;
string whatfile, netname, whatProgram, blank;
id = 1;
whatfile = "Compns";
querytype = 1;
netname = Environment.UserName;
personID = 8948;
whatProgram = "Sales";
blank = "";
try
{
string constr = ConfigurationManager.ConnectionStrings["dbfString"].ConnectionString;
using (var dbfCon = new OleDbConnection(constr))
{
dbfCon.Open();
var dbfCmd = new OleDbCommand("INSERT INTO fromsql VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", dbfCon);
dbfCmd.Parameters.Clear();
dbfCmd.Parameters.AddWithValue("@fq_uniqid", id);
dbfCmd.Parameters.AddWithValue("@fq_whfile", whatfile);
dbfCmd.Parameters.AddWithValue("@fq_what", querytype);
dbfCmd.Parameters.AddWithValue("@fq_whonm", netname);
dbfCmd.Parameters.AddWithValue("@fq_whoid", personID);
dbfCmd.Parameters.AddWithValue("@fq_whoprog", whatProgram);
dbfCmd.Parameters.AddWithValue("@param1", name);
dbfCmd.Parameters.AddWithValue("@param2", town);
dbfCmd.Parameters.AddWithValue("@param3", pcode);
dbfCmd.Parameters.AddWithValue("@param4", blank);
dbfCmd.Parameters.AddWithValue("@param5", blank);
dbfCmd.Parameters.AddWithValue("@param6", blank);
dbfCmd.Parameters.AddWithValue("@param7", blank);
dbfCmd.Parameters.AddWithValue("@param8", blank);
dbfCmd.Parameters.AddWithValue("@param9", blank);
dbfCmd.Parameters.AddWithValue("@param10", blank);
dbfCmd.Parameters.AddWithValue("@param11", blank);
dbfCmd.Parameters.AddWithValue("@param12", blank);
dbfCmd.Parameters.AddWithValue("@param13", blank);
dbfCmd.Parameters.AddWithValue("@paraml1", blank);
dbfCmd.Parameters.AddWithValue("@paraml2", blank);
dbfCmd.ExecuteNonQuery();
return true;
}
}
catch (OleDbException ex)
{
MessageBox.Show("Error Updating MySQL: " + ex);
return false;
}
}
What I would like is that the amount of paramaters sent to DBF is decided on how many fields the user fills in for the company, for example if they fill in just the name, town and post code then SendToDBF will take 3 params
as well as the predefined parameters such as ID
, Whatfile
and QueryType
.我想要的是,发送到 DBF 的参数数量取决于用户为公司填写的字段数量,例如,如果他们只填写名称、城镇和邮政编码,那么 SendToDBF 将需要 3 个
params
以及预定义的参数,例如ID
、 Whatfile
和QueryType
。
Is there a way that I can make SendToDBF take an unspecified number of parameters, and fill in the rest that are not in putted by the user with blanks?有没有一种方法可以让 SendToDBF 采用未指定数量的参数,并用空格填充用户未输入的其余参数?
If the type is always string
you can use the params
keyword in C# :如果类型始终是
string
,则可以在 C# 中使用params
关键字:
public static void TestParams(params string[] Params)
{
foreach (string param in Params)
{
Console.WriteLine(param);
}
}
And then decide on the value name based on the index in Params
然后根据
Params
的索引决定值名称
Example:例子:
public static void TestParams(params string[] Params)
{
// init dbf up here
int maxParams = 12;
for(int i = 1; i <= maxParams; i++)
{
string paramName = "@param" + i;
if(Params.Length >= i)
dbfCmd.Parameters.AddWithValue(paramName, Params[i - 1]); // array index is zero-based
else
dbfCmd.Parameters.AddWithValue(paramName, blank);
}
}
I don't know the exact context of your problem, but you can:我不知道您的问题的确切背景,但您可以:
So you will have a所以你会有一个
new Dictionary<string, string> { {"firstName": "John"}, {"age": "42"} }
and for the rest of the parameters, have an array with keys on which you can diff and populate the remaining with blank (i guess your procedure needs all the parameters)对于其余的参数,有一个带有键的数组,您可以在其中比较并用空白填充其余部分(我猜您的程序需要所有参数)
or another alternative: use a default dictionary containing all parameters with default values for each column:或另一种选择:使用包含所有参数的默认字典,每列具有默认值:
new Dictionary<string, string> { "status": "1" }
Then you would just merge the defaults with the parameter, then add all the keys and values to the command.然后您只需将默认值与参数合并,然后将所有键和值添加到命令中。
See: Merging dictionaries in C#请参阅: 在 C# 中合并字典
You can use params
:您可以使用
params
:
public bool SendToDBF(params string[] parameters)
{
}
public bool SendToDBF(params object[] parameters)
{
}
I would build a dictionary of field names and values, and construct the SQL myself.我会构建一个字段名称和值的字典,并自己构建 SQL。 You can pass in the dictionary from the method, or build it internally.
您可以从方法中传入字典,也可以在内部构建它。
Dictionary<string, object> d = new Dictionary<string, object>();
d["personID"] = 1234;
// ...
The SQL can be built like this: SQL 可以这样构建:
StringBuilder sb = new StringBuilder();
sb.Append("insert into ");
sb.Append(tableName);
sb.Append(" (");
StringBuilder sbValues = new StringBuilder();
sbValues.Append("values (");
bool first = true;
foreach (KeyValuePair<string, object> kvp in d)
{
if (first)
{
first = false;
}
else
{
sb.Append(", ");
sbValues.Append(", ");
}
sb.Append(kvp.Key);
sbValues.Append("?");
// put the value in a SQL parameter;
dbfCmd.Parameters.AddWithValue("@" + kvp.Key, kvp.Value);
}
sb.Append(") ");
sbValues.Append(")");
sb.Append(sbValues.ToString());
string fullSql = sb.ToString();
It separates building the values string and the rest of the SQL statement, since you else need to go over the collection twice, which seems unnecessary.它将构建值字符串和 SQL 语句的其余部分分开,因为您需要两次遍历集合,这似乎没有必要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.