.Net Core Npgsql 준비 문

Sep 09 2020

.Net Core 앱의 일부 SQL 문을 Prepared Statements를 사용하여 더 재사용 가능하도록 변경하려고 시도했지만 NpgsqlDbType에 문제가 있습니다.

설명서 지침을 따르려고 노력했습니다.

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

그러나 컴파일에 실패하고 있습니다.

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

내가 뭔가를 놓치고 있습니까? NpgsqlDbType을 어떻게 사용합니까?

최신 정보

다른 사람에게 도움이 될 수있는 경우를 대비하여 최종 작업을 여기에 넣습니다.

// 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();

답변

2 ESG Sep 09 2020 at 02:16

NpgsqlDbType은 NpgsqlTypes 네임 스페이스에 있습니다. 상단에 NpgsqlTypes를 사용하고 있는지 확인하십시오.

값을 동시에 설정하려면 AddWithValue대신 사용하십시오.Add

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();

매개 변수를 한 번 추가하고 여러 번 실행하려는 경우 매개 변수에 대한 참조를 유지할 수 있습니다.

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

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