نوشته شده توسط
parvizwpf
کد رو بزارید لطفا
کد قسمتی از برنامه که مربوط به مشکل میشه:
private void button4_Click(object sender, EventArgs e)
{
List<string> ips = ipCollector();
SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=sitelog;Integrated Security=True;");
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT ip,rday,rhour,rminute,userAgent FROM requests WHERE ip=@p1";
cmd.Parameters.Add("@p1",SqlDbType.NVarChar);
for (int i = 0; i < ips.Count; i++)
{
List<List<string>> iprequests = new List<List<string>>();
cmd.Parameters["@p1"].Value = ips[i];
con.Open();
SqlDataReader rd = cmd.ExecuteReader();
while (rd.Read())
{
List<string> request = new List<string>();
request.Add(rd["ip"].ToString());
request.Add(rd["rday"].ToString());
request.Add(rd["rhour"].ToString());
request.Add(rd["rminute"].ToString());
request.Add(rd["userAgent"].ToString());
iprequests.Add(request);
}
con.Close();
agentFrequency(iprequests);
}
}
private void agentFrequency(List<List<string>> temp)
{
string agentFrequence = null;
SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=sitelog;Integrated Security=True;");
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "INSERT INTO UAFrequency VALUES(@p1,@p2)";
cmd.Parameters.Add("@p1",SqlDbType.VarChar);
cmd.Parameters.Add("@p2",SqlDbType.NText);
List<int> time = new List<int>();
time.Add(int.Parse(temp[0][1]));
time.Add(int.Parse(temp[0][2]));
time.Add(int.Parse(temp[0][3]));
List<int> upRange = timeIterator(time);
bool finish=false;
int ipNum = 0;
while(!finish)
{
List<string> userAgent = new List<string>();
int agentCounter = 0;
for (int j = ipNum; j < temp.Count; j++)
{
if (int.Parse(temp[j][1]) == upRange[0])
{
if (int.Parse(temp[j][2]) == upRange[1])
{
if (int.Parse(temp[j][3]) <= upRange[2])
{
ipNum++;
if (!(agentController(userAgent, temp[j][4])))
{
agentCounter++;
userAgent.Add(temp[j][4]);
}
}
}
else if (int.Parse(temp[j][2]) < upRange[1])
{
if (int.Parse(temp[j][3]) > upRange[2])
{
ipNum++;
if (!(agentController(userAgent, temp[j][4])))
{
agentCounter++;
userAgent.Add(temp[j][4]);
}
}
}
}
else if (int.Parse(temp[j][1]) < upRange[0])
{
if (int.Parse(temp[j][2]) > upRange[1])
{
if (int.Parse(temp[j][3]) > upRange[2])
{
ipNum++;
if (!(agentController(userAgent, temp[j][4])))
{
agentCounter++;
userAgent.Add(temp[j][4]);
}
}
}
}
}
agentFrequence += (agentCounter.ToString()) + ",";
upRange = timeIterator(upRange);
if (ipNum >= temp.Count)
{
finish = true;
}
}
cmd.Parameters["@p1"].Value=temp[0][0];
cmd.Parameters["@p2"].Value = agentFrequence;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
private bool agentController(List<string> agents, string usedAgent)
{
bool inList = false;
for (int i = 0; i < agents.Count; i++)
{
if (agents[0] == usedAgent)
inList = true;
}
return inList;
}
private List<int> timeIterator(List<int> temp)
{
List<int> upRange = new List<int>();
int rday = temp[0];
int rhour=temp[1];
int rminute = temp[2];
if (((rminute + 30) / 60) > 0)
{
rminute = (rminute + 30) % 60;
if ((rhour + 1) / 24 == 0)
{
rhour++;
}
else if ((rhour + 1) / 24 > 0)
{
if (((rday + 1) / 30) > 0)
{
rday = (rday + 1) % 30;
}
else
rday++;
}
}
else
rminute+=30;
upRange.Add(rday);
upRange.Add(rhour);
upRange.Add(rminute);
return upRange;
}