一个文件监听程序,当有大量的文件进入时如何根据入库时间的不同来判定哪些是未入库的程序

dearacheng520 2011-07-20 05:43:56
该程序主要功能是监听一个文件夹 ,当文件夹中有新文件进入时就自动读取文件中的内容并将其存入到数据库。问题是,当关闭这个监听程序时怎样记录最后一个被读入内容并存入数据库的文件,然后当下次运行时这个监听程序时,怎么自动选择被监听文件夹中在监听程序未运行期间产生的文件,并自动读取其内容存入数据库。监听程序是这样写的:
void UsingFileSystemWatcher()
{
fileSystemWatcher1.Filter = "*.txt";
fileSystemWatcher1.Created += new FileSystemEventHandler(fileSystemWatcher1_Created);
}
private void btnStart_Click(object sender, EventArgs e)
{
fileSystemWatcher1.Path = folderBrowserDialog1.SelectedPath ;
fileSystemWatcher1.EnableRaisingEvents = true;
this.fileSystemWatcher1.Filter = "*.txt";
this.fileSystemWatcher1.EndInit();
}
public void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
{
if (e.ChangeType == WatcherChangeTypes.Created)
{
listBox1.Items.Add("日志文件:" + e.FullPath + "被创建");
string fullPath = e.FullPath;
string fileName = fullPath.Substring(fullPath.LastIndexOf("\\") + 1);
string fileFolder = fullPath.Substring(0, fullPath.LastIndexOf("\\") + 1);
string filePath = fileFolder.Substring(fileFolder.LastIndexOf('\\') + 1) + "\\" + fileName;
string file = fileFolder + fileName;
textBox1.Text = file;
string strcon = "persist Security Info = False;Initial Catalog=Audiomonitor;";
strcon += "Data Source = localhost;Integrated Security = SSPI;";
SqlConnection conn = new SqlConnection(strcon);
conn.Open();
try
{
SqlCommand com = new SqlCommand("BULK INSERT siteinformation FROM '" + file + "' WITH (FIELDTERMINATOR = ',',ROWTERMINATOR= '=')", conn);

com.ExecuteNonQuery();
MessageBox.Show("数据导入成功");
}
catch (SqlException SQLexc)
{
MessageBox.Show("导入数据库时出错:" + SQLexc.ToString());
}
conn.Close();
}
思维有点混乱,希望高手解答一下
...全文
104 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dearacheng520 2011-07-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sp1234 的回复:]

是实际的项目吗?如果是,那么这个思路会死的很惨的。性能、可靠性、插入对数据过滤的能力、网络上通讯方面的考虑,等等都没有概念。如果是实际的项目,会死的很惨,赶紧找人重新设计。
[/Quote]
高手,我承认我没做过项目,刚刚接触做这么具体的东西,没什么经验。望你指点下啊
特别 2011-07-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sp1234 的回复:]

是实际的项目吗?如果是,那么这个思路会死的很惨的。性能、可靠性、插入对数据过滤的能力、网络上通讯方面的考虑,等等都没有概念。如果是实际的项目,会死的很惨,赶紧找人重新设计。
[/Quote]
老大提出这么多问题,能不能给出一个解决办法啊
求救
  • 打赏
  • 举报
回复
是实际的项目吗?如果是,那么这个思路会死的很惨的。性能、可靠性、插入对数据过滤的能力、网络上通讯方面的考虑,等等都没有概念。如果是实际的项目,会死的很惨,赶紧找人重新设计。
卧_槽 2011-07-20
  • 打赏
  • 举报
回复
写入数据库时,同时写入文件修改日期到数据库。
文件修改日期在fileinfo对象中可以获得。

其实这样也不保险。
最好是写一个文件,移走一个文件,这样就不会重复录入,也不会少录入了。

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧