.net core .Net核心Npgsql准备好的语句
我一直试图通过使用准备好的语句来更改.Net核心应用程序中的一些SQL语句,使其更易于重用,但我在使用NpgsqlDbType时遇到了问题 我试着按照文件说明去做.net core .Net核心Npgsql准备好的语句,.net-core,centos7,npgsql,.net Core,Centos7,Npgsql,我一直试图通过使用准备好的语句来更改.Net核心应用程序中的一些SQL语句,使其更易于重用,但我在使用NpgsqlDbType时遇到了问题 我试着按照文件说明去做 NpgsqlCommand command = new NpgsqlCommand ( " select * from computers where com_phys = @com_phys ", dbconnection ); command.Parameters.Add("com_p
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();
NpgsqlDbType位于NpgsqlTypes命名空间中。确保在顶部有一个使用NpgsqlTypes的窗口 如果要同时设置值,请使用
AddWithValue
而不是Add
NpgsqlCommand=newnpgsqlcommand(
“从com_phys=@com_phys的计算机中选择*”,
数据库连接
);
AddValue(“com_phys”,NpgsqlDbType.Varchar,value);
//或者command.Parameters.AddValue(“com_phys”,NpgsqlDbType.Varchar,size,value);
//或者command.Parameters.AddValue(“com_phys”,value);
command.Prepare();
如果要添加一次参数并执行多次,可以保留对该参数的引用
var parameter=command.Parameters.Add(“com_phys”,NpgsqlDbType.Varchar);
//后来,在一个循环中
parameter.Value=“someValue”;
NpgsqlDbType
位于NpgsqlTypes
命名空间中。请确保顶部有一个使用NpgsqlTypes
,或者使用NpgsqlTypes.NpgsqlDbType.Varchar
非常好,非常感谢,现在感觉非常愚蠢。我想你在执行之前不知道如何设置参数a值?这很好,谢谢。不带值就可以添加并在以后为多种用途设置值吗?@LovelySausage根据需要更新了答案这正是我所需要的,Npgsql文档非常混乱,但这非常完美