一次多层架构实战后记--杂谈

  1、由于在SQLServer中定义了大量的视图、存储过程和函数,哪怕是自己定义的,也难免记不清,为了节省在SQLServer和VS中来回切换的次数,可以将所需要的相关内容放在一个静态的密封类中,视图以V_开头,存储过程以P_开头,函数以F_开头,这样通过VS的智能感知,可以快速方便的找到对应项的名字,提高了一定的工作效率。

 

  2、窗体程序中经常牵涉到从数据表格导出数据到EXCEL中的功能,方法有很多,发现网上有一个被转的到处都是的方案,大致如下:

  

   /// <summary>
        /// 将数据网格导出到Excel文件
        /// </summary>
        /// <returns>成功则返回true,否则返回FALSE</returns>
        public bool ExportToExcel(String saveFileName)
        {
            bool result = false;
            if (dataGridViewSource == null || dataGridViewSource.DataSource == null) return result;

            if (saveFileName.IndexOf(":") < 0) return false; 

            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            if (xlApp == null)
            {
                MessageBox.Show("无法创建Excel对象,可能您使用的计算机未安装Excel 2000以上版本");
                return false;
            }
            try
            {
                Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
                Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
                //worksheet.Visible = Microsoft.Office.Interop.Excel.XlSheetVisibility.xlSheetVisible;
                int colIndex = 0;
                //写入标题
                for (int i = 0; i < dataGridViewSource.ColumnCount; i++)
                {

                    if (dataGridViewSource.Columns[i].Visible)
                    {
                        colIndex++;
                        Microsoft.Office.Interop.Excel.Range aCell;
                        aCell = worksheet.Cells[1, colIndex] as Microsoft.Office.Interop.Excel.Range;
                        aCell.Value2 = dataGridViewSource.Columns[i].HeaderText;
                    }
                }
                //写入数值
                for (int r = 0; r < dataGridViewSource.Rows.Count; r++)
                {
                    colIndex = 0;
                    for (int i = 0; i < dataGridViewSource.ColumnCount; i++)
                    {
                        if (dataGridViewSource.Columns[i].Visible)
                        {
                            colIndex++;
                            Microsoft.Office.Interop.Excel.Range aCell;
                            aCell = worksheet.Cells[r + 2, colIndex] as Microsoft.Office.Interop.Excel.Range;
                            //这里不判断有可能抛出NullReferencesException
                            if (dataGridViewSource.Rows[r].Cells[i].Value != null)
                            {
                                aCell.Value2 = dataGridViewSource.Rows[r].Cells[i].Value.ToString();
                            }
                            else
                            {
                                aCell.Value2 = string.Empty;
                            }
                        }
                    }
                    System.Windows.Forms.Application.DoEvents();
                }
                worksheet.Columns.EntireColumn.AutoFit();

                if (saveFileName != "")
                {
                    try
                    {
                        workbook.Saved = true;
                        workbook.SaveCopyAs(saveFileName);
                        result = true;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
                        result = false;
                    }

                }
            }
            finally
            {
                xlApp.Quit();
                GC.Collect();//强行销毁
            }
            // MessageBox.Show("导出Excel成功!");


            return result;
        }

   注意红字的地方,其实正应了那句话,不实际操作的话永远不知道会有问题。。。

   尽信书不如无书,现在改成尽信网不如无网好了^_^

 

   3、经常在数据库操作中用到的几个分析查询的语句

   SET STATISTICS TIME ON

   SET STATISTICS IO ON

   SET STATISTICS PROFILE ON

   另外,感谢一位未注册用户在我的DIY小工具开发---SQL关键字批量转换中的回复,提到的那款SQL PROMPT ,的确非常好用,写SQL真是爽到不行。。^_^

 

   4、再补充点小菜,列几个学到或巩固的几个英文单词:

   Assay -- 化验, 分析

   Leechdom -- 药品

   Organ -- 机构,部门

   Prescription -- 处方

   Manufacturer -- 厂商

 

posted @ 2010-05-21 16:36  RockyXiang  阅读(244)  评论(0编辑  收藏  举报