我一直试图通过使用准备好的语句来更改.Net核心应用程序中的一些SQL语句,以提高可重用性,但我在使用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();发布于 2020-09-08 19:16:26
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";https://stackoverflow.com/questions/63799827
复制相似问题