.Net Core Npgsql Hazırlanmış Açıklama

Sep 09 2020

Bir .Net Core uygulamasındaki bazı SQL ifadelerini Hazırlanmış İfadeleri kullanarak daha yeniden kullanılabilir hale getirmeye çalışıyorum ancak NpgsqlDbType ile sorun yaşıyorum.

Belgeleme talimatlarını izlemeye çalıştım.

NpgsqlCommand command = new NpgsqlCommand (
    " select * from computers where com_phys = @com_phys ",
    dbconnection
);
command.Parameters.Add("com_phys", NpgsqlDbType.Varchar);
command.Prepare();

Ama derleme yapamıyor, diyor ki

The name 'NpgsqlDbType' does not exist in the current context

Bir şey mi kaçırıyorum? NpgsqlDbType'ı nasıl kullanırım?

GÜNCELLEME

Başkasının yararına olabilir diye son çalışma olayını buraya koyuyorum

// prepare

NpgsqlCommand command = new NpgsqlCommand (
    " select * from computers where com_phys = @com_phys ",
    dbconnection
);
var param01 = command.Parameters.Add("com_phys", NpgsqlDbType.Varchar);
command.Prepare();

// execute 01

param01.Value = "value01";
var results = command.ExecuteReader();
while(results.Read()) {
   // nothing
}
command.Close();

// execute 02

param01.Value = "value02";
var results = command.ExecuteReader();
while(results.Read()) {
   // nothing
}
command.Close();

Yanıtlar

2 ESG Sep 09 2020 at 02:16

NpgsqlDbType, NpgsqlTypes ad alanındadır. En üstte bir NpgsqlTypes kullandığınızdan emin olun.

Değeri aynı anda ayarlamak istiyorsanız, AddWithValuebunun yerine kullanınAdd

NpgsqlCommand command = new NpgsqlCommand (
    " select * from computers where com_phys = @com_phys ",
    dbconnection
);
command.Parameters.AddValue("com_phys", NpgsqlDbType.Varchar, value);
// OR command.Parameters.AddValue("com_phys", NpgsqlDbType.Varchar, size, value);
// OR command.Parameters.AddValue("com_phys", value);
command.Prepare();

Parametreyi bir kez eklemek ve birden çok kez çalıştırmak istiyorsanız, parametreye bir referans tutabilirsiniz.

var parameter = command.Parameters.Add("com_phys", NpgsqlDbType.Varchar);

// Later, in a loop
parameter.Value = "someValue";