实例介绍
【实例截图】
【核心代码】
ACCESS数据库操作代码
public class AccessOperator
{
private string conn_str = null;
private OleDbConnection ole_connection = null;
private OleDbCommand ole_command = null;
private OleDbDataReader ole_reader = null;
private DataTable dt = null;
/// <summary>
/// 构造函数
/// </summary>
public AccessOperator()
{
//conn_str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" Environment.CurrentDirectory "\\Data\\test_data.mdb'";//office 2003
conn_str = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" Environment.CurrentDirectory "\\Data\\test_data.mdb'";//office 2007
InitDB();
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="db_path">数据库路径</param>
public AccessOperator(string db_path)
{
//conn_str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" db_path "'";
conn_str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" db_path "'";
InitDB();
}
private void InitDB()
{
ole_connection = new OleDbConnection(conn_str);//创建实例
ole_command = new OleDbCommand();
}
/// <summary>
/// 转换数据格式
/// </summary>
/// <param name="reader">数据源</param>
/// <returns>数据列表</returns>
private DataTable ConvertOleDbReaderToDataTable(ref OleDbDataReader reader)
{
DataTable dt_tmp = null;
DataRow dr = null;
int data_column_count = 0;
int i = 0;
data_column_count = reader.FieldCount;
dt_tmp = BuildAndInitDataTable(data_column_count, ref reader);
if (dt_tmp == null)
{
return null;
}
while (reader.Read())
{
dr = dt_tmp.NewRow();
for (i = 0; i < data_column_count; i)
{
dr[i] = reader[i];
}
dt_tmp.Rows.Add(dr);
}
return dt_tmp;
}
/// <summary>
/// 创建并初始化数据列表
/// </summary>
/// <param name="Field_Count">列的个数</param>
/// <returns>数据列表</returns>
/// 按序号创建列的名称
private DataTable BuildAndInitDataTable(int Field_Count)
{
DataTable dt_tmp = null;
DataColumn dc = null;
int i = 0;
if (Field_Count <= 0)
{
return null;
}
dt_tmp = new DataTable();
for (i = 0; i < Field_Count; i)
{
dc = new DataColumn(i.ToString());
dt_tmp.Columns.Add(dc);
}
return dt_tmp;
}
/// <summary>
/// 创建并初始化数据列表
/// </summary>
/// <param name="Field_Count">列的个数</param>
/// <returns>数据列表</returns>
/// 根据读到的表字段名称,创建列名
private DataTable BuildAndInitDataTable(int Field_Count, ref OleDbDataReader reader)
{
DataTable dt_tmp = null;
DataColumn dc = null;
int i = 0;
if (Field_Count <= 0)
{
return null;
}
dt_tmp = new DataTable();
for (i = 0; i < Field_Count; i)
{
dc = new DataColumn(reader.GetName(i));
dt_tmp.Columns.Add(dc);
}
return dt_tmp;
}
/// <summary>
/// 从数据库里面获取数据
/// </summary>
/// <param name="strSql">查询语句</param>
/// <returns>数据列表</returns>
public DataTable GetDataTableFromDB(string strSql)
{
if (conn_str == null)
{
return null;
}
try
{
ole_connection.Open();//打开连接
if (ole_connection.State == ConnectionState.Closed)
{
return null;
}
ole_command.CommandText = strSql;
ole_command.Connection = ole_connection;
ole_reader = ole_command.ExecuteReader(CommandBehavior.Default);
dt = ConvertOleDbReaderToDataTable(ref ole_reader);
ole_reader.Close();
ole_reader.Dispose();
}
catch (System.Exception e)
{
//Console.WriteLine(e.ToString());
MessageBox.Show("A1:" e.Message);
}
finally
{
if (ole_connection.State != ConnectionState.Closed)
{
ole_connection.Close();
}
}
return dt;
}
/// <summary>
/// 执行sql语句
/// </summary>
/// <param name="strSql">sql语句</param>
/// <returns>返回结果</returns>
public int ExcuteSql(string strSql)
{
int nResult = 0;
try
{
ole_connection.Open();//打开数据库连接
if (ole_connection.State == ConnectionState.Closed)
{
return nResult;
}
ole_command.Connection = ole_connection;
ole_command.CommandText = strSql;
nResult = ole_command.ExecuteNonQuery();
}
catch (System.Exception e)
{
//Console.WriteLine(e.ToString());
MessageBox.Show("A2:" e.Message);
return nResult;
}
finally
{
if (ole_connection.State != ConnectionState.Closed)
{
ole_connection.Close();
}
}
return nResult;
}
/// <summary>
/// 从数据库里面获取数据
/// </summary>
/// <param name="strSql">查询语句</param>
/// <returns>数据列表</returns>
public object ScalarTableFromDB(string strSql)
{
object nResult = 0;
try
{
ole_connection.Open();//打开数据库连接
if (ole_connection.State == ConnectionState.Closed)
{
return nResult;
}
ole_command.Connection = ole_connection;
ole_command.CommandText = strSql;
nResult = ole_command.ExecuteScalar();
}
catch (System.Exception e)
{
//Console.WriteLine(e.ToString());
MessageBox.Show("A3:" e.Message);
return nResult;
}
finally
{
if (ole_connection.State != ConnectionState.Closed)
{
ole_connection.Close();
}
}
return nResult;
}
/// <summary>
/// 查询数据库是否存在某条记录
/// </summary>
/// P_str_Process进程名称
public Boolean InquiryRecordFromAccess(string tablename, string fieldname, string inquiryvaluename)//关闭进程
{
string sql = "Select * from " tablename " where " fieldname " ='" inquiryvaluename "'";
object ret = ScalarTableFromDB(sql);
if (object.Equals(ret, null))
{
return false;
}
else
{
return true;
}
}
/// <summary>
/// 关闭指定的进程
/// </summary>
/// P_str_Process进程名称
private void CloseProcess(string P_str_Process)//关闭进程
{
System.Diagnostics.Process[] excelProcess = System.Diagnostics.Process.GetProcessesByName(P_str_Process);//实例化进程对象
foreach (System.Diagnostics.Process p in excelProcess)
p.Kill();//关闭进程
System.Threading.Thread.Sleep(10);//使线程休眠10毫秒
}
}
chart设置代码
private void chartdisplay(DataTable data)
{
chart1.DataSource = data;
//设置图表背景
this.chart1.BorderSkin.SkinStyle = BorderSkinStyle.FrameThin1;//图标边框外观样式
this.chart1.BorderSkin.BackColor = Color.Blue;//图标背景颜色
this.chart1.Width = 1200;//图表的宽度
this.chart1.Height = 520;//图表的高度
//设置图表区
this.chart1.ChartAreas[0].BackGradientStyle = GradientStyle.None;//背景渐变样式设置
this.chart1.ChartAreas[0].ShadowColor = Color.Gainsboro;//背景渐变色设置
this.chart1.ChartAreas[0].ShadowOffset = 1;//设置阴影的偏移量
this.chart1.ChartAreas[0].BorderDashStyle = ChartDashStyle.Solid;//边框的样式设置
this.chart1.ChartAreas[0].BorderColor = Color.Black;//边框的颜色设置
this.chart1.ChartAreas[0].BackColor = Color.Black;//数据区域背景颜色设置
//X轴网格线设置
this.chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.White;//X轴网格线颜色
this.chart1.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dash;//X轴网格线类型
this.chart1.ChartAreas[0].AxisX.MajorGrid.LineWidth = 1;//X轴网格线宽度
this.chart1.ChartAreas[0].AxisX.MajorGrid.IntervalOffset = 1; //网格线距X轴的偏移量
this.chart1.ChartAreas[0].AxisX.MajorGrid.Interval = 1; //网格X间距
this.chart1.ChartAreas[0].AxisX.ArrowStyle = AxisArrowStyle.Lines; //X轴箭头样式;
this.chart1.ChartAreas[0].AxisX.Interval = 1;
this.chart1.ChartAreas[0].AxisX.IntervalOffset = 0;
this.chart1.ChartAreas[0].AxisX.LabelStyle.Angle = -30; //设置X轴标签角度
//X轴标题设置
this.chart1.ChartAreas[0].AxisX.TitleAlignment = StringAlignment.Far;
this.chart1.ChartAreas[0].AxisX.Title = "测试时间";//X轴标题
//Y轴网格线设置
this.chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.White;//Y轴网格线颜色
this.chart1.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash;//Y轴网格线类型
this.chart1.ChartAreas[0].AxisY.MajorGrid.LineWidth = 1;//Y轴网格线宽度
this.chart1.ChartAreas[0].AxisY.MajorGrid.IntervalOffset = 20; //网格线距Y轴的偏移量
this.chart1.ChartAreas[0].AxisY.MajorGrid.Interval = 20; //网格Y间距
this.chart1.ChartAreas[0].AxisY.ArrowStyle = AxisArrowStyle.Lines; //Y轴箭头样式;
this.chart1.ChartAreas[0].AxisY.Minimum = 0;//Y轴最小显示值
this.chart1.ChartAreas[0].AxisY.Maximum = 100;//Y值最显示值
this.chart1.ChartAreas[0].AxisY.Interval = 20; //Y轴的间距
this.chart1.ChartAreas[0].AxisY.IntervalOffset = 0; //Y轴的坐标偏移量
//Y轴标题设置
this.chart1.ChartAreas[0].AxisY.TitleAlignment = StringAlignment.Far;
this.chart1.ChartAreas[0].AxisY.Title = "压力值";//X轴标题
//图表3D设置
//chart1.ChartAreas[0].Area3DStyle.Enable3D = false;
//chart1.ChartAreas[0].Area3DStyle.LightStyle = LightStyle.Realistic;
//滚动条设置
this.chart1.ChartAreas[0].CursorX.AutoScroll = true;
this.chart1.ChartAreas[0].CursorX.IsUserEnabled = true;
this.chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
this.chart1.ChartAreas[0].AxisX.ScrollBar.Enabled = true;
this.chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
this.chart1.ChartAreas[0].AxisX.ScaleView.Position = 1;
this.chart1.ChartAreas[0].AxisX.ScaleView.Size = 30; //设置滚动条包含的数据数量,此参数默认不设置,不设置滚动不显示
this.chart1.ChartAreas[0].AxisX.ScrollBar.ButtonColor = System.Drawing.Color.Silver;
this.chart1.ChartAreas[0].AxisX.ScrollBar.LineColor = System.Drawing.Color.White;
//////////////////////Series属性设置///////////////////////////
this.chart1.Series[0].XValueType = ChartValueType.DateTime;//设置X轴数据类型为时间
this.chart1.Series[0].YValueType = ChartValueType.Double;//设置Y轴数据类型为双精度浮点数
this.chart1.Series[0].ChartType = SeriesChartType.Line; //设置图表类型为折线
this.chart1.Series[0].IsValueShownAsLabel = true; //是否在数据点上显示值
this.chart1.Series[0].MarkerStyle = MarkerStyle.Circle; // 标记数据点类型
this.chart1.Series[0].MarkerSize = 8; //标记数据点大小
this.chart1.Series[0].MarkerColor = Color.White;//标记数据点的颜色
this.chart1.Series[0].MarkerBorderColor = Color.Green;//标记数据点边框的颜色
this.chart1.Series[0].MarkerBorderWidth = 2;//标记数据点边框的宽度
this.chart1.Series[0].BorderDashStyle = ChartDashStyle.Solid;
//this.chart1.Series[0].BorderColor = Color.Yellow ;//直方图时才有
this.chart1.Series[0].LabelAngle = 45;
this.chart1.Series[0].LabelBackColor = Color.Blue;
this.chart1.Series[0].LabelBorderColor = Color.Black;
this.chart1.Series[0].LabelForeColor = Color.White;//标记数据点字体的颜色
this.chart1.Series[0].Color = Color.Red;//曲线颜色
this.chart1.Series[0].BorderWidth = 2;//曲线宽度
this.chart1.Series[0].Name = "压力采集";//曲线名称
chart1.Series[0].YValueMembers = "压力检测值";
chart1.Series[0].XValueMember = "测试时间";
chart1.Series[0].Points.Clear()
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)