DataBase.cs
上传用户:ruibang818
上传日期:2022-07-12
资源大小:3k
文件大小:20k
源码类别:

数据库编程

开发平台:

Visual C++

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Data.SqlClient;
  7. using System.Data;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. namespace ABP
  11. {
  12.     class DataBase
  13.     {
  14.         public DataBase()
  15.         {
  16.             objcommand.Connection = objConnection;
  17.             objcommand.Connection = objConnection;
  18.         }
  19.         #region defining components
  20.         /******************  defining components and global variables**********************/
  21.         SqlConnection objConnection = new SqlConnection("Password=0000;Persist Security Info=True;User ID=far;Initial Catalog=mytest;Data Source=FARKISH");
  22.         SqlCommand objcommand = new SqlCommand();
  23.         // public and private string temp;
  24.         private SqlDataReader DataReaderInstance;
  25.         private bool Connection_Fail_Flag = false;
  26.         private string[] Main_Tbl_Fields = { "Device_Address", "Device_Name", "Device_Picture", "Auto_Manual", 
  27.                                              "Overal_Trip", "Delay_Shutdown", "Relay_Output", "Reset_Alarm", 
  28.                                              "Signal_Source", "Sensor_Scale", "Velo_Acc", "Adj_Change", 
  29.                                              "RMS_Value", "Date_Time" };
  30.         private string[] Main_Tbl_Fields_Types = { "tinyint", "nchar(30)", "image", "bit", 
  31.                                                    "int", "int", "bit", "bit", 
  32.                                                    "bit", "int", "bit", "bit", 
  33.                                                    "smallint", "smalldatetime" };
  34.         #endregion
  35.         #region properties & methods
  36.         /******************  defining properties and methods**********************/
  37.         public bool ConnectionOpen()                        //open the connection to the DataBase
  38.         {
  39.             try
  40.             {
  41.                 objConnection.Open();
  42.                 return true;
  43.             }
  44.             catch { return false; }
  45.         }
  46.         public bool ConnectionClose()                       //close the connection to the DataBase
  47.         {
  48.             try
  49.             {
  50.                 objConnection.Close();
  51.                 return true;
  52.             }
  53.             catch { return false; }
  54.         }
  55.         public bool Connection_Fail
  56.         {
  57.             get { return Connection_Fail_Flag; }
  58.             set
  59.             {
  60.                 if (value == false)
  61.                     Connection_Fail_Flag = value;
  62.             }
  63.         }
  64.         #endregion
  65.         #region functions
  66.         /*******************************  functions  ********************************/
  67.       
  68.         /// <summary>
  69.         /// if the tables exist the function return false
  70.         /// if the tables was not exist , the function create talbes and return true
  71.         /// if the tables was not exist , and the function cant creat them , return true and connection_fail_Flag=true
  72.         /// if can not connect to data base return false and connection_fail_Flag=true
  73.         /// </summary>
  74.         /// <param name="name"></param>
  75.         /// <returns></returns>
  76.         public bool Create_Table(string name)
  77.         {
  78.             try
  79.             {
  80.                 objConnection.Open();
  81.                 try                         //use for cheaking the tables have been bielt or no
  82.                 {
  83.                     objcommand.CommandText = "SELECT * FROM Main_Table";
  84.                     DataReaderInstance = objcommand.ExecuteReader();
  85.                     return false;
  86.                 }
  87.                 catch
  88.                 {
  89.                     try
  90.                     {
  91.                         objcommand.CommandText = "create table" + Main_Tbl_Fields[0] + " " + Main_Tbl_Fields_Types[0] + " , " + Main_Tbl_Fields[1] + " " + Main_Tbl_Fields_Types[1] + " , "
  92.                                                  + Main_Tbl_Fields[2] + " " + Main_Tbl_Fields_Types[2] + " , " + Main_Tbl_Fields[3] + " " + Main_Tbl_Fields_Types[3] + " , "
  93.                                                  + Main_Tbl_Fields[4] + " " + Main_Tbl_Fields_Types[4] + " , " + Main_Tbl_Fields[5] + " " + Main_Tbl_Fields_Types[5] + " , "
  94.                                                  + Main_Tbl_Fields[6] + " " + Main_Tbl_Fields_Types[6] + " , " + Main_Tbl_Fields[7] + " " + Main_Tbl_Fields_Types[7] + " , "
  95.                                                  + Main_Tbl_Fields[8] + " " + Main_Tbl_Fields_Types[8] + " , " + Main_Tbl_Fields[9] + " " + Main_Tbl_Fields_Types[9] + " , "
  96.                                                  + Main_Tbl_Fields[10] + " " + Main_Tbl_Fields_Types[10] + " , " + Main_Tbl_Fields[11] + " " + Main_Tbl_Fields_Types[11] + " , "
  97.                                                  + Main_Tbl_Fields[12] + " " + Main_Tbl_Fields_Types[12] + " , " + Main_Tbl_Fields[13] + " " + Main_Tbl_Fields_Types[13];
  98.                         objcommand.ExecuteNonQuery();
  99.                         for (byte i = 2; i <= 255; i++)
  100.                         {
  101.                             objcommand.CommandText = "create table T" + i.ToString() + " Date_Time SmallDateTim  , RMS_Value smallint";
  102.                             objcommand.ExecuteNonQuery();
  103.                         }
  104.                     }
  105.                     catch
  106.                     {
  107.                         Connection_Fail_Flag = true;
  108.                     }
  109.                 }
  110.                 objConnection.Close();
  111.                 return true;
  112.             }
  113.             catch
  114.             {
  115.                 try { objConnection.Close(); }
  116.                 catch { }
  117.                 Connection_Fail_Flag = true;
  118.                 return false;
  119.             }
  120.         }
  121.       
  122.         /// <summary>
  123.         /// reads all fields in main table exept Image fild
  124.         /// if cant read them connection_fail_Flag = true;
  125.         /// </summary>
  126.         /// <returns></returns>
  127.         public string[,] Main_Table_Read()
  128.         {
  129.             byte Row_Nmb = 0;
  130.             byte i = 0;
  131.             string[,] OutPut_Array;
  132.             try
  133.             {
  134.                 objConnection.Open();
  135.                 objcommand.CommandText = "SELECT * FROM Main_Table";
  136.                 DataReaderInstance = objcommand.ExecuteReader();
  137.                 while (DataReaderInstance.Read())
  138.                     Row_Nmb++;
  139.                 objcommand.CommandText = "SELECT * FROM Main_Table";
  140.                 DataReaderInstance = objcommand.ExecuteReader();
  141.                 OutPut_Array = new string[Row_Nmb, Main_Tbl_Fields.Length-1];
  142.                 while (DataReaderInstance.Read())
  143.                 {
  144.                     for (byte j = 0; j < Main_Tbl_Fields.Length; j++)
  145.                     {
  146.                         if (j != (byte)(2))                                //because field 2 contain image
  147.                             OutPut_Array[i, j] = Convert.ToString(DataReaderInstance[Main_Tbl_Fields[j]]);
  148.                         else
  149.                             OutPut_Array[i, 2] = "NotRead";
  150.                     }
  151.                     
  152.                     i++;
  153.                 }
  154.                 return OutPut_Array;
  155.             }
  156.             catch
  157.             {
  158.                 try { objConnection.Close(); }
  159.                 catch { }
  160.                 Connection_Fail_Flag = true;
  161.                 string[,] Error_OutPut_Array = null;
  162.                 return Error_OutPut_Array;
  163.             }
  164.         }
  165.         /// <summary>
  166.         /// writes data in hte specific tacles and update 
  167.         /// value and date_time in the main table
  168.         /// if can do this nothing happend
  169.         /// else Connection_Fail_Flag will be true
  170.         /// </summary>
  171.         /// <param name="Input_Array"></param>
  172.         public void Write_Data(string[,] Input_Array)
  173.         {
  174.             try
  175.             {
  176.                 objConnection.Open();
  177.                 for (byte i = 0; i < Input_Array.GetLength(0); i++)
  178.                 {
  179.                     objcommand.Parameters.AddWithValue("@RmsValue", Convert.ToInt16(Input_Array[i, 1]));
  180.                     objcommand.Parameters.AddWithValue("@DateTime", Convert.ToDateTime(Input_Array[i, 2]));
  181.                     objcommand.CommandText = "UPDATE Main_Table SET " + Main_Tbl_Fields[12] + " = @RmsValue , "
  182.                                               + Main_Tbl_Fields[13] + " = @DateTime  WHERE "
  183.                                               + Main_Tbl_Fields[0] + " = " + Input_Array[i, 0];
  184.                     objcommand.ExecuteNonQuery();
  185.                     objcommand.CommandText = "INSERT INTO " + Input_Array[i, 0] +
  186.                                              " (" + Main_Tbl_Fields[13] + " , " + Main_Tbl_Fields[12] +
  187.                                              ") VALUES(@DateTime, @RmsValue)";
  188.                     objcommand.ExecuteNonQuery();
  189.                     objcommand.Parameters.RemoveAt("@RmsValue");
  190.                     objcommand.Parameters.RemoveAt("@DateTime");
  191.                 }
  192.                 objConnection.Close();
  193.             }
  194.             catch 
  195.             {
  196.                 try { objConnection.Close(); }
  197.                 catch { }
  198.                 Connection_Fail_Flag = true;
  199.             }
  200.         }
  201.         /// <summary>
  202.         /// wite image on the specific address
  203.         /// if can do this nothing happend
  204.         /// else Connection_Fail_Flag will be true
  205.         /// </summary>
  206.         /// <param name="?"></param>
  207.         public void Image_Write(string Address, Image Temp_Image)
  208.         {
  209.             try
  210.             {
  211.                 MemoryStream Obj_MemoryStream = new MemoryStream();
  212.                 Temp_Image.Save(Obj_MemoryStream, Temp_Image.RawFormat);
  213.                 byte[] Array_Image = new byte[5000000];
  214.                 Array_Image = Obj_MemoryStream.GetBuffer();
  215.                 objConnection.Open();
  216.                 objcommand.Parameters.Add("@picture_file", SqlDbType.Image).Value = Array_Image;
  217.                 objcommand.Parameters.AddWithValue("@Address", Convert.ToByte(Address));
  218.                 objcommand.CommandText = "UPDATE Main_Table SET " + Main_Tbl_Fields[2] +
  219.                                           " = @picture_file WHERE" + Main_Tbl_Fields[0] + " = @Address";
  220.                 objcommand.ExecuteNonQuery();
  221.                 objcommand.Parameters.RemoveAt("@picture_file");
  222.                 objcommand.Parameters.RemoveAt("@Address");
  223.                 objConnection.Close();
  224.             }
  225.             catch
  226.             {
  227.                 try { objConnection.Close(); }
  228.                 catch { }
  229.                 Connection_Fail_Flag = true;
  230.             }
  231.             
  232.         }
  233.         /// <summary>
  234.         /// read image from specific address
  235.         /// if can do this nothing happend
  236.         /// else Connection_Fail_Flag will be true
  237.         /// </summary>
  238.         /// <param name="?"></param>
  239.         public Image Read_Image(string Address)
  240.         {
  241.             try
  242.             {
  243.                 Image Temp_Image;
  244.                 objConnection.Open();
  245.                 objcommand.Parameters.AddWithValue("@Address", Convert.ToByte(Address));
  246.                 objcommand.CommandText = "SELECT " + Main_Tbl_Fields[2] + "FROM Main_Table"
  247.                                          + " WHERE" + Main_Tbl_Fields[0] + " = @Address";
  248.                 DataReaderInstance = objcommand.ExecuteReader();
  249.                 DataReaderInstance.Read();
  250.                 objcommand.Parameters.RemoveAt("@Address");
  251.                 byte[] Array_Image = (byte[])(DataReaderInstance[Main_Tbl_Fields[2]]);
  252.                 MemoryStream Obj_MemoryStream = new MemoryStream(Array_Image);
  253.                 Temp_Image = Image.FromStream(Obj_MemoryStream);
  254.                 objConnection.Close();
  255.                 return Temp_Image;
  256.             }
  257.             catch
  258.             {
  259.                 try { objConnection.Close(); }
  260.                 catch { }                
  261.                 Connection_Fail_Flag = true;
  262.                 Image Error_Image = null;
  263.                 return Error_Image;
  264.             }
  265.         }
  266.         /// <summary>
  267.         /// return rms and date_time between start and end
  268.         /// if cant retrive data connection_fail_flag will be TRUE
  269.         /// </summary>
  270.         /// <param name="Address"></param>
  271.         /// <param name="Start"></param>
  272.         /// <param name="End"></param>
  273.         /// <returns></returns>
  274.         public string[,] Search_Data(string Address, DateTime Start, DateTime End)
  275.         {
  276.             string[,] Output_Array;
  277.             int Data_Count = 0;
  278.             int i = 0;
  279.             try
  280.             {
  281.                 objConnection.Open();
  282.                 objcommand.Parameters.AddWithValue("@Start", Start);
  283.                 objcommand.Parameters.AddWithValue("@End", End);
  284.                 objcommand.CommandText = "SELECT * FROM T" + Address + " WHERE DateAndTime between @Start and @End";
  285.                 DataReaderInstance = objcommand.ExecuteReader();
  286.                 while (DataReaderInstance.Read())
  287.                     Data_Count++;
  288.                 Output_Array = new string[Data_Count, 2];
  289.                 DataReaderInstance = objcommand.ExecuteReader();
  290.                 while (DataReaderInstance.Read())
  291.                 {
  292.                     Output_Array[i, 0] = Convert.ToString(DataReaderInstance["Date_Time"]);
  293.                     Output_Array[i, 1] = Convert.ToString(DataReaderInstance["RMS_Value"]);
  294.                     i++;
  295.                 }
  296.                 objcommand.Parameters.RemoveAt("@Start");
  297.                 objcommand.Parameters.RemoveAt("@End");
  298.                 objConnection.Close();
  299.                 return Output_Array;
  300.             }
  301.             catch
  302.             {
  303.                 try { objConnection.Close(); }
  304.                 catch { }
  305.                 Connection_Fail_Flag = true;
  306.                 string[,] Error_Array =null;
  307.                 return Error_Array;
  308.             }
  309.         }
  310.         /// <summary>
  311.         /// write adjusts to database , if  adjust exist update them
  312.         /// else create new line in Main_Table with Adjust values
  313.         /// if cant do them , Conncetion_Fail_Flag will be true
  314.         /// </summary>
  315.         /// <param name="Adj_Values"></param>
  316.         public void Write_Adjust(string[] Adj_Values)
  317.         {
  318.             try
  319.             {
  320.                 objConnection.Open();
  321.                 objcommand.Parameters.AddWithValue("@Device_Address", Convert.ToByte(Adj_Values[0]));
  322.                 objcommand.Parameters.AddWithValue("@Device_Name", Convert.ToString(Adj_Values[1]));
  323.                 //adj_Values[2] not read because is image
  324.                 objcommand.Parameters.AddWithValue("@Auto_Manual", Convert.ToBoolean(Adj_Values[3]));
  325.                 objcommand.Parameters.AddWithValue("@Overal_Trip", Convert.ToInt32(Adj_Values[4]));
  326.                 objcommand.Parameters.AddWithValue("@Delay_Shutdown", Convert.ToInt32(Adj_Values[5]));
  327.                 objcommand.Parameters.AddWithValue("@Relay_Output", Convert.ToBoolean(Adj_Values[6]));
  328.                 objcommand.Parameters.AddWithValue("@Reset_Alarm", Convert.ToBoolean(Adj_Values[7]));
  329.                 objcommand.Parameters.AddWithValue("@Signal_Source", Convert.ToBoolean(Adj_Values[8]));
  330.                 objcommand.Parameters.AddWithValue("@Sensor_Scale", Convert.ToInt32(Adj_Values[9]));
  331.                 objcommand.Parameters.AddWithValue("@Velo_Acc", Convert.ToBoolean(Adj_Values[10]));
  332.                 objcommand.Parameters.AddWithValue("@Adj_Change", Convert.ToBoolean(Adj_Values[11]));
  333.                 objcommand.CommandText = "SELECT * FROM Main_Table WHERE " + Main_Tbl_Fields[0] + " = Device_Address";
  334.                 DataReaderInstance = objcommand.ExecuteReader();   //the the addres exist , it will be update , else it will be create
  335.                 if (DataReaderInstance.Read() == true)
  336.                 {
  337.                     objcommand.CommandText = "UPDATE " + Adj_Values[0] + " SET " + Main_Tbl_Fields[0] + " = @Device_Address , " + Main_Tbl_Fields[1] + " = @Device_Name , "
  338.                                              + Main_Tbl_Fields[3] + " = @Auto_Manual , " + Main_Tbl_Fields[4] + " = @Overal_Trip , " + Main_Tbl_Fields[5] + " = @Delay_Shutdown , "
  339.                                              + Main_Tbl_Fields[6] + " = @Relay_Output , " + Main_Tbl_Fields[7] + " = @Reset_Alarm , " + Main_Tbl_Fields[8] + " = @Signal_Source , "
  340.                                              + Main_Tbl_Fields[9] + " = @Sensor_Scale , " + Main_Tbl_Fields[10] + " = @Velo_Acc , " + Main_Tbl_Fields[11] + " = @Adj_Change , "
  341.                                               + " WHERE " + Main_Tbl_Fields[0] + " = @Device_Address";
  342.                     objcommand.ExecuteNonQuery();
  343.                 }
  344.                 else
  345.                 {
  346.                     objcommand.CommandText = "INSERT INTO Main_Table ( " + Main_Tbl_Fields[0] + " , " + Main_Tbl_Fields[1] + " , " + Main_Tbl_Fields[3] + " , "
  347.                                               + Main_Tbl_Fields[4] + " , " + Main_Tbl_Fields[5] + " , " + Main_Tbl_Fields[6] + " , " + Main_Tbl_Fields[7] + " , "
  348.                                               + Main_Tbl_Fields[8] + " , " + Main_Tbl_Fields[9] + " , " + Main_Tbl_Fields[10] + " , " + Main_Tbl_Fields[11] + " )"
  349.                                               + "VALUES @Device_Address, @Device_Name, @Auto_Manual, @Overal_Trip, @Delay_Shutdown, @Relay_Output, "
  350.                                               + "@Reset_Alarm, @Signal_Source, @Sensor_Scale, @Velo_Acc, @Adj_Change";
  351.                     objcommand.ExecuteNonQuery();
  352.                 }
  353.                 objcommand.Parameters.RemoveAt("@Device_Address");
  354.                 objcommand.Parameters.RemoveAt("@Device_Name");
  355.                 objcommand.Parameters.RemoveAt("@Auto_Manual");
  356.                 objcommand.Parameters.RemoveAt("@Overal_Trip");
  357.                 objcommand.Parameters.RemoveAt("@Delay_Shutdown");
  358.                 objcommand.Parameters.RemoveAt("@Relay_Output");
  359.                 objcommand.Parameters.RemoveAt("@Reset_Alarm");
  360.                 objcommand.Parameters.RemoveAt("@Signal_Source");
  361.                 objcommand.Parameters.RemoveAt("@Sensor_Scale");
  362.                 objcommand.Parameters.RemoveAt("@Velo_Acc");
  363.                 objcommand.Parameters.RemoveAt("@Adj_Change");
  364.                 objConnection.Close();
  365.             }
  366.             catch
  367.             {
  368.                 try { objConnection.Close(); }
  369.                 catch { }
  370.                 Connection_Fail_Flag = true;
  371.             }
  372.         }
  373.         /// <summary>
  374.         /// it is just for test and will be delete finaly
  375.         /// </summary>
  376.         public void test()             
  377.         {
  378.             try
  379.             {
  380.                 objConnection.Open();
  381.                 objcommand.Parameters.AddWithValue("@value", Convert.ToInt32(10));
  382.                 objcommand.CommandText = "SELECT * FROM arash1 WHERE value = @value";
  383.                 DataReaderInstance = objcommand.ExecuteReader();
  384.                 DataReaderInstance.Read();
  385.                 DataReaderInstance.Read();
  386.                 DataReaderInstance.Read();
  387.                 // if (DataReaderInstance.Read() == false)
  388.                 //   MessageBox.Show("AAAA");
  389.               //  objcommand.Parameters.RemoveAt("@value");
  390.                 objConnection.Close();
  391.             }
  392.             catch 
  393.             {
  394.                 objcommand.Parameters.AddWithValue("@value", Convert.ToInt32(12));
  395.                 objcommand.Parameters.AddWithValue("@value", Convert.ToInt32(13));
  396.                 objConnection.Close();
  397.             }
  398.         }
  399.         #endregion
  400.     }
  401. }