C# NpgsqlDataReader不返回任何内容
我在asp.net项目中使用Npgsql处理postgreSQL 我遇到一个问题-NpgSqlDataReader不返回任何内容:C# NpgsqlDataReader不返回任何内容,c#,sql,postgresql,npgsql,C#,Sql,Postgresql,Npgsql,我在asp.net项目中使用Npgsql处理postgreSQL 我遇到一个问题-NpgSqlDataReader不返回任何内容: NpgsqlCommand comm = new NpgsqlCommand(command, dbConnect); DataTable dt = new DataTable(); NpgsqlDataReader reader = comm.ExecuteReader(CommandBehavior.CloseCo
NpgsqlCommand comm = new NpgsqlCommand(command, dbConnect);
DataTable dt = new DataTable();
NpgsqlDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection);
dt.Load(reader);
return dt;
命令字符串:
string command = "SELECT mark, wmi, add_cod, manufacturer, mark_owner, country_code, country, additional_info FROM wmi_table WHERE wmi = '" + wmi.ToUpper() + "'; ";
在调试中,它是:
SELECT mark, wmi, add_cod, manufacturer, mark_owner, country_code, country, additional_info FROM wmi_table WHERE wmi = 'XTA';
所以,这是正确的字符串,我在pgAdmin的查询工具中使用它,它返回正确的行。
与数据库的连接稳定(通过pgAdmin检查,而我的应用程序在调试中停止)
还尝试:
da = new NpgsqlDataAdapter();
da.SelectCommand = new NpgsqlCommand(command, dbConnect);
DataSet ds = new DataSet("dataSet");
da.Fill(ds, "dataSet");
return ds;
也不起作用。如何修复它?旁注:不要硬编码值
“+wmi.ToUpper()+”
,而是使用参数您不会遇到异常。这意味着数据库没有数据(连接到错误的服务器或服务器中的数据库),或者查询不正确。我始终建议在使用c#进行测试之前,在供应商工具(如SQL Server Management Studio)中测试查询,以验证查询的语法。