當前位置:首頁 » 編程語言 » 批量圖片存入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();
}
}
}
}
}
}