如何将多个图像添加到数据库。 [英] How to add multiple images to the database.

查看:65
本文介绍了如何将多个图像添加到数据库。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



[来自OP评论的代码]

I have web application in asp.net 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.net 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


这篇关于如何将多个图像添加到数据库。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆