16,554
社区成员
发帖
与我相关
我的任务
分享
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.IO
Public Class Form1
Dim MyDataSet As DataSet = New DataSet()
Dim Cn As New OleDb.OleDbConnection
Dim MyDataAdapterSysSet As OleDbDataAdapter
Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
LoadData()
End Sub
Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
Dim w As StreamWriter
w = File.CreateText(Cn.DataSource & "\test.txt")
Dim nCols As Integer
Dim nRows As Integer
Dim i As Integer, j As Integer
Dim strLine As String
nRows = DataGridView1.RowCount
nCols = DataGridView1.ColumnCount
strLine = DataGridView1.Columns(0).Name
For j = 1 To nCols - 1
strLine = strLine & getDelimited() & DataGridView1.Columns(j).Name
Next
w.WriteLine(strLine)
For i = 0 To nRows - 2
strLine = DataGridView1.Rows(i).Cells(0).Value.ToString
For j = 1 To nCols - 1
strLine = strLine & getDelimited() & DataGridView1.Rows(i).Cells(j).Value.ToString
Next
w.WriteLine(strLine)
Next
w.Close()
MsgBox("OK")
End Sub
Private Sub LoadData()
If Cn.State = ConnectionState.Open Then
Cn.Close()
End If
Cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\VS\VS2008\ODBCTextFile\TextFile\;Extended Properties='text;HDR=Yes;FMT=Delimited';"
Cn.Open()
Dim SqlStr As String
SqlStr = "select * from test.txt"
MyDataSet.Clear()
DataGridView1.DataSource = Nothing
MyDataAdapterSysSet = New OleDbDataAdapter(SqlStr, Cn)
MyDataAdapterSysSet.SelectCommand.CommandText = SqlStr
MyDataAdapterSysSet.Fill(MyDataSet, "tabText")
DataGridView1.DataSource = MyDataSet.Tables("tabText")
End Sub
Private Function getDelimited() As String
Dim key As Microsoft.Win32.RegistryKey
key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Jet\4.0\Engines\Text")
Dim value As String = CType(key.GetValue("Format"), String)
If value = "TabDelimited" Then
getDelimited = vbTab
Else
getDelimited = Replace(Replace(value, "Delimited(", ""), ")", "")
End If
End Function
End Class
Imports System
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Dim MyDataSet As DataSet = New DataSet()
Dim Cn As New OleDb.OleDbConnection
Dim MyDataAdapterSysSet As OleDbDataAdapter
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Cn.State = ConnectionState.Open Then
Cn.Close()
End If
Cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\VS\VS2008\ODBCTextFile\TextFile\;Extended Properties='text;HDR=Yes;FMT=Delimited';"
Cn.Open()
Dim SqlStr As String
SqlStr = "select * from test.txt"
MyDataAdapterSysSet = New OleDbDataAdapter(SqlStr, Cn)
MyDataAdapterSysSet.SelectCommand.CommandText = SqlStr
MyDataAdapterSysSet.Fill(MyDataSet, "tabText") '此名可任取
DataGridView1.DataSource = MyDataSet.Tables("tabText")
End Sub
End Class
Option Explicit
Dim Cn As New ADODB.Connection
Dim Rst As New ADODB.Recordset
'The delimiter can be specified in the registry at the following location:
'HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Jet \ 4.0 \ Engines \ Text
'"Format" = "TabDelimited" 用Tab符作分隔符
'or
'"Format" = "Delimited(;)" 用指定的符号作分隔符
Private Sub Command1_Click()
Cn.ConnectionString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=F:\TEST\杂\各种资料的连接\File;Extensions=asc,csv,tab,txt;HDR=Yes;FMT=Delimited;"
Cn.Open
Dim Sql As String
Sql = "select * from test.txt"
Rst.CursorLocation = adUseClient
Rst.Open Sql, Cn, adOpenStatic, adLockPessimistic
Set DataGrid1.DataSource = Rst
End Sub