我已经尝试构建并运行此代码,但它总是向我发送错误:'因为未从数据库中检索数据。
我的代码在构建后没有错误。我一直在搜索,但我看不出有什么问题。
我希望我能为这种情况找到一些答案。谢谢
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string ProviderName = null;
string CatalogName = null;
string DataSourceName = null;
string ConnectionString = null;
string CommandString = null;
const int MaxRows = 100;
ProviderName = "Provider=Something;";
CatalogName = "Catalog=Something;";
DataSourceName = "DataSource=Something";
ConnectionString = ProviderName+CatalogName+DataSourceName;
////////////////////////////////////////////////////////////////////////////////
CommandString = "Something";
OleDbConnection DBConnection = null;
try
{
DBConnection = new OleDbConnection(ConnectionString);
}catch (Exception ex){
MessageBox.Show("Error: Not connected. \n{0}", ex.Message);
return;
}
try
{
OleDbCommand DBCommand = new OleDbCommand(CommandString, DBConnection);
DBConnection.Open();
OleDbDataReader DBReader = DBCommand.ExecuteReader();
/////////////////////////////////////////////////////////////////////////
DateTime DBTimestamp = new System.DateTime();
Double DBValue = new System.Double();
int DBQualityCode;
int DBFlags;
String ColumnEntry;
listView1.View = View.Details;
listView1.GridLines = true;
listView1.TabIndex = 0;
listView1.Width = 400;
listView1.Columns.Add(new ColumnHeader());
listView1.Columns[0].Text = ("Timestamp");
listView1.Columns[0].TextAlign = (HorizontalAlignment.Center);
listView1.Columns[0].Width = 150;
listView1.Columns.Add(new ColumnHeader());
listView1.Columns[1].Text = ("Value");
listView1.Columns[1].TextAlign = (HorizontalAlignment.Center);
listView1.Columns[1].Width = 60;
listView1.Columns.Add(new ColumnHeader());
listView1.Columns[2].Text = ("Quality Code");
listView1.Columns[2].TextAlign = (HorizontalAlignment.Center);
listView1.Columns[2].Width = 60;
listView1.Columns.Add(new ColumnHeader());
listView1.Columns[3].Text = ("Flags");
listView1.Columns[3].TextAlign = (HorizontalAlignment.Center);
listView1.Columns[3].Width = 60;
ListViewItem SingleRow;
int count = 0;
//////////////////////////////////////////////////////////////////////////
while ((DBReader.Read() && (count < MaxRows)))
{
DBTimestamp = DBReader.GetDateTime(1);
DBValue = DBReader.GetDouble(2);
DBQualityCode = DBReader.GetInt32(3);
DBFlags = DBReader.GetInt32(4);
//////////////////////////////////////
ColumnEntry = String.Format("{0:dd.MM.yy HH:mm:ss}{1:D2}", DBTimestamp, DBTimestamp.Millisecond);
SingleRow = new ListViewItem(ColumnEntry);
ColumnEntry = String.Format("{0:F}", DBValue);
SingleRow.SubItems.Add(ColumnEntry);
ColumnEntry = String.Format("{0:X2}", DBQualityCode);
SingleRow.SubItems.Add(ColumnEntry);
ColumnEntry = String.Format("{0:X4}", DBFlags);
SingleRow.SubItems.Add(ColumnEntry);
listView1.Items.Add(SingleRow);
count = count + 1;
}
DBReader.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error:Failed to retrive Data From Database. \n{0}", ex.Message);
}
finally
{
DBConnection.Close();
}
//////////////////////////////////////////////////
}
}
}