现在的位置: 首页 > 综合 > 正文

c#如何讀取txt數據,然後保存倒sql–zt

2012年03月19日 ⁄ 综合 ⁄ 共 3780字 ⁄ 字号 评论关闭
net_lover:
ODBC   Driver   for   Text    
  oConn.Open   _  
          "Driver={Microsoft   Text   Driver   (*.txt;   *.csv)};"   &   _  
          "Dbq=c:\somepath\;"   &   _  
          "Extensions=asc,csv,tab,txt"    
  Then   specify   the   filename   in   the   SQL   statement:  
   
  oRs.Open   "Select   *   From   customer.csv",   _  
                    oConn,   adOpenStatic,   adLockReadOnly,   adCmdText  
  Note:   If   you   are   using   a   Tab   delimited   file,   then   make   sure   you   create   a   schema.ini   file,   and   include   the   "Format=TabDelimited"   option.    
   
  For   more   information,   see:     Text   File   Driver   Programming   Considerations  
   
  To   view   Microsoft   KB   articles   related   to   Microsoft   Text   Driver,   click   here

ODBC   Driver   for   Text    
  oConn.Open   _  
          "Driver={Microsoft   Text   Driver   (*.txt;   *.csv)};"   &   _  
          "Dbq=c:\somepath\;"   &   _  
          "Extensions=asc,csv,tab,txt"    
  Then   specify   the   filename   in   the   SQL   statement:  
   
  oRs.Open   "Select   *   From   customer.csv",   _  
                    oConn,   adOpenStatic,   adLockReadOnly,   adCmdText  
  Note:   If   you   are   using   a   Tab   delimited   file,   then   make   sure   you   create   a   schema.ini   file,   and   include   the   "Format=TabDelimited"   option.    
   
  For   more   information,   see:     Text   File   Driver   Programming   Considerations  
   
  You   can   also   open   a   Text   file   using   the   JET   OLE   DB   Provider    
   
  oConn.Open   "Provider=Microsoft.Jet.OLEDB.4.0;"   &   _    
                "Data   Source=c:\somepath\;"   &   _    
                "Extended   Properties=""text;HDR=Yes;FMT=Delimited"""  
  'Then   open   a   recordset   based   on   a   select   on   the   actual   file  
   
  oRs.Open   "Select   *   From   MyTextFile.txt",   oConn,   _  
                    adOpenStatic,   adLockReadOnly,   adCmdText     
   

http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q262/5/37.ASP&NoWebContent=1

1. Create a text file called textfile.txt that contains code similar to the following:
"F1","F2","F3"
"one", "two", "three"
"1a","2b", "3c"
1,2,3
					

2. Create a new Standard EXE in Visual Basic. Form1 is created by default.
3. On the Project menu, click to select References, and then select Microsoft Active Data Objects and Microsoft ADO Ext.2.x for DDL and Security.
4. Add two CommandButton controls to the form, and name them CmdOpen and CmdInsert.
5. Add the following code to the form. You need to change the paths to reflect locations on your machine.
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim PathtoTextFile As String
Dim PathtoMDB As String
Dim myarray() As Variant
Private Sub Form_Load()
  PathtoTextFile = "C:\PathtoTextFile\"
  PathtoMDB = "C:\PathtoMDB\"
  CmdOpen.Caption = "Open textfile and display field value"
  CmdInsert.Caption = "Insert textfile values into MDB"
End Sub
Private Sub CmdOpen_Click()
  '============================method 1=================================
  cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
          "Data Source=" & PathtoTextFile & ";" & _
          "Extended Properties=""text;HDR=YES;FMT=Delimited"""
  rs.Open "select * from TextFile.txt", _
          cn, adOpenStatic, adLockReadOnly, adCmdText
  '============================method 2=================================
  'cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
          "Data Source=" & PathtoMDB & "Northwind.mdb"
  'rs.Open "SELECT * FROM [Text;Database=" & PathtoTextFile & ";" & _
          "HDR=YES;FMT=Delimited].[textfile.txt]", _
          cn, adOpenStatic, adLockReadOnly, adCmdText
  '=====================================================================
  MsgBox rs(0)
  rs.Close
  cn.Close
End Sub
Private Sub CmdInsert_Click()
  Set Cat = New ADOX.Catalog
  Set objTable = New ADOX.Table
  cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
          "Data Source=" & PathtoMDB & "Northwind.mdb"
  'Open the Catalog
  Set Cat.ActiveConnection = cn
  'delete the table if it exists
  On Error Resume Next
  Cat.Tables.Delete "table1"
  'Create the table
  objTable.Name = "Table1"
  'Create and Append a new fields to the "table1" Columns Collection
  objTable.Columns.Append "F1", adWChar
  objTable.Columns.Append "F2", adWChar
  objTable.Columns.Append "F3", adWChar
  
  Cat.Tables.Append objTable
  
  'Insert into table1 the contents of textfile.txt
  cn.Execute "INSERT INTO Table1 SELECT * FROM " & _
             "[Text;Database=" & PathtoTextFile & ";HDR=YES].[TextFile.txt]"
  cn.Close
  MsgBox "Finished Inserting into MDB"
End Sub

抱歉!评论已关闭.