日期:2014-05-19  浏览次数:21002 次

c# 读txt文件的内容到程序中
要将.txt文件中的内容读出赋给SqlDataSource1.ConnectionString  
用什么代码可以实现.要用到什么类.
请详细一点.本人初学者.完整的代码便于学习仿照.
谢谢..万分感谢!!

------解决方案--------------------
读写文件:

public ReadFile(string strFileName)
{
StreamReader sr = new StreamReader(strFileName,System.Text.Encoding.Default);
while (sr.Peek() > = 0)
{
Console.WriteLine(sr.ReadLine());
}
sr.Close();
}

Public Shared Sub WriteFile(ByVal strLog As String, ByVal page As System.Web.UI.Page)
If Directory.Exists(page.Server.MapPath( "LogFolder ")) = False Then
Directory.CreateDirectory(page.Server.MapPath( "LogFolder "))
End If
Dim pw As StreamWriter = New StreamWriter(page.Server.MapPath( ". ") & "\LogFolder\log.txt ", True, System.Text.Encoding.UTF8)
pw.WriteLine(strLog)
pw.Flush()
pw.Close()
End Sub
------解决方案--------------------
你们老师有没有搞错阿
如果是要你们练习文件读写,这样没什么问题。如果是要练习数据连接,那就是脑袋进水了。
练习文件读写 文件读写类可以参考。
练习数据连接 建议使用config文件。
例如:
<configuration>
<appSettings/>
<connectionStrings>
<add name= "xxxxName " connectionString= "Data Source=xxx;User ID=xxxID;Password=xxxPW " providerName= "Oracle.DataAccess.Client "/>
</connectionStrings>
</configuration>
这是个oracle连接片断,其他数据类似。
读取connectionString
ConfigurationManager.ConnectionStrings[ "xxxxName "].ConnectionString