[英]How to insert image before already existing image in database
嗨,编码人员,我只是想知道这是否可以在数据库中的其他任何图像之前将图像插入数据库。
假设我有要上传图像的文件加载器和一个下拉列表,以显示这些图像的优先级。 现在我的数据库中有10张图像,并且我绑定了下拉列表的优先级。 现在,当我上传新图像时,我可以在下拉列表中看到图像的优先级。 现在这部分已经完成
现在,我想要的是上载新图像并从数据库中存储的图像的下拉列表中选择优先级之前的优先级。 示例:我选择文件加载器按钮上传文件,然后从下拉列表中选择优先级5。 现在,该图像将以优先级5存储,而优先级为5的图像现在具有优先级6,依此类推。
我怎样才能做到这一点?
我相信您正在寻找的是使用SQL的简单更新查询
Update table set Priority = Priority + 1 where Priority >= @InputtedPriority
然后,您需要从数据库中删除超过10的任何内容
DELETE FROM table where Priority > 10
编辑
由于阅读了更多注释,因此您似乎需要运行SQL的第一行来更新表以移动优先级。 简单的SqlCommand和SQL Server上的存储过程将立即解决您的问题。 快速了解用于执行SqlCommand和过程的SO并不难找到所需的内容。
EDIT2
一个简单的代码示例,如果您从下拉列表中选择优先级数字30并将其设置为priority
,则应上载的图像将为30,存在于30的图像将移至31,依此类推,具有优先级的图像在50处将变为51。此代码假定ID为IDENTIY
列。
string imageName;
string description;
string path;
int priority;
//(snip) populate the variables
const string query = "Update imagesTable set Priority = Priority + 1 where Priority >= @Priority;" +
"insert into imagesTable (ImageName, Description, Path, Priority) values (@ImageName, @Description, @Path, @Priority)";
using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand(query, connection))
{
connection.Open();
command.Parameters.AddWithValue("@ImageName", imageName);
command.Parameters.AddWithValue("@Description", description);
command.Parameters.AddWithValue("@Path", path);
command.Parameters.AddWithValue("@Priority", priority);
command.ExecuteNonQuery();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.