使用以下程序,我试图将 ftp 服务器上文件夹中列出的所有文件名插入到 SQL 表中。
写入表格时出现错误
c# 无法访问已处置的对象。对象名称“system.net.sockets.networkstream”
reader.ReadToEnd()将整个流写入我想要的文件名。
namespace Examples.System.Net
{
public class WebRequestGetExample
{
public static void Main()
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://myftpaddress.com/0708/");
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential("vish10", "MyPasswd");
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
Console.WriteLine(reader.ReadToEnd());
SqlConnection sqlConnection1 = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DMSTG;Data Source=.");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = @"Insert into FTPfileList0708Folder
values('" + reader.ReadToEnd().ToString() + "')";
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
Console.WriteLine("Directory List Complete, status {0}", response.StatusDescription);
reader.Close();
response.Close();
}
}
}