/// <summary> /// 数据初始化 /// </summary> public void datainit() { string mysql = ""; string tablename = "worker"; DataSet info; //新建对象tutoring_info worker1.worker_num = dgv_search.SelectedRows[0].Cells[0].Value.ToString(); mysql = "select * from worker where worker_num= " + worker1.worker_num; try { info = mydb.ExecuteQuery(mysql, tablename); } catch (Exception ex) { MessageBox.Show(ex.Message, "操作数据库出错!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } worker1.name = info.Tables[0].Rows[0].ItemArray[1].ToString(); worker1.sex = info.Tables[0].Rows[0].ItemArray[2].ToString(); worker1.student_num = info.Tables[0].Rows[0].ItemArray[3].ToString(); worker1.subject = info.Tables[0].Rows[0].ItemArray[4].ToString(); worker1.phone = info.Tables[0].Rows[0].ItemArray[5].ToString(); worker1.position = info.Tables[0].Rows[0].ItemArray[6].ToString(); worker1.month_score = info.Tables[0].Rows[0].ItemArray[7].ToString(); worker1.total_score = info.Tables[0].Rows[0].ItemArray[8].ToString(); worker1.isleave = info.Tables[0].Rows[0].ItemArray[9].ToString(); }
/// <summary> /// 数据初始化 /// </summary> public void datainit() { string mysql = ""; string tablename = "tutor"; DataSet info; //新建对象tutoring_info tutor1.tutor_num = dgv_search.SelectedRows[0].Cells[0].Value.ToString(); //Console.WriteLine(tutor1.print_num); mysql = "select * from tutor where tutor_num= " + tutor1.tutor_num; try { info = mydb.ExecuteQuery(mysql, tablename); } catch (Exception ex) { MessageBox.Show(ex.Message, "操作数据库出错!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } tutor1.name = info.Tables[0].Rows[0].ItemArray[1].ToString(); tutor1.student_num = info.Tables[0].Rows[0].ItemArray[2].ToString(); tutor1.subject = info.Tables[0].Rows[0].ItemArray[3].ToString(); tutor1.degree = info.Tables[0].Rows[0].ItemArray[4].ToString(); tutor1.sex = info.Tables[0].Rows[0].ItemArray[5].ToString(); tutor1.phone = info.Tables[0].Rows[0].ItemArray[6].ToString(); tutor1.place = info.Tables[0].Rows[0].ItemArray[7].ToString(); tutor1.blacklist = info.Tables[0].Rows[0].ItemArray[8].ToString(); tutor1.explantion = info.Tables[0].Rows[0].ItemArray[9].ToString(); tutor1.remarks = info.Tables[0].Rows[0].ItemArray[10].ToString(); }
public void dgvbind(DataGridView dgv, string mystr, string tb)//绑定数据的自定义方法 { commondb mydb = new commondb(); DataSet mydataset = mydb.ExecuteQuery(mystr, tb); dgv.DataSource = mydataset.Tables[tb]; }
/// <summary> /// 绑定查询数据库后的datatable数据 /// </summary> /// <param name="cbo"></param> /// <param name="cmdText"></param> /// <param name="tb"></param> /// <param name="DisplayFied"></param> public void tablecbobind(ComboBox cbo, string cmdText, string tb, string DisplayFied) { commondb mydb = new commondb(); DataSet mydataset; // string mystr = "SELECT distinct 接待人 FROM [Table]"; mydataset = mydb.ExecuteQuery(cmdText, tb); cbo.DataSource = mydataset.Tables[tb]; cbo.DisplayMember = DisplayFied; }
/// <summary> /// 将选中单元格数据赋值到对象 /// </summary> public void datainit() { string mysql = ""; string tablename = "tutoring"; DataSet info; //新建对象tutoring_info tutoring1.print_num = dgv_search.SelectedRows[0].Cells[0].Value.ToString(); mysql = "select * from tutoring where print_num= " + tutoring1.print_num; try { info = mydb.ExecuteQuery(mysql, tablename); mysql = "select name from worker where isleave='否'"; cbobind.tablecbobind(cbo_reception, mysql, "workers", "name"); } catch (Exception ex) { MessageBox.Show(ex.Message, "操作数据库出错!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } tutoring1.parent_num = info.Tables[0].Rows[0].ItemArray[1].ToString(); tutoring1.reception = info.Tables[0].Rows[0].ItemArray[2].ToString(); tutoring1.reception_time = info.Tables[0].Rows[0].ItemArray[3].ToString(); tutoring1.parent_name = info.Tables[0].Rows[0].ItemArray[4].ToString(); tutoring1.phone = info.Tables[0].Rows[0].ItemArray[5].ToString(); tutoring1.simple_adr = info.Tables[0].Rows[0].ItemArray[6].ToString(); tutoring1.detail_adr = info.Tables[0].Rows[0].ItemArray[7].ToString(); tutoring1.grade_stu = info.Tables[0].Rows[0].ItemArray[8].ToString(); tutoring1.subject_stu = info.Tables[0].Rows[0].ItemArray[9].ToString(); tutoring1.student_sex = info.Tables[0].Rows[0].ItemArray[10].ToString(); tutoring1.tutors_price = info.Tables[0].Rows[0].ItemArray[11].ToString(); tutoring1.tutors_time = info.Tables[0].Rows[0].ItemArray[12].ToString(); tutoring1.sex = info.Tables[0].Rows[0].ItemArray[13].ToString(); tutoring1.place = info.Tables[0].Rows[0].ItemArray[14].ToString(); tutoring1.grade = info.Tables[0].Rows[0].ItemArray[15].ToString(); tutoring1.subject = info.Tables[0].Rows[0].ItemArray[16].ToString(); tutoring1.other_requests = info.Tables[0].Rows[0].ItemArray[17].ToString(); tutoring1.payment_state = info.Tables[0].Rows[0].ItemArray[18].ToString(); tutoring1.payment_time = info.Tables[0].Rows[0].ItemArray[19].ToString(); tutoring1.tutor_state = info.Tables[0].Rows[0].ItemArray[20].ToString(); tutoring1.remarks = info.Tables[0].Rows[0].ItemArray[21].ToString(); }
private void initdata(string print_num, objectclass tutor) { string tablename = "tutoring"; string mysql = "select * from tutoring where print_num=" + print_num; DataSet mydataset = mydb.ExecuteQuery(mysql, tablename); }
public void ExportExcel(string fileName, string tablename) { /*string saveFileName = ""; * //bool fileSaved = false; * SaveFileDialog saveDialog = new SaveFileDialog(); * saveDialog.DefaultExt = "xlsx"; * saveDialog.Filter = "Excel文件|*.xlsx"; * saveDialog.FileName = fileName; * saveDialog.ShowDialog(); * Cursor.Current = Cursors.WaitCursor;*/ string saveFileName = fileName; if (saveFileName.IndexOf(":") < 0) { return; //被点了取消 } Excel.Application xlApp = new Excel.Application(); if (xlApp == null) { MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel"); return; } Excel.Workbooks workbooks = xlApp.Workbooks; Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet); Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];//取得sheet1 commondb mydb = new commondb(); string mysql = "select * from " + tablename; DataSet mydataset = mydb.ExecuteQuery(mysql, tablename); DataTable dt = mydataset.Tables[0]; //写入标题 for (int i = 0; i < dt.Columns.Count; i++) { worksheet.Cells[1, i + 1] = dt.Columns[i].ColumnName; } //写入数值 Cursor.Current = Cursors.WaitCursor; for (int r = 0; r < dt.Rows.Count; r++) { for (int i = 0; i < dt.Columns.Count; i++) { worksheet.Cells[r + 2, i + 1] = dt.Rows[r][i]; } System.Windows.Forms.Application.DoEvents(); Cursor.Current = Cursors.WaitCursor; } worksheet.Columns.EntireColumn.AutoFit();//列宽自适应 //if (Microsoft.Office.Interop.cmbxType.Text != "Notification") //{ // Excel.Range rg = worksheet.get_Range(worksheet.Cells[2, 2], worksheet.Cells[ds.Tables[0].Rows.Count + 1, 2]); // rg.NumberFormat = "00000000"; //} if (saveFileName != "") { try { workbook.Saved = true; workbook.SaveCopyAs(saveFileName); //fileSaved = true; } catch (Exception ex) { //fileSaved = false; MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message); } } //else //{ // fileSaved = false; //} xlApp.Quit(); GC.Collect();//强行销毁 // if (fileSaved && System.IO.File.Exists(saveFileName)) System.Diagnostics.Process.Start(saveFileName); //打开EXCEL }