gpt4 book ai didi

c# - 参数 '@id' ,将数据插入数据库时​​未提供

转载 作者:搜寻专家 更新时间:2023-10-30 19:47:21 26 4
gpt4 key购买 nike

我尝试根据 id 插入数据,我已经在参数中添加了 id,但错误仍然显示未提供 id。

public static int ExecuteNonQuery(string sql, CommandType type, params SqlParameter[] param)
{
using (SqlConnection conn = new SqlConnection(GetConnectionString))
{
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandType = CommandType.StoredProcedure;
PrepareCommand(sql, conn, cmd, type, param);
return cmd.ExecuteNonQuery(); // Error here (unhandled exception)
}
}

插入数据函数

public int AddUsers(Users user)
{
int rel = 0;
string sql = "INSERT INTO Student
VALUES (@id, @groupId, @userInfo, @imagePath)";

SqlParameter[] param = {
new SqlParameter("@id", user.uid),
new SqlParameter("@groupId", user.groupId),
new SqlParameter("@userInfo",user.userInfo),
new SqlParameter("@imagePath",user.imagePath),
};

rel = SqlHelper.ExecuteNonQuery(sql, CommandType.Text, param);

return rel;
}

如果 id 不为空,我将调用该方法。

 if(id != 0)
{
int rel = AddUsers(user);
}

最佳答案

您正在检查 id != 0 但使用 user.uid 作为 @id 的参数。 user.uid 是什么类型?并确保它是 != null,因为如果您添加 null 作为参数值,它将被视为未提供。如果需要将 NULL 插入数据库,请使用 DbNull.Value

关于c# - 参数 '@id' ,将数据插入数据库时​​未提供,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51215238/

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