将图像从Excel工作表导入SQL Server表C#

普拉布·伊斯瓦尔

在这里,我想SQL Server使用C#代码将Excel工作表中的所有数据插入表中

我有这样的数据的Excel工作表

ID    Name    Designation    ProfilePicture
--------------------------------------------
1    ABC      Manager        C:\Pictures\1.jpg
2    DEF      Asst.Manager   C:\Pictures\2.jpg

而且我有将数据插入表中的代码

String filePath = filePathText.Text;
String fileExtension = "Excel 12.0";
if (filePath != null)
{
    String xlsConnection = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=" + "\"" + fileExtension + ";HDR=YES;\"";

    String sqlConnection = "Your Connection String";

    //Connection to Excel work book
    OleDbConnection xlsConnectionString = new OleDbConnection(xlsConnection);

    //Fetch data from Excel
    OleDbCommand cmd = new OleDbCommand("Select [ID],[Name],[Designation],[ProfilePicture] from [Sheet1$]", xlsConnectionString);

    xlsConnectionString.Open();

    OleDbDataReader dReader;
    dReader = cmd.ExecuteReader();
    SqlBulkCopy sqlBulk = new SqlBulkCopy(sqlConnection);

    //Destination table name
    sqlBulk.DestinationTableName = "EXCEL_DATA";

    sqlBulk.WriteToServer(dReader);

    xlsConnectionString.Close();
}

如果我单击按钮,则这段代码将运行。

我的问题是,如何从Excel工作表上传图片(图纸具有图片的路径)。SQL Server表。我想使用Excel Sheet中提供的Imagepath来获取图片并将其存储为varbinary(MAX)SQL Server

普拉布·伊斯瓦尔

感谢那些真正致力于发布答案的人。终于,我自己找到了解决问题的办法。

这是SQL Server通过使用中提供的路径帮助将图像插入的代码Excel sheet

    private void insert_Click(object sender, EventArgs e)
    {

        UInt64 ID = 0;
        String Name = String.Empty;
        String Designation = String.Empty;
        String ProfilePicture = String.Empty;

        String filePath = filePathText.Text;

        Excel.Application xlApp = null;
        Excel.Workbook xlWorkbook = null;
        Excel._Worksheet xlWorksheet = null;
        Excel.Range xlRange = null;

        String sqlConnectionString = "Your Connection String goes here";

        String insertRecord = "INSERT_USER_RECORDS";

        SqlConnection sqlConnection = new SqlConnection(sqlConnectionString);
        SqlCommand sqlCommand = new SqlCommand(insertRecord, sqlConnection);
        sqlCommand.CommandType = CommandType.StoredProcedure;

        sqlConnection.Open();

        if (filePath != null)
        {

            try
            {

                xlApp = new Excel.Application();
                xlWorkbook = xlApp.Workbooks.Open(filePath);
                xlWorksheet = (Excel._Worksheet)xlWorkbook.Sheets[1];
                xlRange = xlWorksheet.UsedRange;

                int rowCount = xlRange.Rows.Count;
                int colCount = xlRange.Columns.Count;

                for (int row = 1; row <= rowCount; row++)
                {
                    for (int col = 1; col <= colCount; col++)
                    {
                        MessageBox.Show((xlRange.Cells[row, col] as Microsoft.Office.Interop.Excel.Range).Value2.ToString());

                        // Check xlRange for Every run. And assign values to local variables. Here I just show the values using MsgBox

                        // If you get the Path of Image then call the function to Convert Image into byte

                        // Convert Image to Byte Function definition.

                        /* System.IO.FileStream fs = new System.IO.FileStream(ProfilePicture, System.IO.FileMode.Open);
                           Byte[] imageAsBytes = new Byte[fs.Length];
                           fs.Read(imageAsBytes, 0, imageAsBytes.Length);
                           fs.Close();
                           return imageAsBytes; */

                    }

                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.Add("@Name", SqlDbType.NVarChar).Value = FirstName;
                    sqlCommand.Parameters.Add("@Designation", SqlDbType.NVarChar).Value = LastName;
                    sqlCommand.Parameters.Add("@ProfilePicture", SqlDbType.VarBinary).Value = imageAsBytes;
                    sqlCommand.Parameters.Add("@ID", SqlDbType.BigInt).Value = ID;

                    sqlCommand.ExecuteNonQuery();
                }


                MessageBox.Show(Path.GetFileName(filePath) + "is Successfully imported to SQL Server", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                //Release All objects and close the Connection to prevent the Excel file from lock.

                sqlConnection.Close();

                GC.Collect();
                GC.WaitForPendingFinalizers();

                Marshal.FinalReleaseComObject(xlRange);
                Marshal.FinalReleaseComObject(xlWorksheet);

                xlWorkbook.Close(Type.Missing, Type.Missing, Type.Missing);
                Marshal.FinalReleaseComObject(xlWorkbook);

                xlApp.Quit();
                Marshal.FinalReleaseComObject(xlApp);

            }
        }
        else
        {
            MessageBox.Show("Please Select the Valid file to import");
        }
    }

这段代码可以正常工作,可以帮助我将图像从中插入SQL数据库Excel无论Excel文件的版本。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从Excel将数据导入SQL Server表

来自分类Dev

将工作表从Excel导入C#中的SQL时,它会在DateTime列中浪费时间

来自分类Dev

从SQL Server表将数据插入Excel工作表的列

来自分类Dev

需要阅读Excel工作表C#并仅将3列导入mySQL

来自分类Dev

导入Excel工作表时出现C#问题

来自分类Dev

锁定SQL Server表的C#方法

来自分类Dev

C#更新SQL Server表

来自分类Dev

将用户友好的Excel工作表导入具有许多外键的SQL Server表中

来自分类Dev

将数据框导出/导入为Excel工作表

来自分类Dev

将Excel工作表导入Postgresql数据库

来自分类Dev

将多个 Excel 范围/工作表导入到 Powerpoint

来自分类Dev

从Excel工作表导入数据

来自分类Dev

从特定行和列开始的 Excel 工作表数据导入到数据网格,然后导入 SQL Server 表、WPF

来自分类Dev

您如何使用ASP.NET C#将Excel数据导入SQL Server,而表数据不在第一行开始?

来自分类Dev

使用C#将数据更新到Excel工作表

来自分类Dev

C# 将内容添加到 Excel 工作表

来自分类Dev

如何将Excel工作表导出为图像?

来自分类Dev

从Excel工作表下载产品的URL图像,以将它们作为图像插入SQL Server中

来自分类Dev

VBA - 将 SQL 表中的所有行导入 Excel

来自分类Dev

使用查询将SQL Server 2008表导出到Excel工作表

来自分类Dev

将图像从文件夹导入到SQL Server表中

来自分类Dev

复制工作表Excel VSTO C#

来自分类Dev

用C#填充Excel工作表

来自分类Dev

在C#中使用Excel工作表

来自分类Dev

Excel工作表中的C#变量

来自分类Dev

将Excel工作表数据导入Windows应用商店应用中的Sqlite表

来自分类Dev

如何使用 VBA 将所有 Excel 工作簿工作表导入 Access。

来自分类Dev

从C#,如何在SQL Server中查找表的列名?

来自分类Dev

如何从C#修改SQL Server表上的约束?

Related 相关文章

  1. 1

    从Excel将数据导入SQL Server表

  2. 2

    将工作表从Excel导入C#中的SQL时,它会在DateTime列中浪费时间

  3. 3

    从SQL Server表将数据插入Excel工作表的列

  4. 4

    需要阅读Excel工作表C#并仅将3列导入mySQL

  5. 5

    导入Excel工作表时出现C#问题

  6. 6

    锁定SQL Server表的C#方法

  7. 7

    C#更新SQL Server表

  8. 8

    将用户友好的Excel工作表导入具有许多外键的SQL Server表中

  9. 9

    将数据框导出/导入为Excel工作表

  10. 10

    将Excel工作表导入Postgresql数据库

  11. 11

    将多个 Excel 范围/工作表导入到 Powerpoint

  12. 12

    从Excel工作表导入数据

  13. 13

    从特定行和列开始的 Excel 工作表数据导入到数据网格,然后导入 SQL Server 表、WPF

  14. 14

    您如何使用ASP.NET C#将Excel数据导入SQL Server,而表数据不在第一行开始?

  15. 15

    使用C#将数据更新到Excel工作表

  16. 16

    C# 将内容添加到 Excel 工作表

  17. 17

    如何将Excel工作表导出为图像?

  18. 18

    从Excel工作表下载产品的URL图像,以将它们作为图像插入SQL Server中

  19. 19

    VBA - 将 SQL 表中的所有行导入 Excel

  20. 20

    使用查询将SQL Server 2008表导出到Excel工作表

  21. 21

    将图像从文件夹导入到SQL Server表中

  22. 22

    复制工作表Excel VSTO C#

  23. 23

    用C#填充Excel工作表

  24. 24

    在C#中使用Excel工作表

  25. 25

    Excel工作表中的C#变量

  26. 26

    将Excel工作表数据导入Windows应用商店应用中的Sqlite表

  27. 27

    如何使用 VBA 将所有 Excel 工作簿工作表导入 Access。

  28. 28

    从C#,如何在SQL Server中查找表的列名?

  29. 29

    如何从C#修改SQL Server表上的约束?

热门标签

归档