将图像插入SQL Server的代码

时间:2015-11-27 17:26:01

标签: c# sql-server visual-studio

我想从我的Windows窗体应用程序中将图像插入SQL Server数据库。

这个问题看起来像是在试图询问我想要找到什么,但它已经关闭了:

Insert image into SQL Server

1 个答案:

答案 0 :(得分:1)

以下是我过去常用的代码。

通过在Microsoft Management Studio中查看数据库的设计,根据需要修改此代码:

mms

public static void InsertImage(int inventoryID, int businessID, FileInfo file, string sqlConnection)
{
    var list = new List<byte>();
    using (var stream = file.Open(FileMode.Open))
    {
        var data = new byte[stream.Length];
        stream.Read(data, 0, data.Length);
        list.AddRange(data);
    }
    var bmp = System.Drawing.Image.FromFile(file.FullName, true);
    using (var conn = new SqlConnection(sqlConnection))
    {
        conn.Open();
        var imageId = -1;
        var sqlSelect = "SELECT [ImageId] FROM [dbo].[ImageTable] WHERE [InventoryId]=@InventoryId;";
        using (var cmd = new SqlCommand(sqlSelect, conn))
        {
            cmd.Parameters.Add("@InventoryId", System.Data.SqlDbType.Int).Value = inventoryID;
            using (var r = cmd.ExecuteReader())
            {
                if (r.Read())
                {
                    var o = r["ImageId"];
                    if ((o != null) && (o != DBNull.Value))
                    {
                        imageId = (int)o;
                    }
                }
            }
        }
        if (imageId == -1)
        {
            var sqlCmd = "INSERT INTO [dbo].[ImageTable] " +
                "([InventoryId], [ImageFileName], [ImageSize], [ImageWidth], [ImageHeight], [ImageBytes]) " +
                "VALUES " +
                "(@InventoryId,  @ImageFileName,  @ImageSize,  @ImageWidth,  @ImageHeight,  @ImageBytes); ";
            using (var cmd = new SqlCommand(sqlCmd, conn))
            {
                cmd.Parameters.Add("@InventoryId", System.Data.SqlDbType.Int).Value = inventoryID;
                cmd.Parameters.Add("@ImageFileName", System.Data.SqlDbType.VarChar, 255).Value = file.Name;
                cmd.Parameters.Add("@ImageSize", System.Data.SqlDbType.Int).Value = list.Count;
                cmd.Parameters.Add("@ImageWidth", System.Data.SqlDbType.SmallInt).Value = bmp.Width;
                cmd.Parameters.Add("@ImageHeight", System.Data.SqlDbType.SmallInt).Value = bmp.Height;
                cmd.Parameters.Add("@ImageBytes", System.Data.SqlDbType.VarBinary, -1).Value = list.ToArray();
                cmd.ExecuteNonQuery();
            }
        }
    }
}

为了运行/测试代码,我创建了这个帮助方法:

public static string[] GetImages(string fullFolderPath, string searchPattern)
{
    var list = new List<String>();
    if (Directory.Exists(fullFolderPath))
    {
        if (String.IsNullOrEmpty(searchPattern))
        {
            searchPattern = "*.jpg";
        }
        var dir = new DirectoryInfo(fullFolderPath);
        var files = dir.GetFiles(searchPattern);
        for (int i = 0; i < files.Length; i++)
        {
            InsertImage(i + 1, 1, files[i], _sqlConnection);
            list.Add(files[i].FullName);
        }
    }
    return list.ToArray();
}

现在,从我的控制台应用程序运行它是一个简单的单一调用:

static void Main(string[] args)
{
    var list = GetImages(@"C:\inetpub\wwwroot\Ads", "*.jpg");
}