2

I'm working on a small application that verifies some entries in an Oracle database by comparing some entries on an excel sheet to the tables in an Oracle database so I'm only working on querying the database and nothing too complex.

I'm trying to get this to be completely dynamic so as I wouldn't have to make any code changes regardless of what the queries are however I'm running into a small issue when the table I'm querying has numeric and string type values.

If chkConnState() Then
sSql = "SELECT * FROM " & workSheetName & " WHERE "
For i = 1 To ds.Tables.Item(0).Columns.Count - 1
sSql &= ds.Tables.Item(0).Columns.Item(i).ToString & " = " & ds.Tables.Item(0).Rows.Item(i).Item(i).ToString & " AND "
Next
newSql = sSql.Substring(0, sSql.Length - 4)
End If

Here I am only gathering the SQL statement to query the table against. I grab the columns and rows information from the Excel(the information I know is correct and am using to check against what's in the table) and then remove the ending 'AND' from the query.

Is there any easy way to determine if the column is numeric or string? The issue I'm having now is I have no idea how to determine where I should add the "'" & value & "'" in string values dynamically. I would say just query by the PK but I'm not sure there's an easy way to find this either as it's different for every table and doing it the way above I will only retrieve one record rather than possible multiple. Is there a better way to go about this to make it dynamic?

4

2 回答 2

2

正如 LarsTech 所说,您可以使用 TryParse 来确定这些值是否为数字。

If(Double.TryParse(ds.Tables.Item(0).Rows.Item(i).Item(i).ToString, Nothing), _
ds.Tables.Item(0).Rows.Item(i).Item(i).ToString, _ 
"'" & ds.Tables.Item(0).Rows.Item(i).Item(i).ToString & "'")

这将在任何非数字值周围加上单引号。

于 2013-10-03T21:19:14.977 回答
0

您可以使用以下 Func 轻松检查字段

public static Func<string, bool> isNumeric = x => { int i = 0; return Int32.TryParse(x, out i); };

简单使用

if (isNumeric(yourField)) {

} 别的 {

}

于 2013-10-03T21:58:05.467 回答