VB Express 2008:将导入的datagrid保存到mdb [英] VB Express 2008 : Saving imported datagrid to mdb

查看:110
本文介绍了VB Express 2008:将导入的datagrid保存到mdb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我不是那么有经验.我正在使用VB Express2008.我有问题.我正在使用以下代码在datagrid中导入csv文件:

Hi All
I am not that experienced. I am using VB Express 2008. I have a problem. I am importing a csv file in a datagrid with the following code :

Private Sub btnFindFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindFile.Click
        Dim OpenFileDialog As New OpenFileDialog
        OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
        OpenFileDialog.Filter = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*"
        OpenFileDialog.Title = "Select A CSV File"

        If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
            Dim FileName As String = IO.Path.GetFileName(OpenFileDialog.FileName)
            Dim Path As String = IO.Path.GetDirectoryName(OpenFileDialog.FileName)
            Dim connString As String = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" _
                & Path & ";Extended Properties=""Text;HDR=No;FMT=Delimited"""
            Dim conn As New Odbc.OdbcConnection(connString)
            Dim da As New Odbc.OdbcDataAdapter("SELECT * FROM [" & FileName & "]", conn) 
            Dim dt As New DataTable
            da.Fill(dt)
            ItemsDataGridView.DataSource = dt
        End If
End Sub



该代码运行正常,并在我的datagrid中显示了csv文件的所有内容.我的问题如下:我无法弄清楚如何将datagridview中的导入信息保存到我的mdb数据库中. 我尝试了以下方法,但是没有运气:



The code is working perfectly, and display all the content of the csv file in my datagrid. My problem is the following: I cannot figure out how to save the imported info in the datagridview, to my mdb database.
I have tried the following, but with no luck :

Me.ItemsTableAdapter.Fill(Me.ItemsDataSet1.Items)
        Me.Validate()
        Me.ItemsBindingSource.EndEdit()



任何帮助,将不胜感激.
也许我应该提一下:我正在尝试保存到现有的Access mdb数据库中,该表中的列比导入的csv文件更多.



Any help would be appreciated.
Maybe I should just mention: I am trying to save into an existing Access mdb database, where the table has more columns than the imported csv file.

推荐答案

您可以使用File.ReadAllLines获取代表文件中文本的字符串数组,然后从那里将其插入数据库.
You can use File.ReadAllLines to get an array of strings representing the text in the file, then insert that into your database from there.


我正在导入绑定的数据网格.我的代码将信息发送到正确的列.第一列是我的主键.导入后,主键列中的行为空白.这是完美的,因为当我设法将导入的信息保存到Access数据库中的现有信息时,我希望从数据库的最后一行开始对主键进行编号.
I am importing into a bound datagrid. My code is sending the info to the correct columns. The first column is my primary key. After import, the rows in the primary key column are blank. This is perfect, as when I manage to save the imported info to the existing info to the Access Database, I want the numbering of the primary key to carry on from the last row in the database.


这篇关于VB Express 2008:将导入的datagrid保存到mdb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆