插入图片文件到MySql和MSSql数据库

使用C#将指定文件夹以及子文件夹中的图片文件插入到MSSql数据库:

using System.Data.SqlClient;
using System;
using System.IO;

public class Photo
{
    public static void Main(string[]args)
    {
        byte[]byts=null;
        FileStream fs=null;
        string []files=Directory.GetFiles("./images","*.*",SearchOption.AllDirectories);
        string connstr="data source=localhost;initial catalog=test;user id=sa;password=sa1234;";
        SqlConnection connection=new SqlConnection(connstr);
        connection.Open();
        foreach(string file in files)
        {
            string ext=Path.GetExtension(file);
            string name=Path.GetFileNameWithoutExtension(file);
            fs=new FileStream(file,FileMode.Open);
            int size=(int)fs.Length;
            byts=new byte[size];
            fs.Read(byts,0,size);
            SqlCommand command=connection.CreateCommand();
            command.CommandText="select count(*) from photo where name='"+name+"'";
            if(Convert.ToInt32(command.ExecuteScalar())==0)
            {
                command.CommandText="insert into Photo(photo,name,size,extension) values(@photo,@name,@size,@extension)";
                command.Parameters.Add(new SqlParameter("photo",byts));
                command.Parameters.Add(new SqlParameter("name",name));
                command.Parameters.Add(new SqlParameter("size",size));
                command.Parameters.Add(new SqlParameter("extension",ext));
                int effect=command.ExecuteNonQuery();
                if(effect>0)
                {
                    Console.WriteLine(file+"插入成功!");
                }
                else
                {
                    Console.WriteLine(file+"插入失败!");
                }
            }
        }
        connection.Close();
        fs.Close();
    }
}

使用C#将指定文件夹以及子文件夹中的图片文件插入到MySql数据库:

这里将使用到MySql.Data.dll文件使用命令行引入该程序及文件即可:

csc /r:mysql.data.dll photo.cs

View Code
using MySql.Data.MySqlClient;
using System;
using System.Text;
using System.IO;

public class Photo
{
    public static void Main(string[]args)
    {
        byte[]byts=null;
        FileStream fs=null;
        string []files=Directory.GetFiles("./images","*.*",SearchOption.AllDirectories);
        string connstr="oldsyntax=true;Database=Photo;Data Source=127.0.0.1;User Id=root;Password=sa1234;pooling=false;CharSet=utf8;port=3306";
        MySqlConnection connection=new MySqlConnection(connstr);
        connection.Open();
        foreach(string file in files)
        {
            string ext=Path.GetExtension(file);
            string name=Path.GetFileNameWithoutExtension(file);
            fs=new FileStream(file,FileMode.Open);
            int size=(int)fs.Length;
            byts=new byte[size];
            fs.Read(byts,0,size);
            string sql="select count(*) from photo where name='"+name+"'";
            MySqlCommand command=new MySqlCommand(sql,connection);
            if(Convert.ToInt32(command.ExecuteScalar())==0)
            {
                sql="insert into Photo(photo,name,size,extension) values(@photo,@name,@size,@extension)";
                command=new MySqlCommand(sql,connection);
                command.Parameters.Add(new MySqlParameter("@photo",MySqlDbType.MediumBlob));
                command.Parameters.Add(new MySqlParameter("@name",MySqlDbType.VarChar));
                command.Parameters.Add(new MySqlParameter("@size",MySqlDbType.Int32));
                command.Parameters.Add(new MySqlParameter("@extension",MySqlDbType.VarChar));
                command.Parameters[0].Value=byts;
                command.Parameters[1].Value=name;
                command.Parameters[2].Value=size;
                command.Parameters[3].Value=ext;
                int effect=command.ExecuteNonQuery();
                if(effect>0)
                {
                    Console.WriteLine(file+"插入成功!");
                }
                else
                {
                    Console.WriteLine(file+"插入失败!");
                }
            }
        }
        connection.Close();
        fs.Close();
    }
}
posted @ 2012-06-22 21:29  痞子笑  阅读(1371)  评论(0编辑  收藏  举报