日期:2014-05-17 浏览次数:20851 次
需要在SQL SERVER中创建数据库test, 然后再test中创建一个数据表ImageTable,
含字段 id int 自动增长
ImagePath nchar(50)
Image image类型
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace SaveImageToSqlServer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonSaveImageToDataServer_Click(object sender, EventArgs e)
{
this.folderBrowserDialog1.ShowDialog();
string dirName = this.folderBrowserDialog1.SelectedPath;
DirectoryInfo dir = new DirectoryInfo(dirName);
FileInfo[] fileInfos = dir.GetFiles("*.jpg"); //获取文件夹下所有jpg文件,因为图像文件都很大的,选取文件夹下不要有太多图片,否则Primary区会满了,系统会提示无法再插入数据。
List<string> fileList = new List<string>(100);
foreach (FileInfo fileInfo in fileInfos)
{
fileList.Add(fileInfo.FullName);
System.Windows.Forms.Application.DoEvents();
}
#region Save data to Data Server
string strConn = // 设置连接字符串,我是在本机上连接,远程的话需要修改连接字符串
@"Server=(local);"
+ @"Initial Catalog=test;"
+ @"User Id =fisherman;"
+ @"Password=;"
+ @"Integrated Security =&nb