我的插入语句如下所示:
<!-- FileName filter for ACE Report download from CSREPORT folder -->
<bean id="aceReportFileNameFilter" class="com.xyz.ACEReportFileNameFilter" />
<bean id="aceReportDownloadStartPolicy"
class="org.apache.camel.routepolicy.quartz2.CronScheduledRoutePolicy">
<!-- start every 2min from monday to saturday - TESTING -->
<property name="routeStartTime" value="0 0/2 * ? * 1-6 *" />
<!-- Run every 2nd and 23rd of the month at 1AM - PRODUCTION -->
<!-- <property name="routeStartTime" value="0 0 1 2,23 * ? *" /> -->
</bean>
<routeContext id="aceReportDownload" xmlns="http://camel.apache.org/schema/spring">
<route autoStartup="false" id="CSReportFtp" routePolicyRef="aceReportDownloadStartPolicy">
<from id="_from1"
uri="ftp://user:pwd@host:port/reportFolder?filter=#aceReportFileNameFilter" />
<log message="Testing ACE report FTP download" />
<to id="_to1" uri="file://{{downloadFolderPath}}/temp" />
</route>
</routeContext>
我想把它转换成一个通用的插入函数,我可以将表名,几个列和值作为数组传递给它,函数将构造insert语句并执行它。我确实尝试使用StringBuilder创建它,但我无法成功完成它。任何帮助表示赞赏。谢谢!
答案 0 :(得分:-1)
这应该这样做。
public void dbQuery(string tablename, string[] columns, string[] values)
{
OleDbConnection conn2 = new OleDbConnection();
using (OleDbCommand cmd2 = conn2.CreateCommand())
{
conn2.Open();
StringBuilder build = new StringBuilder($"INSERT INTO {tablename} {string.Join(",",columns)}");
foreach (string paramValue in values)
{
int count = 0;
string paramName = $"@Type0{count.ToString()}";
build.Append(paramName);
cmd2.Parameters.Add(
new OleDbParameter(paramName, paramValue.ToString()),
);
count++;
}
cmd2.CommandText = build.ToString();
cmd2.ExecuteNonQuery();
conn2.Close();
}
}
自己注意字符串null /空值。
答案 1 :(得分:-1)
我建议你使用一个自定义类来处理你的列/值管理,就像这样;
public class MyCustomColumn
{
public string ColumnName {get; set;}
public object Value {get; set;}
}
这样你就可以传入一个数组;
CreateGenericInsertFunction(string tableName, MyCustomColumn[] columns)
您可以轻松使用KeyValuePair
或Tuple
,但自定义类在可扩展性方面为您提供了更大的灵活性。
然后你可以做类似的事情;
string colList = "";
string valList = "";
foreach (var col in columns)
{
if (colList.length == 0)
{
colList += ", " + col.ColumnName;
}
else
{
colList += col.ColumnName;
}
// repeat for Value
}
// Finally, join it all together.