public void dgvbind(DataGridView dgv, string mystr, string tb)//绑定数据的自定义方法 { commondb mydb = new commondb(); DataSet mydataset = mydb.ExecuteQuery(mystr, tb); dgv.DataSource = mydataset.Tables[tb]; }
private void mainform_Load(object sender, EventArgs e) { commondb mydb = new commondb(); string mysql = ""; string name = ""; mysql = "select name from worker where worker_num='" + username + "'"; int count = 0; try { count = mydb.Rownum(mysql); if (count > 0) { name = mydb.Returnafield(mysql); lbl_user_num.Text = username; lbl_user_name.Text = name; lbl_admin.Visible = false; } if (degree == "管理员") { lbl_user_num.Text = username; lbl_user_name.Text = ""; lbl_admin.Visible = true; } } catch (Exception ex) { MessageBox.Show(ex.Message, "操作数据库出错!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } }
/// <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; }
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 }