C# 如何检查查询是否得到结果
我试图用一个简单的“选择”查询从数据库中获取一些数据,但我没有得到任何数据。当我在oracle的Toad编辑器中逐字运行相同的查询时,我得到的数据非常好 在C中 编辑: 我还有一个替代方法,它适用于我的其他查询,但不适用于这个查询。此方法也不会为数据表提供任何值C# 如何检查查询是否得到结果,c#,database,oracle,C#,Database,Oracle,我试图用一个简单的“选择”查询从数据库中获取一些数据,但我没有得到任何数据。当我在oracle的Toad编辑器中逐字运行相同的查询时,我得到的数据非常好 在C中 编辑: 我还有一个替代方法,它适用于我的其他查询,但不适用于这个查询。此方法也不会为数据表提供任何值 dtHRSinfo.Load(cmd.ExecuteReader()); 询问 select unitid,lasttimeon,lasttimeoff,currentlyon,currentcallontime, currentca
dtHRSinfo.Load(cmd.ExecuteReader());
询问
select unitid,lasttimeon,lasttimeoff,currentlyon,currentcallontime, currentcallofftime
from opr.mktunit
我没有收到任何错误或异常。db读取器从不获取值,也从不将它们分配给datatable。我不知道我做错了什么
数据库连接
public bool OpenDBConnections(string user, string pass)
{
try
{
this.Log("Connection to Database");
cnOprPrd = new OdbcConnection();
cnOprPrd.ConnectionString = @"Driver={Microsoft ODBC for Oracle};Server=OPRPRD;Uid=" + user + ";Pwd=" + pass;
cnOprPrd.Open();
//INPUT FOLDER
sqlFolder = AC2_SQL_FOLDER;
return true;
}
catch (Exception e)
{
Log(e.Message);
return false;
}
}
更新:
我通过在字符串变量中显式输入命令,而不是使用Streamreader从文件中读取字符串,解决了这个问题
该问题可能是由于streamreader中的某些无法识别的字符序列引起的。谢谢你们的回答。很可能是你们的连接字符串。调试cmd对象或向我们显示设置连接字符串的代码。我们还需要讨论如何处理非托管资源类。。。使用using语法确保正确清理连接:
using (OdbcDataReader reader = cmd.ExecuteReader()) //cmd contains the query
{
DataTable dtHRSinfo = new DataTable();
while (reader.Read()) // no lines to read
{
dtHRSinfo.Rows.Add(reader); // never comes to this statement!
}
}
很可能是您的连接字符串。调试cmd对象或向我们显示设置连接字符串的代码。我们还需要讨论如何处理非托管资源类。。。使用using语法确保正确清理连接:
using (OdbcDataReader reader = cmd.ExecuteReader()) //cmd contains the query
{
DataTable dtHRSinfo = new DataTable();
while (reader.Read()) // no lines to read
{
dtHRSinfo.Rows.Add(reader); // never comes to this statement!
}
}
该区块:
while (reader.Read()) // no lines to read
{
dtHRSinfo.Rows.Add(reader); // never comes to this statement!
}
看起来很奇怪。我从未见过直接从数据读取器向数据表一次添加一行的扩展方法
我希望看到所使用的方法:
OdbcDataReader reader = cmd.ExecuteReader(); //cmd contains the query
DataTable dtHRSinfo = new DataTable();
dtHRSinfo.Load(reader);
我的猜测是,它使用了一个Add重载,将reader.ToString的值隐式添加到第一列。此块:
while (reader.Read()) // no lines to read
{
dtHRSinfo.Rows.Add(reader); // never comes to this statement!
}
看起来很奇怪。我从未见过直接从数据读取器向数据表一次添加一行的扩展方法
我希望看到所使用的方法:
OdbcDataReader reader = cmd.ExecuteReader(); //cmd contains the query
DataTable dtHRSinfo = new DataTable();
dtHRSinfo.Load(reader);
我的猜测是,它使用了一个Add重载,将reader.ToString的值隐式添加到第一列。根据我的建议,如果您可以连接到数据库,这应该很简单
DataSet ds = new DataSet();
OracleConnection connection;
OracleDataAdapter OracleAdapter;
connection = new OracleConnection(ConfigurationManager.AppSettings["ConnectionString"]);
connection.Open();
OracleAdapter = new OracleDataAdapter(ConfigurationManager.AppSettings["your select statement"], ConfigurationManager.AppSettings["ConnectionString"]);
OracleAdapter.Fill(ds, "your table name");
使用fill方法将把数据库表中的所有数据填充到数据集中
使用configurationManager.Appsetting引用
using System.Configuration;
添加一个配置文件,并使用中的键值标记将选择查询外部化
<appSettings>
<addKey="your select statement" value
"select unitid,lasttimeon,lasttimeoff,currentlyon,currentcallontime, currentcallofftime
from opr.mktunit"/>
看看这对你是否有效让我知道。干杯我建议,如果你能连接到数据库,这应该很简单,试试这个
DataSet ds = new DataSet();
OracleConnection connection;
OracleDataAdapter OracleAdapter;
connection = new OracleConnection(ConfigurationManager.AppSettings["ConnectionString"]);
connection.Open();
OracleAdapter = new OracleDataAdapter(ConfigurationManager.AppSettings["your select statement"], ConfigurationManager.AppSettings["ConnectionString"]);
OracleAdapter.Fill(ds, "your table name");
使用fill方法将把数据库表中的所有数据填充到数据集中
使用configurationManager.Appsetting引用
using System.Configuration;
添加一个配置文件,并使用中的键值标记将选择查询外部化
<appSettings>
<addKey="your select statement" value
"select unitid,lasttimeon,lasttimeoff,currentlyon,currentcallontime, currentcallofftime
from opr.mktunit"/>
看看这对你是否有效让我知道。欢呼通常,当你看到标签[C]和[sql]同时出现时,你会认为是MS sql Server,但在你的帖子中,你提到了Oracle。你能说得更具体一点吗?检查reader.HasRows==trueI是否也强烈建议使用ORM框架,如实体框架,并避免许多此类情况。当你看到标签[C]和[sql]在一起时,你通常会快速浏览谷歌,你会认为是MS sql Server,但在你的帖子中,你提到了Oracle。你能说得更具体一点吗?检查reader.HasRows==trueI是否也强烈建议使用ORM框架,如实体框架,并避免许多此类情况。我用谷歌快速搜索了一下,两种方法都没用。我正在使用.LOADreader方法进行其他一些查询,它们工作得很好。但对于这一次,我不明白为什么数据没有粘住。我尝试了两种方法都没有用。我正在使用.LOADreader方法进行其他一些查询,它们工作得很好。但对于这一次,我不明白为什么数据没有粘住。