110,538
社区成员
发帖
与我相关
我的任务
分享
public static void Main(string[] args)
{
QueryCSV();
Console.ReadLine();
}
public static void QueryCSV()
{
//文件路径
string tableName = "file.csv";
string filePath = AppDomain.CurrentDomain.BaseDirectory;
string pContent = string.Empty;
OleDbConnection oledbConn = new OleDbConnection();
OleDbCommand oledbCmd = new OleDbCommand();
OleDbDataReader dataReader;
try
{
//两种连接方式皆可
//string strConnOledb = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=";
string strConnOledb="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
strConnOledb += filePath;
strConnOledb += ";Extended Properties='Text;HDR=Yes;IMEX=1;'";
oledbConn.ConnectionString = strConnOledb;
oledbConn.Open();
StringBuilder commandText = new StringBuilder("SELECT ");
commandText.AppendFormat("* From {0}", tableName);
oledbCmd.Connection = oledbConn;
oledbCmd.CommandText = commandText.ToString();
dataReader = oledbCmd.ExecuteReader();
while (dataReader.Read())
{
pContent = Convert.ToString(dataReader["content"]);
}
dataReader.Close();
}
catch (System.Exception ex)
{
oledbConn.Close();
}
finally
{
oledbConn.Close();
}
Console.WriteLine(pContent);
}
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frm As New OpenFileDialog
With frm
.ShowDialog()
If .FileName <> "" Then
Dim strCon As String = ""
If .FileName.EndsWith(".xlsx") Then
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & .FileName & ";Extended Properties=""Excel 12.0 Xml;HDR=Yes"";"
Else
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & .FileName & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
End If
Dim strSQL As String = "SELECT * FROM [Sheet1$]"
Using adp As New Data.OleDb.OleDbDataAdapter(strSQL, strCon), dtbRet As New DataTable
adp.Fill(dtbRet)
TextBox1.Text = dtbRet.Rows(0)(0)
End Using
End If
End With
End Sub