private void buttonQueryTest_Click(object sender, EventArgs e) { SqlConnection sqlConnection = null; DataTable dataTable = null; // create sql statement StringBuilder sb = new StringBuilder(); sb.Clear(); sb.Append(@"SELECT * FROM [Attend].[dbo].[FEmployee]"); try { // create connection //sqlConnection = MvDbConnector.Connection_MVDB01_Dot_Attend; sqlConnection = MvDbConnector.getSystemDbConnection(MvCompanySite.MACHVISION, MvSystem.TW_HR); sqlConnection.Open(); // read data from db dataTable = MvDbConnector.queryDataBySql(sqlConnection, sb.ToString()); } catch (SqlException se) { Console.WriteLine(se.StackTrace); } finally { if (sqlConnection != null) { sqlConnection.Close(); sqlConnection.Dispose(); } } // show data table result in console showDataTableInConsole(dataTable); dataTable = null; }
/// <summary> /// query the data from DB /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonQuery_Click(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(); SqlConnection sqlConnection = null; string machineType = null; DataTable dataTable = null; // 先將值存起來, 後續顯示判斷使用 machineType = cboRpt.Text; // get machineTypeName from data base if (machineTypeMappingTable.Rows.Count == 0) { // get machineTypeName and Type id sb.Clear(); sb.Append(@"SELECT DISTINCT(MachTypeID), MachTypeName FROM [MV-DB01].MVPlanSystem2018.dbo.tblMachManages WHERE OrderType is NULL "); // create connection try { //sqlConnection = MvDbConnector.Connection_MVDB01_Dot_MVPlanSystem2018; sqlConnection = MvDbConnector.getSystemDbConnection(MvCompanySite.MACHVISION, MvSystem.TW_MVPLAN); sqlConnection.Open(); // read data from db machineTypeMappingTable = queryMachTypeIdAndName(sqlConnection, sb.ToString()); } catch (SqlException se) { Console.WriteLine(se.StackTrace); } finally { if (sqlConnection != null) { sqlConnection.Close(); sqlConnection.Dispose(); } } // cboRpt.DataSource = machineTypeMappingTable; cboRpt.ValueMember = "MachTypeID"; cboRpt.DisplayMember = "Value.DisplayName"; cboRpt.Text = ""; cboRpt.SelectedValue = ""; } else { machineType = cboRpt.SelectedValue == null ? "" : cboRpt.SelectedValue.ToString(); } // get the list from data base sb.Clear(); sb.Append(@"SELECT MachID,MachSN,MachTypeID,MachTypeName,OrderNumber,Locate,CustomerName,EndCustomerName,OrderType, ") .Append("ShipType,OrderShipDate,ConfirmShipDate,MachNote,MachShip,MachClose,CustomerPONo,ManufactureOrderNo,MDate ") .Append("FROM [MV-DB01].MVPlanSystem2018.dbo.tblMachManages ") .Append("WHERE OrderType is NULL ") .Append(machineType == "" ? "" : " AND MachTypeID = '" + machineType + "'"); // create connection try { //sqlConnection = MvDbConnector.Connection_MVDB01_Dot_MVPlanSystem2018; sqlConnection = MvDbConnector.getSystemDbConnection(MvCompanySite.MACHVISION, MvSystem.TW_MVPLAN); sqlConnection.Open(); // read data from db dataTable = MvDbConnector.queryDataBySql(sqlConnection, sb.ToString()); } catch (SqlException se) { Console.WriteLine(se.StackTrace); } finally { if (sqlConnection != null) { sqlConnection.Close(); sqlConnection.Dispose(); } } dataGridView1.DataSource = dataTable; dataGridView1.ReadOnly = true; // show data table result in console //showDataTableInConsole(dataTable); sb.Clear(); sb = null; dataTable = null; }