public bool InsertBifaTop3(BifaTop3 bifaTop3) { int total = 0; string conStr = "server=localhost;User Id=root;database=aicai;" + "Password=root;Character Set=utf8;"; using (MySqlConnection mySqlCon = new MySqlConnection(conStr)) { mySqlCon.Open(); using (MySqlCommand mySqlCom = mySqlCon.CreateCommand()) { //这里需要注意的是在 Sql 语句中有一个参数是 ?BlogsName //很明显,这个参数有点古怪,和我们一直使用的 @ 不一样, //这是因为在 MySql 以前的提供的 .NET Connector 中 //都是采用 ? 来标志一个参数的,而现在的 .NET Connector 呢, //您既可以采用 ? 来标志一个参数,您也可以使用一个 @ 符号来标志一个参数 //新版的 .NET Connector 对此都是支持的 string sqlStr = @"insert into bifatop3 ( riqi,paiming,zhudui,kedui,shijian,shuxing,peilv,chengjiaoe,lucky,operator,operatetime) values ( ?riqi,?paiming,?zhudui,?kedui,?shijian,?shuxing,?peilv,?chengjiaoe,?lucky,?operator,?operatetime ) "; mySqlCom.CommandText = sqlStr; mySqlCom.Parameters.AddWithValue("?riqi", bifaTop3.Riqi); mySqlCom.Parameters.AddWithValue("?paiming", bifaTop3.Paiming); mySqlCom.Parameters.AddWithValue("?zhudui", bifaTop3.Zhudui); mySqlCom.Parameters.AddWithValue("?kedui", bifaTop3.Kedui); mySqlCom.Parameters.AddWithValue("?shijian", bifaTop3.Shijian); mySqlCom.Parameters.AddWithValue("?shuxing", bifaTop3.Shuxing); mySqlCom.Parameters.AddWithValue("?peilv", bifaTop3.Peilv); mySqlCom.Parameters.AddWithValue("?chengjiaoe", bifaTop3.Chengjiaoe); mySqlCom.Parameters.AddWithValue("?lucky", bifaTop3.Lucky); mySqlCom.Parameters.AddWithValue("?operator", bifaTop3.Operator); mySqlCom.Parameters.AddWithValue("?operatetime", bifaTop3.OperateTime); total = Convert.ToInt32(mySqlCom.ExecuteScalar()); } } return total == 0 ? true : false; }
private void ImportBifa(DateTime importdate) { try { DateTime importDate = importdate; string url = GetBifaUrl(importDate); string jsonData = GetHtmlFromUrl(url); HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument(); document.LoadHtml(jsonData); HtmlNode rootNode = document.DocumentNode; HtmlNodeCollection sideDivLis = rootNode.SelectNodes("./div/div"); foreach (HtmlNode sideDiv in sideDivLis) { int number = Convert.ToInt32(sideDiv.SelectSingleNode("./p/i").InnerText); HtmlNodeCollection spans = sideDiv.SelectNodes("./p")[0].SelectNodes("./em/span"); string zhudui = spans[0].InnerText.Trim(); string kedui = spans[2].InnerText.Trim(); string shijianandshuxing = sideDiv.SelectNodes("./p")[1].InnerText.Trim('\t'); int shuxingIndex = shijianandshuxing.IndexOf("属性:"); string shijian = importDate.Year + "-" + shijianandshuxing.Substring(3, shuxingIndex - 3).Trim('\t'); string shuxing = shijianandshuxing.Substring(shuxingIndex + 3).Trim('\t'); string includePeilv = sideDiv.SelectNodes("./p")[2].InnerText; int endindex = includePeilv.IndexOf(",成交额"); string peilv = includePeilv.Substring(3, endindex - 3); string chengjiaoe = sideDiv.SelectNodes("./p/span[@class='c_red']")[0].InnerText; BifaTop3 bifa = new BifaTop3(); bifa.Paiming = number; bifa.Riqi = importDate.Date; bifa.Zhudui = zhudui; bifa.Kedui = kedui; bifa.Shijian = Convert.ToDateTime(shijian); bifa.Shuxing = shuxing; bifa.Peilv = Convert.ToDouble(peilv); bifa.Chengjiaoe = Convert.ToInt32(chengjiaoe); bifa.Lucky = -1; bifa.Operator = "吴林"; bifa.OperateTime = DateTime.Now; new BifaTop3DAL().InsertBifaTop3(bifa); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }