这组代码显然是从服务器检索数据(从特定的几天开始)并在记事本中显示它们。我不知道为什么,它不断弹出消息框“char”,“money”和“nvarchar”。请原谅我这是一个愚蠢的问题
SqlCommand objCmd = new SqlCommand("SELECT CONVERT(char(80), InvDate,3) AS InvDate,InvoiceNo,EmployerCode,TaxAmount + SubTotal AS Amount,'' AS Payment FROM Invoice WHERE (InvDate >= CONVERT(datetime, '"+dtpFrom.Text +"', 0 )) AND (InvDate <= CONVERT(datetime, '"+dtpTo.Text+"', 0))", objConn);
SqlDataReader objReader;
objReader = objCmd.ExecuteReader();
System.IO.FileStream fs = new System.IO.FileStream("C:\\CMSExportedData\\Sales-" + DateTime.Now.ToString("dd-MM-yyyy") + ".txt", System.IO.FileMode.Create);
System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.Default);
int count = 0;
while (objReader.Read())
{
for (int i = 0; i < 5; i++)
{
if (!objReader.IsDBNull(i))
{
string s;
s = objReader.GetDataTypeName(i);
MessageBox.Show(s);
if (objReader.GetDataTypeName(i) == "char")
{
sw.Write(objReader.GetString(i));
}
else if (objReader.GetDataTypeName(i) == "datetime")
{
sw.Write(objReader.GetSqlMoney(i).ToString());
}
else if (objReader.GetDataTypeName(i) == "nvarchar")
{
sw.Write(objReader.GetString(i));
}
}
if (i < 4)
{
sw.Write("\t");
}
}
count = count + 1;
sw.WriteLine();
}
sw.Flush();
fs.Close();
objReader.Close();
objConn.Close();
MessageBox.Show(count + " records exported successfully.");
this.Close();
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void dtpTo_ValueChanged(object sender, EventArgs e)
{
}
答案 0 :(得分:4)
您在循环中显示MessageBox
,这就是您查看多个MessageBox
数据类型的原因。如果您真的不想看到它们,只需删除以下行:
MessageBox.Show(s);
答案 1 :(得分:1)
您的数据源包含Money
类型的列。请注意,您不处理货币类型。
这就是为什么其中一个messageBox包含'Money'文本。