MSDN:for example:using System; using System.IO;class Test {
public static void Main() { string path = @"c:\temp\MyTest.txt"; try { if (File.Exists(path)) { File.Delete(path); } using (StreamWriter sw = new StreamWriter(path)) { sw.WriteLine("This"); sw.WriteLine("is some text"); sw.WriteLine("to test"); sw.WriteLine("Reading"); } using (StreamReader sr = new StreamReader(path)) { while (sr.Peek() >= 0) { Console.WriteLine(sr.ReadLine()); } } } catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); } } }
还可以通过流的方式: public StreamReader ( Stream stream ) 还是以上面的代码举例,可以也行: using (FileStream fs = new FileStream(path, FileMode.Open)) { using (StreamReader sr = new StreamReader(fs)) { while (sr.Peek() >= 0) { Console.WriteLine(sr.ReadLine()); } } }
using System.IO;class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt"; try
{
if (File.Exists(path))
{
File.Delete(path);
} using (StreamWriter sw = new StreamWriter(path))
{
sw.WriteLine("This");
sw.WriteLine("is some text");
sw.WriteLine("to test");
sw.WriteLine("Reading");
} using (StreamReader sr = new StreamReader(path))
{
while (sr.Peek() >= 0)
{
Console.WriteLine(sr.ReadLine());
}
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
public StreamReader (
Stream stream
)
还是以上面的代码举例,可以也行:
using (FileStream fs = new FileStream(path, FileMode.Open))
{
using (StreamReader sr = new StreamReader(fs))
{ while (sr.Peek() >= 0)
{
Console.WriteLine(sr.ReadLine());
}
}
}
ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.NETDEVFX.v20.chs/cpref8/html/O_T_System_IO_StreamReader_.ctor.htm
System.IO.StreamReader strm=new System.IO.StreamReader(path);
System.Data.SqlClient.SqlConnection cn=new System.Data.SqlClient.SqlConnection(連接字符串);
System.Data.SqlClient.Command cmd=new Command("Insert Into Table1(字段) Values (@txt)",cn);
cmd.Parements.Add("@txt",strm.ReadToEnd());
cn.Open();
cmd.ExecuteNonQuery();
cn.close();
cmd.Dispose();
strm.close();如果要想作為含格式的數據源﹐就麻煩些﹐先用ODBC測試一下是否能夠導入成功﹐如果可以﹐則可以在C#中直接調用這個ODBC或手動創建這個ODBC的復制作為數據源,再使用Command導入吧。
========================================================
string str=My.Computer.FileSystem.ReadAllText(Server.MapPath("") & "\1.txt");
========================================================