怎样用SQL读取CSV文件
包括怎样写入DATASET
谁能给出代码啊

解决方案 »

  1.   

    public void WriteCsv(string strQuestion)
    {
    string strFile="";
    string path="";
    DataSet ds=GetDataSet(strQuestion);
    DataTable DT=ds.Tables[0];
    /*FileSetting*/
    strFile=strFile+"test.csv";
    path=Server.MapPath("SysLogUpdate/"+strFile);
    string[] strHead={"11","22"};
    System.IO.FileStream fs=new FileStream(path,System.IO.FileMode.Create,System.IO.FileAccess.Write);
    StreamWriter sw=new StreamWriter(fs,new System.Text.UnicodeEncoding());
    for(int i=0;i<strHead.Length;i++)
    {
    if(i==strHead.Length-1)
    {
    sw.Write(strHead[i]);
    }
    else
    {
    sw.Write(strHead[i]);
    sw.Write("\t");
    }
    }
    sw.WriteLine("");
    for(int i=0;i<DT.Rows.Count;i++)
    {
    sw.Write(ss.DelQuota(Convert.ToString(DT.Rows[i]["USERNAME"])));
    sw.Write("\t");
    sw.Write(ss.DelQuota(Convert.ToString(DT.Rows[i]["USERID"])));
    sw.Write("\t");
    sw.WriteLine("");
    }
    sw.Flush();
    sw.Close();
    }
      

  2.   

    string strPath = "c:\\test.csv"; 
    DataSet ds = new DataSet(); 
    try { 
     System.IO.File f; 
     if (f.Exists(strPath)) { 
       string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "c:\\" + ";Extended Properties=\"Text;HDR=No;FMT=Delimited\\\""; 
       OleDb.OleDbConnection conn = new OleDb.OleDbConnection(ConStr); 
       OleDb.OleDbDataAdapter da = new OleDb.OleDbDataAdapter("Select * from " + "test.csv", conn); 
       da.Fill(ds, "test.csv"); 
     } 
    } catch (Exception ex) { 
     MessageBox.Show(ex.ToString); 

    DataGrid1.DataSource = ds.Tables(0);
      

  3.   

    Public Sub OutputCsvToDataset(ByVal dataSource As DataSet, _
                                      ByVal dataMember As String, _
                                      ByVal filePath As String, _
                                      Optional ByVal separator As String = ",", _
                                      Optional ByVal headerLength As Integer = 0)        Dim fileReader As StreamReader
            Dim line As String = ""                                     
            fileReader = New StreamReader(filePath, Encoding.Default)        Try            
                For i As Integer = 0 To headerLength - 1
                    line = fileReader.ReadLine()
                    If line Is Nothing Then                     
                        Exit For                                
                    End If
                Next           
                dataSource.Tables(dataMember).Clear()           
                Dim colCount As Integer = dataSource.Tables(dataMember).Columns.Count            If colCount > 0 Then                            
                   
                    dataSource.Tables(dataMember).BeginLoadData()                Try                    
                        Do Until line Is Nothing
                            line = fileReader.ReadLine()                                If Not (line Is Nothing) Then      
                                Dim row As DataRow = dataSource.Tables(dataMember).NewRow
                                Dim startPos As Integer = 0    
                                Dim colIndex As Integer = 0                                 Do While startPos < line.Length And startPos <> -1 And colIndex < colCount
                                    Dim delimiter As String                                     
                                    If line.Chars(startPos) = """" Then
                                        
                                        delimiter = """" & separator
                                        
                                        startPos = startPos + 1
                                    Else
                                        
                                        delimiter = separator
                                    End If                                Dim endPos As Integer = line.IndexOf(delimiter, startPos)
                                    If endPos <> -1 Then
                                       
                                        row(colIndex) = getColValueByType(line.Substring(startPos, endPos - startPos), _
                                                                          dataSource.Tables(dataMember).Columns(colIndex).DataType.ToString)
                                        startPos = endPos + delimiter.Length
                                    Else
                                        
                                        If delimiter.Chars(0) = """" AndAlso line.Chars(line.Length - 1) = """" Then
                                           
                                            row(colIndex) = getColValueByType(line.Substring(startPos, line.Length - startPos - 1), _
                                                                              dataSource.Tables(dataMember).Columns(colIndex).DataType.ToString)
                                        Else
                                            
                                            row(colIndex) = getColValueByType(line.Substring(startPos), _
                                                                              dataSource.Tables(dataMember).Columns(colIndex).DataType.ToString)
                                        End If
                                        startPos = -1
                                    End If                                
                                    colIndex = colIndex + 1
                                Loop
                                dataSource.Tables(dataMember).Rows.Add(row)
                            End If
                        Loop                Finally                   
                        dataSource.Tables(dataMember).EndLoadData()                End Try            End If        Finally            fileReader.Close()        End Try    End Sub
      

  4.   

    Public Sub OutputCsvToDataset(ByVal dataSource As DataSet, _
                                      ByVal dataMember As String, _
                                      ByVal filePath As String, _
                                      Optional ByVal separator As String = ",", _
                                      Optional ByVal headerLength As Integer = 0)        Dim fileReader As StreamReader
            Dim line As String = ""                                     
            fileReader = New StreamReader(filePath, Encoding.Default)        Try            
                For i As Integer = 0 To headerLength - 1
                    line = fileReader.ReadLine()
                    If line Is Nothing Then                     
                        Exit For                                
                    End If
                Next           
                dataSource.Tables(dataMember).Clear()           
                Dim colCount As Integer = dataSource.Tables(dataMember).Columns.Count            If colCount > 0 Then                            
                   
                    dataSource.Tables(dataMember).BeginLoadData()                Try                    
                        Do Until line Is Nothing
                            line = fileReader.ReadLine()                                If Not (line Is Nothing) Then      
                                Dim row As DataRow = dataSource.Tables(dataMember).NewRow
                                Dim startPos As Integer = 0    
                                Dim colIndex As Integer = 0                                 Do While startPos < line.Length And startPos <> -1 And colIndex < colCount
                                    Dim delimiter As String                                     
                                    If line.Chars(startPos) = """" Then
                                        
                                        delimiter = """" & separator
                                        
                                        startPos = startPos + 1
                                    Else
                                        
                                        delimiter = separator
                                    End If                                Dim endPos As Integer = line.IndexOf(delimiter, startPos)
                                    If endPos <> -1 Then
                                       
                                        row(colIndex) = getColValueByType(line.Substring(startPos, endPos - startPos), _
                                                                          dataSource.Tables(dataMember).Columns(colIndex).DataType.ToString)
                                        startPos = endPos + delimiter.Length
                                    Else
                                        
                                        If delimiter.Chars(0) = """" AndAlso line.Chars(line.Length - 1) = """" Then
                                           
                                            row(colIndex) = getColValueByType(line.Substring(startPos, line.Length - startPos - 1), _
                                                                              dataSource.Tables(dataMember).Columns(colIndex).DataType.ToString)
                                        Else
                                            
                                            row(colIndex) = getColValueByType(line.Substring(startPos), _
                                                                              dataSource.Tables(dataMember).Columns(colIndex).DataType.ToString)
                                        End If
                                        startPos = -1
                                    End If                                
                                    colIndex = colIndex + 1
                                Loop
                                dataSource.Tables(dataMember).Rows.Add(row)
                            End If
                        Loop                Finally                   
                        dataSource.Tables(dataMember).EndLoadData()                End Try            End If        Finally            fileReader.Close()        End Try    End Sub
      

  5.   

    csv看成数据源string strPath = "c:\\test.csv"; 
    DataSet ds = new DataSet(); 
    try { 
     System.IO.File f; 
     if (f.Exists(strPath)) { 
       string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "c:\\" + ";Extended Properties=\"Text;HDR=No;FMT=Delimited\\\""; 
       OleDb.OleDbConnection conn = new OleDb.OleDbConnection(ConStr); 
       OleDb.OleDbDataAdapter da = new OleDb.OleDbDataAdapter("Select * from " + "test.csv", conn); 
       da.Fill(ds, "test.csv"); 
     } 
    } catch (Exception ex) { 
     MessageBox.Show(ex.ToString); 

    DataGrid1.DataSource = ds.Tables[0];
      

  6.   

    string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "c:\\" + ";Extended Properties=\"Text;HDR=No;FMT=Delimited\\\""; 
    这个语句能解释一下吗
      

  7.   

    net_lover(孟子E章) 你给的代码不对,运行说对象不是RECORDSET