您的位置:首页 > 编程语言 > C#

C#2005 一个简单的查询界面代码:DataGridView使用、图像显示、复合查询样例

2008-04-11 15:29 1151 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using System.Runtime.InteropServices;
using FileControl;

namespace xxxxx
{
public partial class FrmFind : Form
{
private const int CLSLicenseMaxLength = 16; // 车牌字符串最大长度
private const int CLSLaneMinNumber = 1; // 车道最小值
private const int CLSLaneMaxNumber = 255; // 车道最大值
private const int CLSSpeedMinNumber = 0; // 车速最小值
private const int CLSSpeedMaxNumber = 300; // 车速最大值
private const int CLSDefaultReturnRecord = 500; // 查询默认返回记录数目
private const int CLSDefaultLoadRecord = 8; // 启动时默认返回记录数目
private const int CLSOverSpeedStateNormal = 1303; // 一般超速状态代码 小于等于50%
private const int CLSOverSpeedStateSerious = 1603; // 严重超速状态代码 大于50%

private DataSet clsDSVehicle; // 内存中缓存的数据库表保存在此对象中
private Image clsImageFull; // 图像显示对象-全景
private Image clsImageSpecial; // 图像显示对象-牌识用
private int clsPreviousRowIndex; // 前一次显示记录的行索引号(在DataSet中)

public FrmFind()
{
InitializeComponent();

clsPreviousRowIndex = -1; // 前一次行的索引号
}

private void FrmFind_Load(object sender, EventArgs e)
{
InitCompenent();

GetVehicleFromDatabase(CLSDefaultLoadRecord, "");
}

private void FrmFind_FormClosed(object sender, FormClosedEventArgs e)
{
if (clsImageFull != null)
{
clsImageFull.Dispose();
}
if (clsImageSpecial != null)
{
clsImageSpecial.Dispose();
}

if (clsDSVehicle != null)
{
clsDSVehicle.Dispose();
}
}

/// <summary>
/// 初始化界面上各个组件的状态
/// </summary>
private void InitCompenent()
{
dtpTimeStart.Enabled = false;
dtpTimeEnd.Enabled = false;
txtLicenseFind.Enabled = false;
cboLicenseColor.Enabled = false;
cboLane.Enabled = false;
cboSpeedCompareSymbol.Enabled = false;
cboSpeed.Enabled = false;

InitDataGridView();
}

/// <summary>
/// 初始化DataGridView控件的状态
/// </summary>
private void InitDataGridView()
{
dgvVehicle.AutoGenerateColumns = false;
dgvVehicle.AllowUserToOrderColumns = true;
dgvVehicle.AllowUserToAddRows = false;
dgvVehicle.AllowUserToDeleteRows = false;
// 设置奇数数据行的背景色
dgvVehicle.AlternatingRowsDefaultCellStyle.BackColor = SystemColors.InactiveCaptionText;
dgvVehicle.MultiSelect = false;
// 设置采用行选取模式
dgvVehicle.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
//dgvVehicle.VirtualMode = true // 如果含未绑定数据则必须选设为true
this.dgvVehicle.DataSource = this.bsVehicle;

DataGridViewTextBoxColumn colautoId = new DataGridViewTextBoxColumn();
colautoId.DataPropertyName = "autoId"; // 数据源字段
colautoId.HeaderText = "序号";
colautoId.Name = "自增长序号";
colautoId.Width = 40;
colautoId.SortMode = DataGridViewColumnSortMode.NotSortable; // 不允许按列排序
dgvVehicle.Columns.Add(colautoId);

DataGridViewTextBoxColumn colindex = new DataGridViewTextBoxColumn();
colindex.DataPropertyName = "index";
colindex.HeaderText = "索引";
colindex.Name = "车辆索引号";
colindex.Width = 40;
colindex.SortMode = DataGridViewColumnSortMode.NotSortable;
dgvVehicle.Columns.Add(colindex);

DataGridViewTextBoxColumn collicense = new DataGridViewTextBoxColumn();
collicense.DataPropertyName = "license";
collicense.HeaderText = "车牌号码";
collicense.Name = "车牌号码";
collicense.Width =70;
collicense.SortMode = DataGridViewColumnSortMode.NotSortable;
dgvVehicle.Columns.Add(collicense);

DataGridViewTextBoxColumn collicenseColor = new DataGridViewTextBoxColumn();
collicenseColor.DataPropertyName = "licenseColor";
collicenseColor.HeaderText = "颜色";
collicenseColor.Name = "车牌颜色";
collicenseColor.Width = 35;
collicenseColor.SortMode = DataGridViewColumnSortMode.NotSortable;
dgvVehicle.Columns.Add(collicenseColor);

DataGridViewTextBoxColumn collicenseType = new DataGridViewTextBoxColumn();
collicenseType.DataPropertyName = "licenseType";
collicenseType.HeaderText = "类型";
collicenseType.Name = "车牌类型";
collicenseType.Width = 0;
collicenseType.SortMode = DataGridViewColumnSortMode.NotSortable;
collicenseType.Visible = false;
dgvVehicle.Columns.Add(collicenseType);

DataGridViewTextBoxColumn colpassTime = new DataGridViewTextBoxColumn();
colpassTime.DataPropertyName = "passTime"; // 数据源字段
colpassTime.DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
colpassTime.HeaderText = "通行时间";
colpassTime.Name = "通行时间";
colpassTime.Width = 125;
colpassTime.SortMode = DataGridViewColumnSortMode.NotSortable;
dgvVehicle.Columns.Add(colpassTime);

DataGridViewTextBoxColumn collocation = new DataGridViewTextBoxColumn();
collocation.DataPropertyName = "location"; // 数据源字段
collocation.HeaderText = "地点";
collocation.Name = "图像采集地点";
collocation.Width = 120;
collocation.SortMode = DataGridViewColumnSortMode.NotSortable;
dgvVehicle.Columns.Add(collocation);

DataGridViewTextBoxColumn collane = new DataGridViewTextBoxColumn();
collane.DataPropertyName = "lane"; // 数据源字段
collane.HeaderText = "车道";
collane.Name = "车道";
collane.Width = 35;
collane.SortMode = DataGridViewColumnSortMode.NotSortable;
dgvVehicle.Columns.Add(collane);

DataGridViewTextBoxColumn coltype = new DataGridViewTextBoxColumn();
coltype.DataPropertyName = "type"; // 数据源字段
coltype.HeaderText = "车型";
coltype.Name = "车型";
coltype.Width = 50;
coltype.SortMode = DataGridViewColumnSortMode.NotSortable;
dgvVehicle.Columns.Add(coltype);

DataGridViewTextBoxColumn coldirection = new DataGridViewTextBoxColumn();
coldirection.DataPropertyName = "direction"; // 数据源字段
coldirection.HeaderText = "方向";
coldirection.Name = "方向";
coldirection.Width = 35;
coldirection.SortMode = DataGridViewColumnSortMode.NotSortable;
dgvVehicle.Columns.Add(coldirection);

DataGridViewTextBoxColumn colspeed = new DataGridViewTextBoxColumn();
colspeed.DataPropertyName = "speed"; // 数据源字段
colspeed.HeaderText = "车速";
colspeed.Name = "车速";
colspeed.Width = 35;
colspeed.SortMode = DataGridViewColumnSortMode.NotSortable;
dgvVehicle.Columns.Add(colspeed);

DataGridViewTextBoxColumn colspeedLimit = new DataGridViewTextBoxColumn();
colspeedLimit.DataPropertyName = "speedLimit"; // 数据源字段
colspeedLimit.HeaderText = "限速";
colspeedLimit.Name = "限速";
colspeedLimit.Width = 35;
colspeedLimit.SortMode = DataGridViewColumnSortMode.NotSortable;
dgvVehicle.Columns.Add(colspeedLimit);

DataGridViewTextBoxColumn colstate = new DataGridViewTextBoxColumn();
colstate.DataPropertyName = "state"; // 数据源字段
colstate.HeaderText = "状态";
colstate.Name = "车辆状态";
colstate.Width = 40;
colstate.SortMode = DataGridViewColumnSortMode.NotSortable;
dgvVehicle.Columns.Add(colstate);
}

/// <summary>
/// DataGridView控件的当前单元格变化事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dgvVehicle_CurrentCellChanged(object sender, EventArgs e)
{
if (dgvVehicle.CurrentRow != null)
{
if (clsPreviousRowIndex == dgvVehicle.CurrentRow.Index)
{
return;
}
else
{
clsPreviousRowIndex = dgvVehicle.CurrentRow.Index;
ShowCurrentRecord(dgvVehicle.CurrentRow.Index);
}
}
}

/// <summary>
/// 显示当前记录
/// </summary>
/// <param name="noRecordFlag">此参数为true时加载无记录信息</param>
private void ShowCurrentRecord(bool noRecordFlag)
{
if (noRecordFlag)
{
txtLicense.BackColor = Color.Gray;
txtLicense.Text = "";
lblCurrentVehicleDetail.Text = "";
picFull.Image = null;
picSpecial.Image = null;
}
}

/// <summary>
/// 显示当前记录
/// </summary>
/// <param name="recordIndex">此参数大于等于0时界面加载显示DataSet中索引为recordIndex的记录信息</param>
private void ShowCurrentRecord(int recordIndex)
{
if (recordIndex < 0)
{
return;
}

string licenseColorCode = clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[4].ToString().Trim();
switch (licenseColorCode)
{
case "0":
txtLicense.BackColor = Color.White;
txtLicense.ForeColor = Color.Black;
break;
case "1":
txtLicense.BackColor = Color.Yellow;
txtLicense.ForeColor = Color.Black;
break;
case "2":
txtLicense.BackColor = Color.Blue;
txtLicense.ForeColor = Color.White;
break;
case "3":
txtLicense.BackColor = Color.Black;
txtLicense.ForeColor = Color.White;
break;
case "5": // 底色为绿色时字体色为白 2008-01-24从罗兵华处得知
txtLicense.BackColor = Color.Green;
txtLicense.ForeColor = Color.White;
break;
default:
txtLicense.BackColor = Color.Gray;
txtLicense.ForeColor = Color.Black;
break;
} //End Switch
txtLicense.Text = clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[2].ToString().Trim();

string detail = "";
detail = detail + "索引:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[1].ToString();
detail = detail + "/r/n车牌号码:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[2].ToString();
detail = detail + "/r/n车牌颜色:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[3].ToString();
detail = detail + "/r/n时间:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[6].ToString();
detail = detail + "/r/n位置:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[7].ToString();
detail = detail + "/r/n车道:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[8].ToString();
detail = detail + "/r/n车型:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[9].ToString();
detail = detail + "/r/n方向:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[10].ToString();
//detail = detail + "/r/n小车限速:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[13].ToString();
//detail = detail + "/r/n卡车限速:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[14].ToString();
detail = detail + "/r/n本车限速:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[15].ToString();
detail = detail + "/r/n触发车速:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[12].ToString();
detail = detail + "/r/n车速:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[11].ToString();
if (CLSOverSpeedStateNormal == (int)clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[16])
{
detail = detail + "/r/n状态:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[16].ToString() + " 一般超速,超速小于等于50%";
}
else if (CLSOverSpeedStateSerious == (int)clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[16])
{
detail = detail + "/r/n状态:" + clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[16].ToString() + " 严重超速,超速大于50%";
}
lblCurrentVehicleDetail.Text = detail + "/r/n";

try
{
string imageFullPath = clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[17].ToString().Trim();
if (File.Exists(imageFullPath))
{
if (clsImageFull != null)
{
clsImageFull.Dispose();
}
clsImageFull = Image.FromFile(imageFullPath);
picFull.Image = clsImageFull;
}
else
{
picFull.Image = null;
lblCurrentVehicleDetail.Text = lblCurrentVehicleDetail.Text + "未找到全景图像!";
}

string imageSpecialPath = clsDSVehicle.Tables["tbVehicle"].Rows[recordIndex].ItemArray[18].ToString().Trim();
if (File.Exists(imageSpecialPath))
{
if (clsImageSpecial != null)
{
clsImageSpecial.Dispose();
}
clsImageSpecial = Image.FromFile(imageSpecialPath);
picSpecial.Image = clsImageSpecial;
}
else
{
picSpecial.Image = null;
lblCurrentVehicleDetail.Text = lblCurrentVehicleDetail.Text + "未找到牌识图像!";
}
}
catch (Exception ex)
{
Log.WriteToFile("FrmFind.ShowCurrentRecord Exception","recordIndex=" + recordIndex.ToString() + " ex.Message: " + ex.ToString());

// 界面显示图像加载异常信息
lblCurrentVehicleDetail.Text = lblCurrentVehicleDetail.Text + "显示图像发生异常!详细请查阅日志!";
}
}

private void chkTimeStart_CheckedChanged(object sender, EventArgs e)
{
if (chkTimeStart.Checked)
{
dtpTimeStart.Enabled = true;
dtpTimeStart.Value = DateTime.Now;
}
else
{
dtpTimeStart.Enabled = false;
}
}

private void chkTimeEnd_CheckedChanged(object sender, EventArgs e)
{
if (chkTimeEnd.Checked)
{
dtpTimeEnd.Enabled = true;
dtpTimeEnd.Value = DateTime.Now;
}
else
{
dtpTimeEnd.Enabled = false;
}
}

private void chkLicense_CheckedChanged(object sender, EventArgs e)
{
if (chkLicense.Checked)
{
txtLicenseFind.Enabled = true;
if (txtLicenseFind.Text.Length == 0)
{
txtLicenseFind.Text = "无车牌";
}
txtLicenseFind.Select();
}
else
{
txtLicenseFind.Enabled = false;
}
}

private void chkLicenseColor_CheckedChanged(object sender, EventArgs e)
{
if (chkLicenseColor.Checked)
{
cboLicenseColor.Enabled = true;
if (cboLicenseColor.SelectedIndex < 0)
{
cboLicenseColor.SelectedIndex = 2;
}
}
else
{
cboLicenseColor.Enabled = false;
}
}

private void chkLane_CheckedChanged(object sender, EventArgs e)
{
if (chkLane.Checked)
{
cboLane.Enabled = true;
if (cboLane.SelectedIndex < 0)
{
cboLane.SelectedIndex = 0;
}
}
else
{
cboLane.Enabled = false;
}
}

private void chkSpeed_CheckedChanged(object sender, EventArgs e)
{
if (chkSpeed.Checked)
{
cboSpeedCompareSymbol.Enabled = true;
if (cboSpeedCompareSymbol.SelectedIndex < 0)
{
cboSpeedCompareSymbol.SelectedIndex = 0;
}
cboSpeed.Enabled = true;
if (cboSpeed.Text.Length == 0)
{
cboSpeed.Text = "60";
}
}
else
{
cboSpeedCompareSymbol.Enabled = false;
cboSpeed.Enabled = false;
}
}

private void txtLicenseFind_Validated(object sender, EventArgs e)
{
int inputLicenseLength = txtLicenseFind.Text.Length;
if (inputLicenseLength > CLSLicenseMaxLength)
{
MessageBox.Show("您输入的车牌字符串的超过了长度限制,最长为" + CLSLicenseMaxLength.ToString() + "个字符!/r/n" + "一般车牌字符串长度为7。/r/n" + "本软件支持模糊查询,例如您输入:123,可以查询所有含123的车牌号码!", "请重新输入!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
txtLicenseFind.Focus();
txtLicenseFind.Select(0, inputLicenseLength);
}
}

private void cboLane_Validated(object sender, EventArgs e)
{
try
{
int lane = int.Parse(cboLane.Text);

if (lane < CLSLaneMinNumber || lane > CLSLaneMaxNumber)
{
MessageBox.Show("您输入的车道号超出了允许范围,必须为" + CLSLaneMinNumber.ToString() + "-" + CLSLaneMaxNumber.ToString() + "之间的数字!", "请重新输入!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
cboLane.Focus();
cboLane.Select(0, cboLane.Text.Length);
}
}
catch (Exception ex)
{
MessageBox.Show("您输入的车道号不符合要求,导致以下异常产生:/r/n" + ex.Message + "/r/n输入必须为" + CLSLaneMinNumber.ToString() + "-" + CLSLaneMaxNumber.ToString() + "之间的数字!", "请重新输入!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
cboLane.Text = "1";
cboLane.Focus();
cboLane.Select(0, cboLane.Text.Length);
}
}

private void cboSpeed_Validated(object sender, EventArgs e)
{
try
{
int speed = int.Parse(cboSpeed.Text);

if (speed < CLSSpeedMinNumber || speed > CLSSpeedMaxNumber)
{
MessageBox.Show("您输入的车速超出了允许范围,必须为" + CLSSpeedMinNumber.ToString() + "-" + CLSSpeedMaxNumber.ToString() + "之间的数字!", "请重新输入!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
cboSpeed.Focus();
cboSpeed.Select(0, cboSpeed.Text.Length);
}
}
catch (Exception ex)
{
MessageBox.Show("您输入的车速不符合要求,导致以下异常产生:/r/n" + ex.Message + "/r/n输入必须为" + CLSSpeedMinNumber.ToString() + "-" + CLSSpeedMaxNumber.ToString() + "之间的数字!", "请重新输入!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
cboSpeed.Text = "60";
cboSpeed.Focus();
cboSpeed.Select(0, cboSpeed.Text.Length);
}
}

private void btnFind_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;

string errorMsg = "";
if (!ValidateSqlParameter(ref errorMsg))
{
MessageBox.Show("您输入的查询条件不正确:/r/n" + errorMsg + "/r/n请重新选择查询条件!", "错误提示!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
this.Cursor = Cursors.Arrow;
return;
}

GetVehicleFromDatabase(0, GetSearchSqlCommand(0));

this.Cursor = Cursors.Arrow;
}

/// <summary>
/// 验证Sql查询条件间的参数有效性
/// </summary>
/// <param name="errorMsg">检测到错误则写入错误信息并立即返回</param>
/// <returns>全部有效返回 true;否则返回false,并将错误信息写入errorMsg</returns>
private bool ValidateSqlParameter(ref string errorMsg)
{
string timeStart;
string timeEnd;
errorMsg = null;

if (chkTimeStart.Checked && chkTimeEnd.Checked)
{
timeStart = dtpTimeStart.Value.ToString("yyyy-MM-dd HH:mm:ss");
timeEnd = dtpTimeEnd.Value.ToString("yyyy-MM-dd HH:mm:ss");

if ((dtpTimeStart.Value > dtpTimeEnd.Value) && (!timeStart.Equals(timeEnd)))
{
errorMsg = "终止时间必须大于等于起始时间!/r/n" + "本次查询终止时间:" + dtpTimeEnd.Value.ToString() + " 小于起始时间:" + dtpTimeStart.Value.ToString();
return false;
}
}

return true;
}

/// <summary>
/// 获取查询语句【目前只支持Access数据库】
/// </summary>
/// <param name="databaseType">数据库类型 0 Access;1 MySql;2 Sql Server;3 Oracle</param>
/// <returns>返回查询语句</returns>
private string GetSearchSqlCommand(int databaseType)
{
string sqlCommand;

string timeStart;
string timeEnd;
bool otherChecked = false;

if (chkNotLimitReturnRecord.Checked)
{
sqlCommand = "select * from tbVehicle";
}
else
{
sqlCommand = "select top " + CLSDefaultReturnRecord.ToString() + " * from tbVehicle";
}

if (chkTimeStart.Checked)
{
if (otherChecked)
{
sqlCommand = sqlCommand + " and ";
}
else
{
sqlCommand = sqlCommand + " where ";
}

timeStart = dtpTimeStart.Value.ToString("yyyy-MM-dd HH:mm:ss");
if (0 == databaseType) // 如果是Access数据,时间标识字符不一样
{
sqlCommand = sqlCommand + "passTime >=" + "#" + timeStart + "#";
}
else
{
sqlCommand = sqlCommand + "passTime >=" + "'" + timeStart + "'";
}
otherChecked = true;
}

if (chkTimeEnd.Checked)
{
if (otherChecked)
{
sqlCommand = sqlCommand + " and ";
}
else
{
sqlCommand = sqlCommand + " where ";
}

timeEnd = dtpTimeEnd.Value.ToString("yyyy-MM-dd HH:mm:ss");
if (0 == databaseType) // 如果是Access数据,时间标识字符不一样
{
sqlCommand = sqlCommand + "passTime <=" + "#" + timeEnd + "#";
}
else
{
sqlCommand = sqlCommand + "passTime <=" + "'" + timeEnd + "'";
}
otherChecked = true;
}

if (chkLicense.Checked)
{
if (otherChecked)
{
sqlCommand = sqlCommand + " and ";
}
else
{
sqlCommand = sqlCommand + " where ";
}

sqlCommand = sqlCommand + "license like " + "'%" + txtLicenseFind.Text.Trim() + "%'";
otherChecked = true;
}

if (chkLicenseColor.Checked)
{
if (otherChecked)
{
sqlCommand = sqlCommand + " and ";
}
else
{
sqlCommand = sqlCommand + " where ";
}

sqlCommand = sqlCommand + "licenseColor = " + "'" + cboLicenseColor.Text.Trim() + "'";
otherChecked = true;
}

if (chkLane.Checked)
{
if (otherChecked)
{
sqlCommand = sqlCommand + " and ";
}
else
{
sqlCommand = sqlCommand + " where ";
}

sqlCommand = sqlCommand + "lane = " + cboLane.Text.Trim();
otherChecked = true;
}

if (chkSpeed.Checked)
{
if (otherChecked)
{
sqlCommand = sqlCommand + " and ";
}
else
{
sqlCommand = sqlCommand + " where ";
}

switch (cboSpeedCompareSymbol.SelectedIndex)
{
case 0:
sqlCommand = sqlCommand + "Speed >= " + cboSpeed.Text.Trim();
break;
case 1:
sqlCommand = sqlCommand + "Speed = " + cboSpeed.Text.Trim();
break;
case 2:
sqlCommand = sqlCommand + "Speed <= " + cboSpeed.Text.Trim();
break;
default:
sqlCommand = sqlCommand + "Speed >= " + cboSpeed.Text.Trim();
break;
} //End Switch

otherChecked = true;
}

if (chkOrderByPassTime.Checked)
{
sqlCommand = sqlCommand + " order by passTime";
}

return sqlCommand;
}

/// <summary>
/// 从数据库装载车辆信息
/// </summary>
/// <param name="recordNumber">当此参数大于0小于CLSDefaultReturnRecord时,返回前recordNumber条记录</param>
/// <param name="sqlCommand">当recordNumber无效时用指定查询语句查询</param>
private void GetVehicleFromDatabase(int recordNumber,string sqlCommand)
{
try
{
if (clsDSVehicle != null)
{
clsDSVehicle.Clear();
clsDSVehicle.Dispose();
}

if (recordNumber > 0 && recordNumber < CLSDefaultReturnRecord)
{
clsDSVehicle = DataAccess.GetDataSet("select top " + recordNumber.ToString() + " * from tbVehicle");
}
else
{
clsDSVehicle = DataAccess.GetDataSet(sqlCommand);
}

if (clsDSVehicle != null)
{
clsDSVehicle.Tables[0].TableName = "tbVehicle";

if (clsDSVehicle.Tables[0].Rows.Count <= 0)
{
// 显示无信息
ShowCurrentRecord(true);
return;
}

this.bsVehicle.DataSource = clsDSVehicle;
this.bsVehicle.DataMember = "tbVehicle";
this.bsVehicle.AllowNew = false;

this.bnVehicle.BindingSource = this.bsVehicle;

clsPreviousRowIndex = -1;
}
else
{
// 异常
ShowCurrentRecord(true);
MessageBox.Show("从数据库查询时发生异常,可能是表tbVehicle不存在!/r/n" + "recordNumber=" + recordNumber.ToString() + "/r/nsqlCommand=" + sqlCommand, "错误提示!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
Log.WriteToFile("FrmFind.GetVehicleFromDatabase", "clsDSVehicle == null recordNumber=" + recordNumber.ToString() + " sqlCommand=" + sqlCommand);
}
}
catch (Exception ex)
{
MessageBox.Show("从数据库查询时发生以下异常:/r/n" + ex.Message , "错误提示!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
Log.WriteToFile("FrmFind.GetVehicleFromDatabase exception", "recordNumber=" + recordNumber.ToString() + " sqlCommand=" + sqlCommand + " ex.Message=" + ex.Message);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐