我的Web应用程序中有一个OleDB连接,它连接到MS Access数据库,并带有几个搜索参数(OrderID
,LineNumber
)作为输入。这在过去几年中运作良好。
现在我被要求启用多个LineNumbers
的搜索(例如:2,3,8)。
如果我硬编码它当然有效:
and ([Position] in ('2','3','8') or @LINENUM ='')
如何使用给定的输入格式动态实现此操作作为逗号分隔的字符串?
我试图使用C#在OleDB命令字符串之外使用LineNumbers
构建一个字符串,然后传递参数,但它不起作用。
and ([Position] in @LineNumbers or @LINENUM ='')
我还试图在OleDB命令中使用LineNumbers
构建一个表,如:
DECLARE @LineNumber TABLE (Value INT)
INSERT INTO @LineNumber VALUES (2)
INSERT INTO @LineNumber VALUES (3)
INSERT INTO @LineNumber VALUES (8)
and ([Position] IN (SELECT Value FROM @LineNumber))
然后是错误
“无效的SQL语句;预期'DELETE','INSERT','PROCEDURE','SELECT'或'UPDATE'”
被抛出。
我该怎么做?
这就是我的代码:
string SelectOleDb = @"SELECT SalesID, CertificationID, CalibrationDate, Path
FROM Zeugnis WHERE (SalesID = @SALESID or @SALESID = '')
and ([Position] = @LINENUM or @LINENUM ='')
UNION ALL
SELECT SalesID, CertificationID, CalibrationDate, Path
FROM Zeugnis_2016 WHERE (SalesID = @SALESID or @SALESID = '')
and ([Position] = @LINENUM or @LINENUM ='')
UNION ALL
SELECT SalesID, CertificationID, CalibrationDate, Path
FROM Zeugnis_2015 WHERE (SalesID = @SALESID or @SALESID = '')
and ([Position] = @LINENUM or @LINENUM ='')
ORDER BY CalibrationDate, CertificationID";
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\XXXXXXXX");
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter da = new OleDbDataAdapter();
cmd = new OleDbCommand(SelectOleDb, con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("SALESID", SALESID.Text);
cmd.Parameters.AddWithValue("LINENUM", LINENUM.Text);
答案 0 :(得分:1)
我认为您需要更改此行:
const url = "https://api.flickr.com/services/feeds/photos_public.gne?tags=kitten&format=json"
class App extends React.Component {
constructor() {
super()
this.state = {
items: []
}
}
componentWillMount() {
axios.get(url)
.then((response) => {
console.log(response.data.jsonFlickrFeed.items);
this.setState({
items: response.data.jsonFlickrFeed.items
})
})
.catch((err) => {
})
}
render() {
const mappedStorage = this.state.items.map((item) => <li>{item.media.m} </li>)
return (
<div>
hello
<ul>{mappedStorage}</ul>
</div>
);
}
}
ReactDOM.render(<App />, document.body)
在C#中使用这样的东西:
and ([Position] in @LineNumbers or @LINENUM ='')
然后将string LineNumber = "('2','3','8')"; // build this string dynamically
string sqlCondition = " and ([Position] in " + LineNumbers + " or @LINENUM ='')";
字符串连接到您的主sqlCondition
字符串。
如果您需要完整示例,请发布通过OleDB运行SQL查询的完整代码段。