我终于让它工作但没有使用system.data.odbc。我发现了可以安装到项目中的iAnywhere .net参考。我在C:\ Program Files \ SQL Anywhere 12 \ Assembly \ v4.5下找到我的,然后我添加了对我的项目浏览的引用到这个文件夹并导入了iAnywhere.Data.SQLAnywhere.v4.5.dll ..使用这个引用即使在查询中使用过滤器,我现在也可以获得结果。
这是现在的代码:
public List<SearchArticleNr> SearchArticle(string articlenr) { List<SearchArticleNr> SarticleList = new List<SearchArticleNr>(); iAnywhere.Data.SQLAnywhere.SAConnection myConnection = null; iAnywhere.Data.SQLAnywhere.SACommand myCommand = null; iAnywhere.Data.SQLAnywhere.SADataReader myDataReader = null; try { myConnection = new iAnywhere.Data.SQLAnywhere.SAConnection(conStrMonitor); myConnection.Open(); myCommand = myConnection.CreateCommand(); myCommand.CommandText = "Select art_artnr, art_ben from monitor.ARTIKEL where art_artnr = ?"; myCommand.Parameters.Add("@art", articlenr); myDataReader = myCommand.ExecuteReader(); int i = 0; while (myDataReader.Read()) { i++; SearchArticleNr article = new SearchArticleNr(); article.Article = myDataReader["art_artnr"].ToString(); article.Ben = myDataReader["art_ben"].ToString(); SarticleList.Add(article); } if (i == 0) { SearchArticleNr article = new SearchArticleNr(); article.Article = "NO OBJECTS FOUND"; article.Ben = "NO OBJECTS FOUND"; SarticleList.Add(article); } } catch (Exception exp) { myConnection = null; throw exp; } finally { myConnection.Close(); myDataReader.Close(); myCommand = null; } return SarticleList.ToList(); }
你从不关闭连接。 var sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); 或类似的东西(我很久没有使用ODBC了 CommandBehavior.CloseConnection 可能不是正确的答案,但你可以google它)。然后 while 会受到打击。
var sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
CommandBehavior.CloseConnection
while