在我的例子里使用的是MS SqlServer 2005, 如果要使用其他数据库,可能某些地方要做一定的修改.
钱龙软件的股票代码按照市场不同保存在两个文件: stockinfo.sha和stockinfo.szn, 分别代表上海和深圳.
下面是我的程序的核心代码:
private
static
void
ReadStockCodes(
string
strPath,
string
p_strMarket)
{
FileStream stream = new FileStream(strPath, FileMode.Open, FileAccess.Read);
BinaryReader b_reader = new BinaryReader(stream);
SqlCommand cmd = new SqlCommand("", m_conn);
cmd.Transaction = m_tran;
cmd.CommandText = "insert into stockinfos ([StockCode] ,[Market] ,[StockName], [IsIndex]) " +
"VALUES (@StockCode,@Market,@StockName, @IsIndex)";
cmd.Parameters.Add("@StockCode", SqlDbType.NChar, 6);
cmd.Parameters.Add("@Market", SqlDbType.NVarChar, 10);
cmd.Parameters.Add("@StockName", SqlDbType.NVarChar, 20);
cmd.Parameters.Add("@IsIndex", SqlDbType.Bit);
try
{
while (stream.CanRead)
{
int nIndex = b_reader.ReadInt32();
string strCode = GetString(b_reader, 7);
int nCnt = 0;
List<Byte> endbytes = new List<byte>();
endbytes.Add(0);
string strName = GetStringTillEnd(b_reader,endbytes, out nCnt);
strCode = strCode.Replace('\0', ' ');
strName = strName.Replace('\0', ' ');
strCode = strCode.Trim();
strName = strName.Trim();
int n = 0x119;
n -= 4 + 7;
n -= nCnt;
cmd.Parameters[0].Value = strCode;
cmd.Parameters[1].Value = p_strMarket;
cmd.Parameters[2].Value = strName;
int nIsIndex=0;
if ((p_strMarket == "SH" && ( strCode.StartsWith("000") || strCode.StartsWith("801"))
||
(p_strMarket == "SZ" && ( strCode.StartsWith("399")))
||
strName.Contains("指数")))
{
nIsIndex = 1;
}
cmd.Parameters[3].Value = nIsIndex;
cmd.ExecuteNonQuery();
Console.WriteLine("Index={0}, Code={1}, Name={2}, {3}", nIndex, strCode, strName,
nIsIndex==1 ? "指数" : "证券");
b_reader.ReadBytes(n);
}
}
catch (EndOfStreamException)
{
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
}
{
FileStream stream = new FileStream(strPath, FileMode.Open, FileAccess.Read);
BinaryReader b_reader = new BinaryReader(stream);
SqlCommand cmd = new SqlCommand("", m_conn);
cmd.Transaction = m_tran;
cmd.CommandText = "insert into stockinfos ([StockCode] ,[Market] ,[StockName], [IsIndex]) " +
"VALUES (@StockCode,@Market,@StockName, @IsIndex)";
cmd.Parameters.Add("@StockCode", SqlDbType.NChar, 6);
cmd.Parameters.Add("@Market", SqlDbType.NVarChar, 10);
cmd.Parameters.Add("@StockName", SqlDbType.NVarChar, 20);
cmd.Parameters.Add("@IsIndex", SqlDbType.Bit);
try
{
while (stream.CanRead)
{
int nIndex = b_reader.ReadInt32();
string strCode = GetString(b_reader, 7);
int nCnt = 0;
List<Byte> endbytes = new List<byte>();
endbytes.Add(0);
string strName = GetStringTillEnd(b_reader,endbytes, out nCnt);
strCode = strCode.Replace('\0', ' ');
strName = strName.Replace('\0', ' ');
strCode = strCode.Trim();
strName = strName.Trim();
int n = 0x119;
n -= 4 + 7;
n -= nCnt;
cmd.Parameters[0].Value = strCode;
cmd.Parameters[1].Value = p_strMarket;
cmd.Parameters[2].Value = strName;
int nIsIndex=0;
if ((p_strMarket == "SH" && ( strCode.StartsWith("000") || strCode.StartsWith("801"))
||
(p_strMarket == "SZ" && ( strCode.StartsWith("399")))
||
strName.Contains("指数")))
{
nIsIndex = 1;
}
cmd.Parameters[3].Value = nIsIndex;
cmd.ExecuteNonQuery();
Console.WriteLine("Index={0}, Code={1}, Name={2}, {3}", nIndex, strCode, strName,
nIsIndex==1 ? "指数" : "证券");
b_reader.ReadBytes(n);
}
}
catch (EndOfStreamException)
{
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
}
其中用到的两个读入字符串的方法为:
public
static
string
GetString(BinaryReader m_reader,
int
p_nLength)
{
Encoding mbcs = Encoding.GetEncoding(936);
Encoding unicode = Encoding.Unicode;
byte[] byteBuffer = m_reader.ReadBytes(p_nLength);
byte[] unicodBytes = Encoding.Convert(mbcs, unicode, byteBuffer);
char[] buffer;// = new char[unicode.GetCharCount(unicodBytes)];
buffer = unicode.GetChars(unicodBytes);
// char[] buffer = m_reader.ReadChars(p_nLength);
string str = new string(buffer);
str = str.Replace('\x3', '\x20');
return str.Trim();
}
public static string GetStringTillEnd(BinaryReader m_reader, List < byte > p_endchars, out int p_nLength)
{
Encoding mbcs = Encoding.GetEncoding(936);
Encoding unicode = Encoding.Unicode;
p_nLength = 0;
byte[] byteBuffer_0 = new byte[1024];
byte b;
do
{
b = m_reader.ReadByte();
p_nLength++;
byteBuffer_0[p_nLength - 1] = b;
}
while (!p_endchars.Contains(b));
//byteBuffer = m_reader.ReadBytes(p_nLength);
byte[] byteBuffer= new byte[p_nLength];
for (int i = 0; i < p_nLength; i++) byteBuffer[i] = byteBuffer_0[i];
byte[] unicodBytes = Encoding.Convert(mbcs, unicode, byteBuffer);
char[] buffer;// = new char[unicode.GetCharCount(unicodBytes)];
buffer = unicode.GetChars(unicodBytes);
// char[] buffer = m_reader.ReadChars(p_nLength);
string str = new string(buffer);
str = str.Replace('\x3', '\x20');
return str.Trim();
}
{
Encoding mbcs = Encoding.GetEncoding(936);
Encoding unicode = Encoding.Unicode;
byte[] byteBuffer = m_reader.ReadBytes(p_nLength);
byte[] unicodBytes = Encoding.Convert(mbcs, unicode, byteBuffer);
char[] buffer;// = new char[unicode.GetCharCount(unicodBytes)];
buffer = unicode.GetChars(unicodBytes);
// char[] buffer = m_reader.ReadChars(p_nLength);
string str = new string(buffer);
str = str.Replace('\x3', '\x20');
return str.Trim();
}
public static string GetStringTillEnd(BinaryReader m_reader, List < byte > p_endchars, out int p_nLength)
{
Encoding mbcs = Encoding.GetEncoding(936);
Encoding unicode = Encoding.Unicode;
p_nLength = 0;
byte[] byteBuffer_0 = new byte[1024];
byte b;
do
{
b = m_reader.ReadByte();
p_nLength++;
byteBuffer_0[p_nLength - 1] = b;
}
while (!p_endchars.Contains(b));
//byteBuffer = m_reader.ReadBytes(p_nLength);
byte[] byteBuffer= new byte[p_nLength];
for (int i = 0; i < p_nLength; i++) byteBuffer[i] = byteBuffer_0[i];
byte[] unicodBytes = Encoding.Convert(mbcs, unicode, byteBuffer);
char[] buffer;// = new char[unicode.GetCharCount(unicodBytes)];
buffer = unicode.GetChars(unicodBytes);
// char[] buffer = m_reader.ReadChars(p_nLength);
string str = new string(buffer);
str = str.Replace('\x3', '\x20');
return str.Trim();
}
因为程序很简单, 其他不重要的部分我就不放上来了.