在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
using System; using System.Collections.Generic; using System.Text; using System.Collections; using System.Windows.Forms; using System.Drawing; //展示端口 namespace TemperatureSytem { static class ShowPortIsOrNo { /// <summary> /// 显示可用端口,和提示 /// </summary> /// <param name="combox">显示端口</param> /// <param name="lab">提示</param> public static void FromLoadPort(ComboBox combox,Label lab) { ArrayList alPort = PortCheckAndSeting.PortSeting(); if (alPort.Count < 1) { lab.Text = "未检测到可用串口!!!"; lab.ForeColor = Color.Red; return; } else { foreach (string sPort in alPort) { combox.Items.Add(sPort); combox.Text = sPort; } } } } } //串口检测 using System; using System.Collections.Generic; using System.Text; using System.Collections; using System.IO.Ports; using System.Windows.Forms; namespace TemperatureSytem { static class PortCheckAndSeting { /// <summary> /// 加载的有串口 /// </summary> /// <returns>返回一个串口动态数组</returns> public static ArrayList PortSeting() { ArrayList alPort = new ArrayList(); foreach (string sPort in SerialPort.GetPortNames()) { alPort.Add(sPort); } return alPort; } /// <summary> /// 新串口检测设置 /// </summary> /// <param name="alOlePort"></param> /// <returns></returns> public static string NewPortSeting(ComboBox comBox) { //存在 ArrayList alOldPort = new ArrayList();//保存旧串口号的 foreach (string strOldPort in comBox.Items)//遍历旧的保存在动态数组里 { alOldPort.Add(strOldPort); } foreach (string sPort in SerialPort.GetPortNames()) { bool exists = ((IList)alOldPort).Contains(sPort); if (!exists)//在旧的串口里是否包含新检测的,不包含则返回这个新检测的 return sPort; } return ""; } } } using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace TemperatureSytem { class Sound { [DllImport("winmm.dll")] public static extern long PlaySound(String fileName, long a, long b); [DllImport("winmm.dll")] public static extern long mciSendString(string lpstrCommand, string lpstrReturnString, long length, long hwndcallback); /// <summary> /// 播放音乐文件(重复) /// </summary> /// <param name="p_FileName">音乐文件名称</param> public static void PlayMusic_Repeat(string p_FileName) { try { mciSendString(@"close temp_music", " ", 0, 0); mciSendString(@"open " + p_FileName + " alias temp_music", " ", 0, 0); mciSendString(@"play temp_music repeat", " ", 0, 0); } catch { } } /// <summary> /// 播放音乐文件 /// </summary> /// <param name="p_FileName">音乐文件名称</param> public static void PlayMusic(string p_FileName) { try { mciSendString(@"close temp_music", " ", 0, 0); mciSendString(@"open " + p_FileName + " alias temp_music", " ", 0, 0); mciSendString(@"play temp_music", " ", 0, 0); } catch { } } /// <summary> /// 停止当前音乐播放 /// </summary> /// <param name="p_FileName">音乐文件名称</param> public static void StopMusic(string p_FileName) { try { mciSendString(@"close " + p_FileName, " ", 0, 0); } catch { } } } } using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.OleDb; using System.Windows.Forms; using System.Collections; using System.Data.SqlClient; using System.Diagnostics; namespace SqlOperate { class SqlHelpers { public bool bl = true; public static string sql; public static string userName; public static string userPwd; public static string sqlDatabase; public static string strConn; public string Sql { get { return sql; } set { sql = value; } } public string UserName { get { return userName; } set { userName = value; } } public string UserPwd { get { return userPwd; } set { userPwd = value; } } public string SqlDataBase { get { return sqlDatabase; } set { sqlDatabase = value; } } public string StrrConn { get { return strConn; } set { strConn = value; } } OleDbConnection con; OleDbDataAdapter adp; //连接 public string StrConnString() { string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\data.mdb"; return strConn; } public void Conn() { // string sqlconn = "server=" + sql + ";database="+sqlDatabase+";u; con = new OleDbConnection(StrConnString()); try { con.Open(); bl = true; } catch (Exception) { bl = false; } } #region 返回DataTable /// <summary> /// 返回DataTable /// </summary> /// <param name="sql"></param> /// <returns></returns> public DataTable datatable(string sqlxx) { DataSet dataset = new DataSet(); try { adp = new OleDbDataAdapter(sqlxx, StrConnString()); adp.Fill(dataset, "table1"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } return dataset.Tables["table1"]; } #endregion #region 返回DATASET /// <summary> /// 返回DataTable /// </summary> /// <param name="sql"></param> /// <returns></returns> public DataSet dataSet(string sqlxx) { DataSet dataset = new DataSet(); try { adp = new OleDbDataAdapter(sqlxx, StrConnString()); adp.Fill(dataset, "table1"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } return dataset; } public DataSet dataSet(ArrayList sqlxx) { DataSet dataset = new DataSet(); int i = 0; try { foreach (string item in sqlxx) { adp = new OleDbDataAdapter(item, StrConnString()); adp.Fill(dataset,i.ToString()); i++; } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } return dataset; } #endregion #region 返回一个动态数组 /// <summary> /// 返回一个动态数组 /// </summary> /// <param name="sql"></param> /// <param name="ziduan"></param> /// <param name="combox"></param> public ArrayList pinyin(string sql, string ziduan) { OleDbConnection con = new OleDbConnection(StrConnString()); OleDbDataAdapter adp = new OleDbDataAdapter(sql, con); DataTable dt = new DataTable(); adp.Fill(dt); ArrayList al = new ArrayList(); for (int i = 0; i < dt.Rows.Count; i++) { DataRow dr2 = dt.Rows[i]; string username = Convert.ToString(dr2[ziduan]); bool rep = false; foreach (string str in al) { if (username == str) { rep = true; break; } } if (!rep) { al.Add(username); } } return al; } #endregion #region 普通ExecuteNonQuery /// <summary> /// 普通ExecuteNonQuery /// </summary> /// <param name="sql"></param> public void ExecuteNonQuery(string sqlxx) { try { OleDbConnection con = new OleDbConnection(StrConnString()); con.Open(); OleDbCommand cmd = new OleDbCommand(sqlxx, con); cmd.ExecuteNonQuery(); con.Close(); } catch {throw new Exception(); } } #endregion #region 普通ExecuteNonQuery /// <summary> /// 数组ExecuteNonQuery /// </summary> /// <param name="sql"></param> public void ExecuteNonQuery(ArrayList sqlxx) { try { OleDbConnection con = new OleDbConnection(StrConnString()); con.Open(); foreach (string item in sqlxx) { OleDbCommand cmd = new OleDbCommand(item, con); cmd.ExecuteNonQuery(); } con.Close(); } catch { throw new Exception(); } } #endregion #region ComboBox下拉选择 /// <summary> /// ComboBox下拉选择 /// </summary> /// <param name="sql"></param> /// <param name="ziduan"></param> /// <param name="combox"></param> public void Combox(string sql, string ziduan, ComboBox combox, int j) { combox.Items.Clear(); OleDbConnection con = new OleDbConnection(StrConnString()); OleDbDataAdapter adp = new OleDbDataAdapter(sql, con); DataTable dt = new DataTable(); adp.Fill(dt); ArrayList al = new ArrayList(); for (int i = 0; i < dt.Rows.Count; i++) { DataRow dr2 = dt.Rows[i]; string username = Convert.ToString(dr2[ziduan]); bool rep = false; foreach (string str in al) { if (username == str) { rep = true; break; } } if (!rep) { al.Add(username); combox.Items.Add(username); } } if (al.Count < 1) { combox.Text = ""; } else { if (j == 0) { combox.AutoCompleteCustomSource.AddRange((string[])al.ToArray(typeof(string))); //ArryList转为string[] combox.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest; combox.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource; } else { combox.Text = al[0].ToString(); combox.AutoCompleteCustomSource.AddRange((string[])al.ToArray(typeof(string))); //ArryList转为string[] combox.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest; combox.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource; } } } #endregion #region 返回一个动态数组 /// <summary> /// 返回一个动态数组含两个字段 /// </summary> /// <param name="sql"></param> /// <param name="ziduan"></param> /// <param name="combox"></param> public ArrayList TwoZiduan(string sql, string ziduan, string ziduan2) { OleDbConnection con = new OleDbConnection(StrConnString()); OleDbDataAdapter adp = new OleDbDataAdapter(sql, con); DataTable dt = new DataTable(); adp.Fill(dt); ArrayList al = new ArrayList(); for (int i = 0; i < dt.Rows.Count; i++) { DataRow dr2 = dt.Rows[i]; string username = Convert.ToString(dr2[ziduan]); string phonenumber = Convert.ToString(dr2[ziduan2]); string xx = username + "--" + phonenumber; bool rep = false; foreach (string str in al) { if (xx == str) { rep = true; break; } } if (!rep) { al.Add(xx); } } return al; } #endregion #region 提取一个字段int /// <summary> /// 提取一个字段int /// </summary> /// <param name="sql"></param> /// <param name="ziduan"></param> /// <returns>int</returns> public int ReturnValue(string sql, string ziduan) { int IdValude = 0; OleDbConnection con = new OleDbConnection(StrConnString()); OleDbDataAdapter adp = new OleDbDataAdapter(sql, con); DataTable dt = new DataTable(); adp.Fill(dt); if (dt.Rows.Count > 0) { DataRow dr2 = dt.Rows[0]; IdValude = Convert.ToInt32(dr2[ziduan]); return IdValude; } else { IdValude = -1; return IdValude; } } #endregion #region 提取一个字段string //提取一个字段string public string TextBox(string sql, string ziduan) { try { OleDbConnection con = new OleDbConnection(StrConnString()); OleDbDataAdapter adp = new OleDbDataAdapter(sql, con); DataTable dt = new DataTable(); adp.Fill(dt); if (dt.Rows.Count > 0) { DataRow dr2 = dt.Rows[0]; return Convert.ToString(dr2[1]); } else { return ""; } } catch (Exception ex) { MessageBox.Show(ex.Message); return ""; } } #endregion #region dataGridView美化 /// <summary> /// dataGridView美化 /// </summary> /// <param name="dataGridView"></param> public void dataGridView(DataGridView dataGridView) { System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle(); dataGridView.AllowUserToAddRows = false; dataGridView.AllowUserToDeleteRows = false; dataGridViewCellStyle1.BackColor = System.Drawing.Color.LightCyan; dataGridView.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1; dataGridView.BackgroundColor = System.Drawing.Color.White; dataGridView.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; dataGridView.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single; dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;//211, 223, 240 dataGridViewCellStyle2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(211)))), ((int)(((byte)(223)))), ((int)(((byte)(240))))); dataGridViewCellStyle2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); dataGridViewCellStyle2.ForeColor = System.Drawing.Color.Navy; dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight; dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText; dataGridView.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle2; dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; dataGridView.EnableHeadersVisualStyles = false; dataGridView.GridColor = System.Drawing.SystemColors.GradientInactiveCaption; dataGridView.ReadOnly = false ; dataGridView.RowHeadersVisible = false; dataGridView.RowTemplate.Height = 23; dataGridView.RowTemplate.ReadOnly = false ; dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; } #endregion private void pilian(string tableName,string[] aa,string[] bb) { Stopwatch sw = new Stopwatch(); OleDbConnection con = new OleDbConnection(StrConnString()); OleDbCommand sqlComm = new OleDbCommand(); sqlComm.CommandText = string.Format("insert into sz_tb (UserName,Pwd)values(@p0,@p1,@p2)");//参数化SQL sqlComm.Parameters.Add("@p0", SqlDbType.Int); sqlComm.Parameters.Add("@p1", SqlDbType.NVarChar); sqlComm.Parameters.Add("@p2", SqlDbType.VarChar); sqlComm.CommandType = CommandType.Text; sqlComm.Connection = con; con.Open(); try { //循环插入100万条数据,每次插入10万条,插入10次。 for (int multiply = 0; multiply < 10; multiply++) { for (int count = multiply * 100000; count < (multiply + 1) * 100000; count++) { sqlComm.Parameters["@p0"].Value = count; sqlComm.Parameters["@p1"].Value = string.Format("User-{0}", count * multiply); sqlComm.Parameters["@p2"].Value = string.Format("Pwd-{0}", count * multiply); sw.Start(); sqlComm.ExecuteNonQuery(); sw.Stop(); } //每插入10万条数据后,显示此次插入所用时间 // Console.WriteLine(string.Format("Elapsed Time is {0} Milliseconds", sw.ElapsedMilliseconds)); } } catch (Exception ex) { throw ex; } finally { con.Close(); } Console.ReadLine(); } } } 全部评论
专题导读
上一篇:TaskVision(C#版)在Win2003上完美安装(说明:访问的库是本地的) ...发布时间:2022-07-10下一篇:Win10系列:C#应用控件进阶7发布时间:2022-07-10热门推荐
热门话题
阅读排行榜
|
请发表评论