将excel文件保存在数据库VB.NET中 [英] Save the excel file in a database VB.NET
本文介绍了将excel文件保存在数据库VB.NET中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个与数据库有关的问题
如何在Access数据库中保存列表框?
如何在Visual Basic Access数据库中保存文件。网格视图是编辑和查看数据的能力。
来自我们的感恩源
< b>我尝试过:
Hi, I have two questions in relation to database
How to save a list box in the Access database?
How to save a file in Visual Basic Access database. Grid View is the ability to edit and view the data.
Let's'm grateful source
What I have tried:
How to save a list box in the Access database
推荐答案
我不太明白将列表框保存到Access数据库的问题,但是将Excel文件保存到数据库中是什么,这是另一回事。 br />
Excel文件毕竟是一系列字节,因此您读取字节,将数据放入INSERT或UPDATE语句的参数中,打开连接并保存数据。
简而言之,代码看起来像
I don't quite understand the question about saving a list box to Access database, but what comes to saving Excel file into a database, it's another thing.
The Excel file is after all a series of bytes so you read the bytes, place the data into a parameter for INSERT or UPDATE statement, open the connection and save the data.
In short the code could look something like
Dim fs As System.IO.FileStream
Dim sr As System.IO.StreamReader
fs = New System.IO.FileStream("c:\path\excelfile", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)
sr = New System.IO.StreamReader(fs)
Dim bytes(fs.Length - 1) As Byte
fs.Read(bytes, 0, fs.Length)
Try
Using connection As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=accessfilename")
Using command As System.Data.OleDb.OleDbCommand = New System.Data.OleDb.OleDbCommand("insert into tablename (columnname) values (?)", connection)
command.Parameters.Add("@bytes", System.Data.OleDb.OleDbType.Binary, fs.Length).Value = bytes
connection.Open()
command.ExecuteNonQuery()
connection.Close()
End Using
End Using
Catch exception As Exception
' error handling goes here
End Try
这篇关于将excel文件保存在数据库VB.NET中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文