如何将多个图像添加到数据库。

编程入门 行业动态 更新时间:2024-10-23 15:18:02
本文介绍了如何将多个图像添加到数据库。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在asp中有web应用程序,其中包含产品代码和详细信息。产品详细信息在sql表中。现在我在我的系统文件夹中有产品的图像。我需要根据每个代码将图像直接添加到sql表中。代码名称类似于110-1,110-2等,图像名称类似于110-1.jpg,110-2.jpg。我已经尝试过sql命令来插入所有图像,但它失败了。 c#asp中是否有任何代码可以将图像更新为代码。请帮助 [来自OP评论的代码]

I have web application in asp in which it contain product codes and details. The product details are in sql table. Now I have the images of the products in my system folder. I need to add the images directly to the sql table according to the each code. The code names are like 110-1, 110-2 etc and image names are like 110-1.jpg,110-2.jpg. I have tried sql commands to insert all the images, but it fails. Is there any code available in c# asp to update the images to the code. pls help [Code from OP's comment]

DECLARE @CODE nvarchar(50) DECLARE image_cursor CURSOR FOR SELECT CODE FROM MyMast WHERE img IS NULL OPEN image_cursor; FETCH NEXT FROM image_cursor INTO @CODE; WHILE @@FETCH_STATUS = 1 BEGIN DECLARE @sql NVARCHAR(MAX) DECLARE @imagePath NVARCHAR(255) SET @imagePath = 'D:\images\'+ RTRIM(LTRIM('@CODE')) + '.jpg' SET @sql = 'UPDATE MyMast ' SET @sql = @sql + 'SET img = (SELECT BulkColumn FROM OPENROWSET( BULK ''' + @imagePath + ''', Single_Blob) AS img) ' SET @sql = @sql + 'WHERE CODE = ''' + (@CODE) +''';'; BEGIN TRY EXECUTE sp_executesql @sql END TRY BEGIN CATCH END CATCH FETCH NEXT FROM image_cursor INTO @CODE; END CLOSE image_cursor; DEALLOCATE image_cursor; SELECT CODE, img FROM MyMast WHERE img IS NOT NULL

推荐答案

驱动器D:是SQL服务器上的驱动器吗? 不,它在我的系统上 那你怎么期望SQL服务器(a)知道那; (b)在不建立共享的情况下访问另一台计算机上的硬盘?这是一个坏主意... 在C#中执行:读取文件数据,将其作为参数化INSERT或UPDATE命令发送给SQL。这样做的代码很简单,这里有介绍:为什么我得到参数无效。我从数据库中读取图像时出现异常? [ ^ ]

根据OP的评论...... 请注意,SQL服务器会在服务器上执行命令,因此对存储的任何引用(例如D:在您的情况下)都与服务器本身有关! 在您的情况下SQL服务器找不到您在PC上下文中创建的路径,但SQL在其自己的上下文中解释它... 您可以执行以下两种操作之一: 1.分享你的路径并让SQL访问它(如果这是一次性的数据操作,它可能是goo) 2.从代码中插入BLOB According to OP's comment... Notice that SQL server suns your command on the server, so any reference to storage (like D: in your case) is relative to the server itself! In your case SQL server can not find the path you provide as you created it in the context of your PC but SQL interpreted it in its own context... You can do one of the two: 1. Share your path and let SQL access it (it can be goo if this is a one-time-only data manipulation) 2. Insert the BLOB from code

更多推荐

如何将多个图像添加到数据库。

本文发布于:2023-10-27 03:01:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532115.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   如何将   图像   数据库

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!