当前位置:首页 » 编程语言 » 批量图片存入sql
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

批量图片存入sql

发布时间: 2023-08-10 18:06:10

sql Server中怎么用text批量上传图片

你是用SqlServer什么版本,2000?2005?2005中已经没有这个程序了。但可直接对blob 数据类型进行操作:CREATE TABLE #BlobData(BlobData varbinary(max))

--insert blob into temp table
SET @SqlStatement =
N'
INSERT INTO #BlobData
SELECT BlobData.*
FROM OPENROWSET
(BULK ''' + @FileName + ''',
SINGLE_BLOB) BlobData'
EXEC sp_executesql @SqlStatement

--update
UPDATE dbo.MyTable
SET MyBlob = (SELECT BlobData FROM #BlobData)
WHERE MyTable.MyPK = @MyPK

DROP TABLE #BlobData
GO 如果你还是用2000,也就是text方式,参考一下代码: 1 C:\Program Files\Microsoft SQL Server\MSSQL\Binn>text
2 TEXTCOPY Version 1.0
3 DB-Library version 8.00.194
4 Type the SQL Server to connect to: 220.**.**.*
5 Type your login: sa
6 Type your password: sa
7 Type the database: Proct
8 Type the table: Image
9 Type the text or image column: data
10 Type the where clause: where id=16
11 Type the file: c:\abc.gif
12 Type the direction ('I' for in, 'O' for out):I
13 Data copied into SQL Server image column from file 'c:\abc.gif'.

Ⅱ C#控制台程序怎么将本地图片批量插入sql server 2008数据库,请看补充,请大神帮忙给出代码,有追加!

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.IO;
usingSystem.Data.SqlClient;
usingSystem.Data;

namespaceImportImage
{
classProgram
{
staticvoidMain(string[]args)
{
stringconn="server=.;database=testDB;Uid=sa;Pwd=sa";
using(SqlConnectionmyconn=newSqlConnection(conn))
{
myconn.Open();
using(SqlCommandmycomm=newSqlCommand())
{
DirectoryInfodir=newDirectoryInfo(@"C:rimages");
foreach(FileInfoitemindir.GetFiles("*.jpg"))
{
stringstr=string.Format("insertinto[数据库中的表的名字](imagAns)values(@file)",item.Name);//假设你的id是自动增长的
mycomm.CommandText=str;
mycomm.Connection=myconn;
FileStreamfs=newFileStream(item.FullName,FileMode.Open);
BinaryReaderbr=newBinaryReader(fs);
Byte[]byData=br.ReadBytes((int)fs.Length);
fs.Close();
mycomm.Parameters.Add("@file",SqlDbType.Binary,byData.Length);
mycomm.Parameters["@file"].Value=byData;
mycomm.ExecuteNonQuery();
}
}
}
}
}
}