從錢龍數據中讀取股票權息信息導入到數據庫
前面寫了如果讀股票代碼和日線數據,下面是如何讀股票的權息信息。
錢龍中權息數據存儲在QLDATA/history/shase/weight和QLDATA/history/sznse/weight目錄下,每個文件對應一只股票。
與前文一樣,只貼核心代碼:
??????? private static void ReadStockWeights(string strPath, string p_strMarket)
??????? {
??????????? string[] parts = strPath.Split('//');
??????????? string strStockCode = null;
??????????? for (int i = parts.Length - 1; i >= 0;i-- )
??????????? {
??????????????? string strTemp = parts[i];
??????????????? if (strTemp.ToUpper().EndsWith(".WGT"))
??????????????? {
??????????????????? strStockCode = strTemp.Substring(0, strTemp.Length - 4) ;
??????????????????? break;
??????????????? }
??????????? }
??????????? Debug.Assert(strStockCode != null);
??????????? Console.WriteLine("Read stock weight from file '" + strPath + "'");
??????????? FileStream stream = new FileStream(strPath, FileMode.Open, FileAccess.Read);
??????????? BinaryReader b_reader = new BinaryReader(stream);
??????????? List<StockWeightInfo> weightInfos = new List<StockWeightInfo>();
??????????? try
??????????? {
??????????????? while (stream.CanRead && stream.Position < stream.Length)
??????????????? {
??????????????????? int[] oneRow = new int[9];
??????????????????? for( int i=0;i<9;i++)
??????????????????? {
??????????????????????? oneRow[i] = b_reader.ReadInt32();
??????????????????? }//for
??????????????????? if (oneRow[8] != 0)
??????????????????? {
??????????????????????? throw new Exception("Last entry is not empty");
??????????????????? }
??????????????????? //read date
??????????????????? int nYear = oneRow[0] >> 20;
??????????????????? int nMon = (int)(((uint)(oneRow[0] << 12))>> 28);
??????????????????? int nDay = (oneRow[0] & 0xffff)>> 11;
??????????????????? DateTime wgtDate;
??????????????????? if (nYear == 0 && nMon == 0 && nDay == 0)
??????????????????????? wgtDate = DateTime.MinValue;
??????????????????? else
??????????????????????????? wgtDate = new DateTime(nYear, nMon, nDay);
??????????????????? StockWeightInfo wgtInfo = new StockWeightInfo();
??????????????????? wgtInfo.m_date = wgtDate;
??????????????????? wgtInfo.m_stockCountAsGift = oneRow[1];/**10000.0f;
??????????????????? wgtInfo.m_stockCountForSell = oneRow[2];/**10000.0f;
??????????????????? wgtInfo.m_priceForSell = oneRow[3];/**1000.0f;
??????????????????? wgtInfo.m_bonus = oneRow[4];/**1000.0f;
??????????????????? wgtInfo.m_stockCountOfIncreasement = oneRow[5];/**10000.0f;
??????????????????? wgtInfo.m_stockOwnership = (ulong)oneRow[6];
??????????????????? wgtInfo.m_freeStockCount = (ulong)oneRow[7];
??????????????????? if (!weightInfos.Contains(wgtInfo))
??????????????????? {
??????????????????????? weightInfos.Add(wgtInfo);
??????????????????????? //Console.WriteLine();
??????????????????????? //Console.Write(wgtInfo.ToString());
??????????????????? }
??????????????? }//while
??????????????? weightInfos.Sort();
??????????? }
??????????? catch (EndOfStreamException)
??????????? {
??????????????? Console.WriteLine("Unexpected end of stream");
??????????? }
??????????? catch (Exception ex)
??????????? {
??????????????? Console.WriteLine(ex.Message);
??????????????? throw;
??????????? }
??????????? finally
??????????? {
??????????????? stream.Close();
??????????? }
??????????? SqlCommand cmd = new SqlCommand("", m_conn);
??????????? cmd.Transaction = m_tran;
??????????? cmd.CommandText = "INSERT INTO [StockWeightInfos] ([StockCode] ,[Market] ,[Date] ,[StockCountAsGift] ,[StockCountForSell] ,[PriceForSell] ,[Bonus] ,[StockCountOfIncreasement] ,[StockOwnership] ,[FreeStockCount]) " +
?????????????????????????????? "VALUES (@StockCode, @Market ,@Date ,@StockCountAsGift ,@StockCountForSell ,@PriceForSell ,@Bonus ,@StockCountOfIncreasement ,@StockOwnership ,@FreeStockCount)";
??????????? cmd.Parameters.Add("@StockCode", SqlDbType.NVarChar, 50);
??????????? cmd.Parameters.Add("@Market", SqlDbType.NVarChar, 50);
??????????? cmd.Parameters.Add("@Date", SqlDbType.DateTime);
??????????? cmd.Parameters.Add("@StockCountAsGift", SqlDbType.Real);
??????????? cmd.Parameters.Add("@StockCountForSell", SqlDbType.Real);
??????????? cmd.Parameters.Add("@PriceForSell", SqlDbType.Real);
??????????? cmd.Parameters.Add("@Bonus", SqlDbType.Real);
??????????? cmd.Parameters.Add("@StockCountOfIncreasement", SqlDbType.Real);
??????????? cmd.Parameters.Add("@StockOwnership", SqlDbType.BigInt);
??????????? cmd.Parameters.Add("@FreeStockCount", SqlDbType.BigInt);
??????????? foreach(StockWeightInfo info in weightInfos)
??????????? {
??????????????? cmd.Parameters["@StockCode"].Value = strStockCode;
??????????????? cmd.Parameters["@Market"].Value = p_strMarket;
??????????????? cmd.Parameters["@Bonus"].Value = info.m_bonus / 1000.0;
??????????????? if (info.m_date != DateTime.MinValue)
??????????????????? cmd.Parameters["@Date"].Value = info.m_date;
??????????????? else
??????????????????? cmd.Parameters["@Date"].Value = DBNull.Value;
??????????????? cmd.Parameters["@FreeStockCount"].Value = info.m_freeStockCount;
??????????????? cmd.Parameters["@PriceForSell"].Value = info.m_priceForSell/1000.0;
??????????????? cmd.Parameters["@StockCountAsGift"].Value = info.m_stockCountAsGift/10000.0;
??????????????? cmd.Parameters["@StockCountForSell"].Value = info.m_stockCountForSell/10000.0;
??????????????? cmd.Parameters["@StockCountOfIncreasement"].Value = info.m_stockCountOfIncreasement/10000.0;
??????????????? cmd.Parameters["@StockOwnership"].Value = info.m_stockOwnership;
??????????????? int nCnt=cmd.ExecuteNonQuery();
??????????????? Debug.Assert(nCnt == 1);
??????????? }//foreach
??????? }//ReadStockWeights