gpt4 book ai didi

c# - command.parameters.addwithvalue 不工作

转载 作者:行者123 更新时间:2023-11-30 23:31:02 29 4
gpt4 key购买 nike

我在使用参数保存记录时遇到问题以下是我的代码

try
{
string usd = "USR" + randomnumber(001, 1000).ToString();
dbconn.poconne.Open();
string useradd = "insert into users values(@userid,@username,@password,@email,@department,@log)";

using (NpgsqlCommand cmdadd = new NpgsqlCommand(useradd, dbconn.poconne))
{
cmdadd.Parameters.AddWithValue("@userid", usd);
}
}
catch (Exception er)
{
MessageBox.Show(er.Message);
}

我收到这个错误

Error 1 'Npgsql.NpgsqlParameterCollection' does not contain a definition for 'AddWithValue' and no extension method 'AddWithValue' accepting a first argument of type 'Npgsql.NpgsqlParameterCollection' could be found (are you missing a using directive or an assembly reference?) C:\Users\ICT PGL\Documents\Visual Studio 2010\Projects\officeautomation\customercare\customercare\users.cs 34 39 customercare

请帮忙解决错误

最佳答案

您的查询:

string useradd = "insert into users (id, name, password, email, department, log) values(:userid,:username,:password,:email,:department,:log)";

您的参数:

cmd.Parameters.Add(new NpgsqlParameter("userid", NpgsqlTypes.NpgsqlDbType.Int));
cmd.Parameters.Add(new NpgsqlParameter("username", NpgsqlTypes.NpgsqlDbType.Text));
cmd.Parameters[0].Value = usd;
cmd.Parameters[1].Value = "username";

我只展示了两个参数。相应地添加其他参数。

最好指定要插入数据的列。

关于c# - command.parameters.addwithvalue 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34760071/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com