将一个文件夹下的所有文件写入数据库
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace Test
{
public partial class File2SQL : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//string file = "D:\\FeiQ-v2.5.rar";
string[] FileInfos = Directory.GetFiles("D:\\Text");
foreach (string i in FileInfos)
{
FileStream fs = new FileStream(i, FileMode.OpenOrCreate);
byte[] bs = new byte[fs.Length];
fs.Read(bs, 0, bs.Length);
fs.Close();
FileInfo info = new FileInfo(i);
insert(info.Name, info.Extension, bs);
}
}
private void insert( string FileName,string FileName2,byte[] data )
{
string str = "Server=.;Database=Files; Uid=sa;Pwd=111111";
string sql = "insert into TFiles(Name,Tname,Data) values(@name,@TName,@Data)";
SqlConnection conn = new SqlConnection(str);
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter p1 = new SqlParameter("@name", FileName);
cmd.Parameters.Add(p1);
SqlParameter p2 = new SqlParameter("@TName", FileName2);
cmd.Parameters.Add(p2);
SqlParameter p3 = new SqlParameter("@Data", data);
cmd.Parameters.Add(p3);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
}