forked from lyhabc/SQLServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
picture.cs
74 lines (60 loc) · 1.66 KB
/
picture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
-- Non-Proc
CREATE TABLE Pictures(
Picture VARBINARY(MAX)
)
CREATE PROCEDURE stp_AddImage
@f VARCHAR(1000)
AS
BEGIN
DECLARE @s NVARCHAR(MAX)
SET @s = 'INSERT INTO Pictures (Picture)
SELECT *
FROM OPENROWSET(BULK N''' + @f + ''', SINGLE_BLOB) mf'
EXEC sp_executesql @s
END
*/
// C# Loop
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.SqlClient;
namespace PictureInsert
{
class Program
{
static void Main(string[] args)
{
string dir = @"F:\Images\";
string[] fs = Directory.GetFiles(dir);
foreach (string f in fs)
{
if (f.Trim().EndsWith(".png") | f.Trim().EndsWith(".jpg"))
{
using (var scon = Connection.Connect())
{
SqlCommand addpic = new SqlCommand("EXECUTE stp_AddImage @p", scon);
addpic.Parameters.AddWithValue("@p", f);
addpic.ExecuteNonQuery();
scon.Close();
}
Console.WriteLine("Image " + f + " added." + "\n");
}
}
Console.WriteLine("All images imported.");
Console.ReadLine();
}
}
public static class Connection
{
public static SqlConnection Connect()
{
SqlConnection scon = new SqlConnection();
scon.ConnectionString = "integrated security=SSPI;data source=SERVER;persist security info=False;initial catalog=DATABASE";
scon.Open();
return scon;
}
}
}