我想為 Npgsql 查詢執行我的閱讀器,但出現如下錯誤:
'NpgsqlBatchCommand' does not contain a definition for 'Connection' and no accessible extension method 'Connection' accepting a first argument of type 'NpgsqlBatchCommand' could be found (are you missing a using directive or an assembly reference?) [Assembly-CSharp]
&
'NpgsqlBatchCommand' does not contain a definition for 'ExecuteReader' and no accessible extension method 'ExecuteReader' accepting a first argument of type 'NpgsqlBatchCommand' could be found (are you missing a using directive or an assembly reference?) [Assembly-CSharp]
有誰知道為什么?還是6.0.5版本有新功能或被洗掉???
這是我的代碼:
using Npgsql;
void Start()
{
using(NpgsqlConnection conn = new NpgsqlConnection())
{
conn.ConnectionString = "Server = localhost; Port = 5433; Database =
Postgres2; User Id = postgres; Password = admin";
try
{
NpgsqlBatchCommand cmd = new NpgsqlBatchCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT id FROM m_pasukan";
cmd.Connection = conn;
conn.Open();
Debug.Log("Connection Open!");
NpgsqlBatchCommand sdr = cmd.ExecuteReader();
while(sdr.Read())
{
int id = (int)sdr["id"];
Debug.Log(id);
}
}
catch(Exception ex)
{
Debug.Log("Cannot Open Connection!!");
}
}
}
uj5u.com熱心網友回復:
他們沒有走了。使用了錯誤的類。該代碼使用NpgsqlBatchCommand而不是NpgsqlCommand。派生自 ADO.NET 的DbCommand基類并實作IDbCommand介面的類是 NpgsqlCommand,而不是 NpgsqlBatchCommand。
您的代碼應該是:
var cmd = new NpgsqlCommand();
甚至 :
using(var cmd=new NpgsqlCommand())
{
...
}
或者
using(var cmd=new NpgsqlCommand(sql,conn))
{
...
}
ExecuteReader回傳一個也需要處理的 DbDataReader。DbDataReader 是查詢結果上的快進游標,同時消耗客戶端和服務器資源:
using(var sdr = cmd.ExecuteReader())
{
...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/493914.html
