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

來源:互聯網
上載者:User
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.ConnectionDim rs As New ADODB.RecordsetDim PathtoTextFile As StringDim PathtoMDB As StringDim myarray() As VariantPrivate 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 SubPrivate 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.CloseEnd SubPrivate 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.