连续运行MySQL查询(在c#程序中)以查找新添加的记录
本文关键字:查找 新添加 添加 记录 MySQL 运行 查询 程序 连续 | 更新日期: 2023-09-27 17:52:59
我有一个用c#编写的程序,该程序接受MySQL查询的结果集,并将列值转换为webrequest中HTML字符串的一部分。它工作得很好,但我需要能够连续运行这个程序,只抓取以前没有抓取的记录,这样我就不会一遍又一遍地发送相同的数据。有办法做到这一点在MySQL/c# ?我正在使用的查询没有时间戳。
编辑:代码:
public class mysqlExporter
{
public mysqlExporter()
{
//Create MySQL connection
string SQLString;
SQLString = "my connection string";
MySqlConnection conn = new MySqlConnection(SQLString);
//Write Query
MySqlDataAdapter eQuery = new MySqlDataAdapter(@"SELECT * FROM whatever;", conn);
//Put SQL results into dataset
DataSet eExport = new DataSet();
eQuery.FillSchema(eExport, SchemaType.Source);
eQuery.Fill(eExport);
//Convert dataset to datatable
DataTable exportTable;
exportTable = eExport.Tables[0];
//Make a webrequest for each record
foreach (DataRow row in exportTable.Rows)
{
//Push each record out
WebRequest httpPost = WebRequest.Create("http://whatever.whatever/name="+row["name"]+"&phone="+row["phone"]);
httpPost.Method = "POST";
string postData = httpPost.ToString();
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
httpPost.ContentLength = postData.Length;
Stream dataStream = httpPost.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = httpPost.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
}
}
}
将索引/主键值(可能是自动递增的)与我们的结果一起传递给c#,然后在请求中传递最后接收到的索引,以便查询可以从下一个索引
读取。应该有一个主键,就这样做:D