c#總結

来源:互联网 发布:碘伏 碘酒 知乎 编辑:程序博客网 时间:2024/04/30 01:02

頁面的基本組成

<HTML>

        <HEAD>

                <TITLE>--Foxconn ORT Test Record --</TITLE>

                <meta http-equiv="Content-Type" content="text/html; charset=big5">

                <LINK href="css/fox_Li.css" type="text/css" rel="stylesheet">

                        <SCRIPT language="JAVASCRIPT">

        if (top.location != self.location){

                top.location=self.location;

        }

                        </SCRIPT>

        </HEAD>

        <frameset id="test" border="0" name="forum" frameSpacing="0" borderColor="white" rows="90,0,*"

                frameBorder="0" scrolling="no" noresize>

                <frame name="top" src="head.aspx" noResize scrolling="no">

                <frame name="hidden" src="left.aspx" noResize scrolling="no">

                <frameset id="frmMain" border="0" name="content" frameSpacing="0" frameBorder="0" cols="205,10,*"

                        scrolling="no" noresize>

                        <frame name="MenuTree" src="left.aspx" noResize scrolling="no" target="BoardMenu">

                        <FRAME name="Set" src="SetFrame.aspx" scrolling="no">

                        <frame name="Main" src="error.aspx" scrolling="yes">

                </frameset>

        </frameset>

</HTML>

基本語法﹕

數組的定義﹕

string[] Fox_Array;//定義數組

Fox_Array = new string[ds_count];//為數組分配空間

Fox_Array[i] = du.Tables[0].Rows[i]["Foxconn"].ToString();//給數據賦值

Session 的定義﹕

Session["get_date"]

循環語句﹕

for( i = 0; i < du.Tables[0].Rows.Count; i++)

{}

得到與今天相差一天的值

System.DateTime.Now.AddDays(-1).ToString("d");

綁定DataGrid

string strSQL;

strSQL=select * from aa;

SqlDataAdapter da = new SqlDataAdapter(strSQL,Conn);

DataSet ds = new DataSet();

     da.Fill(ds);              

     DG_R.DataSource = ds.Tables[0].DefaultView;

     DG_R.DataBind();

打開新的窗口

string str_script1  = "<script>window.open('OverLook.aspx','','')</script>";

     RegisterStartupScript("", str_script1);

針對DataGrid的Command

private void DG_R_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)

           {

                Session["con_a"]=e.Item.Cells[0].Text;              

                switch (e.CommandName)

                {

                case "OverLook":

                string str_script1  = "<script>window.open('OverLook.aspx','','')</script>";

                           RegisterStartupScript("", str_script1);

                                break;

                     case "Detail":

                           string str_script  = "<script>window.open('Detail.aspx','','')</script>";

                           RegisterStartupScript("", str_script);

                           break;                    

                }

           }

警告語句﹕

RegisterStartupScript(" ", "<script>alert('Please input Foxconn or Custormer Model Name,SN or Date From !')</script>");

判斷語句﹕

if (tb_fox.Text != "")

           {

                strSQL=strSQL+" and foxconn='" + tb_fox.Text.Trim() + "'"  ;         

           }

 

一些有用的函數﹕

得到含年月日時分秒的字符串﹕

DateTime.Now.ToString("yyyyMMddHHmmss");

 

 

 

與數據庫連接

web config文件<configuration>下添加

SQL連接

<appSettings>

<add key=" Connect" value="server=10.191.23.18;uid=qe;pwd=qe123!;database=ort;Max Pool Size=10;"/>

<add key="Connect" value="Server=10.191.23.18;Userid=qe;Pwd=qe123!;Database=ort"></add>

</appSettings>

Oracle連接

<appSettings>

    <add key="ConnectionString" value="Provider=MSDAORA.1;User ID=dbs;Password=dbs;Data Source=DBMSODB"></add>

    </appSettings>

 

如何連接數據庫﹕

在頁面添加﹕

using System.Data.SqlClient;

protected SqlConnection Conn;//定義連接字符串

Conn = new SqlConnection();//創建對象﹐內存分配空間。

Conn.ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["Connect"];

//連接到數據庫

Conn.Open();    //打開數據庫              

…….//執行操作

Conn.Close();//關閉數據庫

 

 

執行存儲過程

Conn = new SqlConnection();                          Conn.ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["DB_Default"];

     Conn.Open();        

strSQL="exec spTest '"+Session["con_a"].ToString() +"','"+ Session["con_b"].ToString()+"','"+ Session["con_d"].ToString()+"','"+Session["con_c"].ToString()+"','"+ Session["get_date"] +"'" ;

     SqlCommand da = new SqlCommand(strSQL,Conn);

     da.ExecuteNonQuery();

讀取數據庫中的數據的值

public void get_value()

           {

                try

                {

                     string strSQL = "select Foxconn,Customer,sn,convert(varchar(10) ,test_date,111) as test_date,avg_tx,avg_rx,avg_rate,avg_tmp,testTime,avg_send,avg_receive,avg_hum from  temp_Thruput_data where Foxconn='"+ Session["con_b"].ToString() +"' and Customer='"+ Session["con_a"].ToString() +"' and sn='"+ Session["con_c"].ToString() +"'  and datediff(day,Test_Date,'" + Session["con_d"] + "')<=1  order by test_date,testTime";      

                     SqlDataAdapter dk = new SqlDataAdapter(strSQL,Conn);           

                     DataSet du = new DataSet();         

                     dk.Fill(du);   

                     ds_count=du.Tables[0].Rows.Count;

                     Fox_Array = new string[ds_count];

                     Cus_Array = new string[ds_count];

                     SN_Array = new string[ds_count];

                     TD_Array = new string[ds_count];

                     Tmp_Array= new string[ds_count] ;

                     tx_Array = new string[ds_count];

                     rx_Array = new string[ds_count];                    

                     rate_Array = new string[ds_count];

                     testTime_Array = new string[ds_count];    

                     send_Array=new string[ds_count];

                     receive_Array=new string[ds_count];

                     hum_Array=new string[ds_count];

                     int i;

                     for( i = 0; i < du.Tables[0].Rows.Count; i++)

                     {                                                                   

                           Fox_Array[i] = du.Tables[0].Rows[i]["Foxconn"].ToString();

                           Cus_Array[i] = du.Tables[0].Rows[i]["Customer"].ToString();

                           SN_Array[i] = du.Tables[0].Rows[i]["sn"].ToString();

                           TD_Array[i] = du.Tables[0].Rows[i]["test_date"].ToString();

                           Tmp_Array[i]= du.Tables[0].Rows[i]["avg_tmp"].ToString();

                           tx_Array[i] =(Math.Round(decimal.Parse(du.Tables[0].Rows[i]["avg_tx"].ToString()) ,2)).ToString();

                           rx_Array[i] = (Math.Round(decimal.Parse(du.Tables[0].Rows[i]["avg_rx"].ToString()) ,2)).ToString();

                           rate_Array[i] = du.Tables[0].Rows[i]["avg_rate"].ToString();

                           testTime_Array[i] = du.Tables[0].Rows[i]["testTime"].ToString();

                           send_Array[i]=du.Tables[0].Rows[i]["avg_send"].ToString() ;

                           receive_Array[i]=du.Tables[0].Rows[i]["avg_receive"].ToString() ;

                           hum_Array[i]=du.Tables[0].Rows[i]["avg_hum"].ToString() ;

                     }

                }

                catch(Exception error)

                {

                    

                }         

           }

EXCEL進行操作﹕

該操作的功能是將從數據庫中得到的數據寫入到EXCEL中﹐再通過代碼在EXCEL中畫出圖

public void get_data()

           {              

                //代表可選參數

                object objOpt = Missing.Value ;

                int i=0 ;

                getDept2Name();

                Excel.Application    v_Excel = null;

                Excel.Workbooks      v_Books = null;

                Excel.Workbook       v_Book     = null;

                Excel.Worksheet      v_Sheet    = null;

                Excel.Chart                v_Chart = null;           

                try

                {

                     v_Excel         = new Excel.Application();

                     v_Excel.DisplayAlerts = false;

                     v_Excel.Visible            = false;

                     v_Excel.UserControl        = true;                   

                     v_Books    = (Excel.Workbooks)v_Excel.Workbooks ;//顯示的進行類型轉換,減少程式出錯的情況

                     v_Books.Open(Server.MapPath(strFilePath),objOpt,objOpt,objOpt,objOpt,objOpt,objOpt,objOpt,objOpt,objOpt,objOpt,objOpt,objOpt,objOpt,objOpt);

                     v_Book     = (Excel.Workbook)v_Books[1];                  

                     v_Sheet    = (Excel.Worksheet)v_Book.Worksheets[2];

                    

                     //Begin Excel中數據的處理

                     for (i=0 ; i<ds_count ; ++i)

                     {                         

                           v_Sheet.Cells[i+2,1] = Cus_Array[i];      

                           v_Sheet.Cells[i+2,2] = Fox_Array[i];

                           v_Sheet.Cells[i+2,3] = SN_Array[i];       

                           v_Sheet.Cells[i+2,4] =TD_Array[i];

                           v_Sheet.Cells[i+2,5] =testTime_Array[i].ToString().Substring(1,5);

                           v_Sheet.Cells[i+2,6] = tx_Array[i];

                           v_Sheet.Cells[i+2,7] = rx_Array[i];       

                           v_Sheet.Cells[i+2,8] =Tmp_Array[i]; 

                           v_Sheet.Cells[i+2,9] = hum_Array[i];

                           v_Sheet.Cells[i+2,10] = rate_Array[i];         

                           v_Sheet.Cells[i+2,11] =send_Array[i];     

                           v_Sheet.Cells[i+2,12] = receive_Array[i];      

                     }   

     Excel.ChartObjects charts = (Excel.ChartObjects)v_Sheet.ChartObjects(Type.Missing);

                     int get_width =20*(ds_count-1)+300;

                     int get_height=300;

                     Excel.ChartObject chartObj = charts.Add(10,20,get_width,get_height);                           

                     v_Chart = chartObj.Chart ;

                     Excel.Range  chartRange = v_Sheet.get_Range("E1","J"+ds_count);

                     v_Chart.SetSourceData(chartRange,Excel.XlRowCol.xlColumns);

                     v_Chart.ChartType = Excel.XlChartType.xlLine ;

                     //橫坐標的名稱

                     Excel.Axis  xAxis = (Excel.Axis)v_Chart.Axes(Excel.XlAxisType.xlCategory,Excel.XlAxisGroup.xlPrimary);

                     xAxis.HasTitle = true;

                     xAxis.AxisTitle.Text  = "Time";

                     xAxis.TickLabels.Font.Size=8;

                     xAxis.TickLabels.Font.Name="Arial";                 

                     //設置圖表顯示類型

                     v_Chart.ChartType = Excel.XlChartType.xlLine ;

                     //圖表名稱

                     v_Chart.HasTitle = true;

                     v_Chart.ChartTitle.Text = "Detail";

                     v_Chart.ChartTitle.Font.Name ="Arial";

                     v_Chart.ChartTitle.Font.Size =10;

                     Excel.SeriesCollection v_SeriesCollection ;

                     v_SeriesCollection =(Excel.SeriesCollection)v_Chart.SeriesCollection(Type.Missing);

                     Excel.Series v_Series = v_SeriesCollection.Item(v_SeriesCollection.Count);

                     v_Chart.Legend.Left=0;

                     v_Chart.Legend.Top =0;

                     v_Chart.Legend.Height=70;

                     v_Chart.Legend.Width=100;

                     v_Chart.Legend.Font.Name ="Arial";

                     v_Chart.Legend.Font.Size =8;              

                     v_Chart.PlotArea.Left =0;

                     v_Chart.PlotArea.Top =100;

                     v_Chart.PlotArea.Height=200;

                     //設定X軸的取值范圍

                     v_Series.XValues = v_Sheet.get_Range("E2","E"+ds_count+2);

                     v_Series.AxisGroup = Excel.XlAxisGroup.xlSecondary;

                                    

                     //縱坐標名稱(左邊)

                     Excel.Axis  y1Axis = (Excel.Axis)v_Chart.Axes(Excel.XlAxisType.xlValue,Excel.XlAxisGroup.xlPrimary);

                     y1Axis.HasTitle = true;

                     y1Axis.AxisTitle.Text  = "Value";

                     y1Axis.AxisTitle.Font.Name ="Arial";

                     y1Axis.AxisTitle.Font.Size =8;

                     y1Axis.TickLabels.Font.Name  ="Arial";

                     y1Axis.TickLabels.Font.Size =8;

 

                     //縱坐標名稱(右邊)

                     Excel.Axis y2Axis = (Excel.Axis)v_Chart.Axes(Excel.XlAxisType.xlValue,Excel.XlAxisGroup.xlSecondary);

                     y2Axis.HasTitle = true;

                     y2Axis.AxisTitle.Text  = "Rate";

                     y2Axis.AxisTitle.Font.Name ="Arial";

                     y2Axis.AxisTitle.Font.Size =8;

                      y2Axis.TickLabels.Font.Name  ="Arial";

                     y2Axis.TickLabels.Font.Size =8;

                     v_Chart.Location(Excel.XlChartLocation.xlLocationAsObject,"sheet1");                

               

                     //將修改的Excel文件另存到另一個目錄下﹐提供下載.

                     v_Book.SaveAs(Server.MapPath("File/") + "dept2_chart_2.xls",objOpt,objOpt,objOpt,objOpt,objOpt,Excel.XlSaveAsAccessMode.xlNoChange,objOpt,objOpt,objOpt,objOpt,objOpt);

 

                     //將修改后的Excle以html的方式另存.

                     v_Book.SaveAs(Server.MapPath("File/") + "dept2_chart_2.html",Excel.XlFileFormat.xlHtml,objOpt,objOpt,objOpt,objOpt,Excel.XlSaveAsAccessMode.xlNoChange,objOpt,objOpt,objOpt,objOpt,objOpt);                   

                     v_Book.Close(false,objOpt,objOpt);

                     v_Excel.Quit();

                     v_Sheet    = null;

                     v_Book     = null;

                     v_Books    = null;

                     v_Excel    = null;

                     GC.Collect();

                     Response.Redirect("file/dept2_chart_2.html");

                }

                catch(Exception error)

                {

                     v_Book.Close(false,objOpt,objOpt);

                     v_Excel.Quit();

                     v_Sheet    = null;

                     v_Book     = null;

                     v_Books    = null;

                     v_Excel    = null;

                     GC.Collect();                                       

                }                         

           }