コード例 #1
0
ファイル: frmMain.cs プロジェクト: Zerdusht/XlsImporter
 private void btnSave2Table_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
 {
     frmSQLConnector frmConn = new frmSQLConnector();
     DialogResult ret = frmConn.ShowDialog();
     if (ret == DialogResult.OK)
     {
         Cursor cursor = Cursor.Current;
         Cursor.Current = Cursors.WaitCursor;
         SqlConnection cnn = new SqlConnection(frmConn.GetDataBaseConnectionString());
         cnn.Open();
         string tableName = cboxSheets.EditValue.ToString();
         if (Library.InputBox("Tablo Oluştur", "Yeni tablo adı:", ref tableName) == DialogResult.OK)
         {
             listColumns = new BindingList<Library.ColumnMapping>();
             for (int i = 0; i <= gwXlsFile.Columns.Count - 1; i++)
             {
                 Library.ColumnMapping map = new Library.ColumnMapping();
                 string sColumnName;
                 if ((int)radioHeader.EditValue == 1)
                 {
                     if (!gwXlsFile.Columns[i].Caption.Contains("[")) sColumnName = "[" + gwXlsFile.Columns[i].Caption + "]";
                     else sColumnName = gwXlsFile.Columns[i].Caption;
                 }
                 else
                     sColumnName = string.Format("Column{0}", i);
                 map.ColumnName = sColumnName; //((int)radioHeader.EditValue == 1) ? gwXlsFile.Columns[i].Caption : "Column" + Convert.ToString(i);
                 string dataType;
                 bool boolNull=true, boolNumeric=false;
                 int iSize=0, iPrecision=0, iScale=0;
                 switch (gwXlsFile.Columns[i].ColumnType.Name)
                 {
                     case "Default": dataType = "varchar"; iSize = 255;
                         break;
                     case "String": dataType = "varchar"; iSize = 255;
                         break;
                     case "Boolean": dataType = "bit";
                         break;
                     case "DateTime": dataType = "datetime";
                         break;
                     case "Decimal": dataType = "decimal"; iPrecision = 18; iScale = 2; boolNumeric = true;
                         break;
                     case "Double": dataType = "float"; boolNumeric = true;
                         break;
                     case "Integer": dataType = "int"; boolNumeric = true;
                         break;
                     default:
                         dataType = "varchar"; iSize = 255;
                         break;
                 }
                 map.ColumnType = dataType;
                 map.Nullable = boolNull;
                 map.Precision = iPrecision;
                 map.Size = iSize;
                 map.Scale = iScale;
                 map.IsNumeric = boolNumeric;
                 listColumns.Add(map);
             }
             frmColumnMapping frmMaps = new frmColumnMapping();
             frmMaps.listColumns = listColumns;
             frmMaps.tableName = tableName;
             frmMaps.cnn = cnn;
             frmMaps.ShowDialog();
             libraryDataLayer = new Library.DataLayer();
             DataTable dtVeri = (gridXlsFile.DataSource as DataTable);
             foreach (DataRow row in dtVeri.Rows)
             {
                 Library.DataLayer.InsertedFieldType[] arrFields = new Library.DataLayer.InsertedFieldType[listColumns.Count];
                 int i = 0;
                 foreach (Library.ColumnMapping mapColumn in listColumns)
                 {
                     arrFields[i].FieldName = mapColumn.ColumnName;
                     arrFields[i].FieldValue = row[i].ToString();
                     arrFields[i].IsNumeric = mapColumn.IsNumeric;
                     i = i + 1;
                 }
                 try
                 {
                     string retMsg = libraryDataLayer.InsertRow2Table(cnn, tableName, arrFields);
                     if (retMsg != "") throw new Exception(retMsg);
                 }
                 catch (Exception ex)
                 {
                     MessageBox.Show("Hata oluştu(0x8800F56D).\nHata detayı : " + ex.Message, "Kayıt Hatası", MessageBoxButtons.OK, MessageBoxIcon.Error);
                     if (MessageBox.Show("Diğer kayıtları aktarmaya devam edilsin mi?", "Onay", MessageBoxButtons.YesNo,
                             MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.No)
                         return;
                 }
             }
             MessageBox.Show("Aktarım tamamlandı!");
         }
         Cursor.Current = cursor;
     }
 }
コード例 #2
0
ファイル: frmMain.cs プロジェクト: zhifu10001/XlsImporter
        private void btnSave2Table_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmSQLConnector frmConn = new frmSQLConnector();
            DialogResult    ret     = frmConn.ShowDialog();

            if (ret == DialogResult.OK)
            {
                Cursor cursor = Cursor.Current;
                Cursor.Current = Cursors.WaitCursor;
                SqlConnection cnn = new SqlConnection(frmConn.GetDataBaseConnectionString());
                cnn.Open();
                string tableName = cboxSheets.EditValue.ToString();
                if (Library.InputBox("Tablo Oluştur", "Yeni tablo adı:", ref tableName) == DialogResult.OK)
                {
                    listColumns = new BindingList <Library.ColumnMapping>();
                    for (int i = 0; i <= gwXlsFile.Columns.Count - 1; i++)
                    {
                        Library.ColumnMapping map = new Library.ColumnMapping();
                        string sColumnName;
                        if ((int)radioHeader.EditValue == 1)
                        {
                            if (!gwXlsFile.Columns[i].Caption.Contains("["))
                            {
                                sColumnName = "[" + gwXlsFile.Columns[i].Caption + "]";
                            }
                            else
                            {
                                sColumnName = gwXlsFile.Columns[i].Caption;
                            }
                        }
                        else
                        {
                            sColumnName = string.Format("Column{0}", i);
                        }
                        map.ColumnName = sColumnName; //((int)radioHeader.EditValue == 1) ? gwXlsFile.Columns[i].Caption : "Column" + Convert.ToString(i);
                        string dataType;
                        bool   boolNull = true, boolNumeric = false;
                        int    iSize = 0, iPrecision = 0, iScale = 0;
                        switch (gwXlsFile.Columns[i].ColumnType.Name)
                        {
                        case "Default": dataType = "varchar"; iSize = 255;
                            break;

                        case "String": dataType = "varchar"; iSize = 255;
                            break;

                        case "Boolean": dataType = "bit";
                            break;

                        case "DateTime": dataType = "datetime";
                            break;

                        case "Decimal": dataType = "decimal"; iPrecision = 18; iScale = 2; boolNumeric = true;
                            break;

                        case "Double": dataType = "float"; boolNumeric = true;
                            break;

                        case "Integer": dataType = "int"; boolNumeric = true;
                            break;

                        default:
                            dataType = "varchar"; iSize = 255;
                            break;
                        }
                        map.ColumnType = dataType;
                        map.Nullable   = boolNull;
                        map.Precision  = iPrecision;
                        map.Size       = iSize;
                        map.Scale      = iScale;
                        map.IsNumeric  = boolNumeric;
                        listColumns.Add(map);
                    }
                    frmColumnMapping frmMaps = new frmColumnMapping();
                    frmMaps.listColumns = listColumns;
                    frmMaps.tableName   = tableName;
                    frmMaps.cnn         = cnn;
                    frmMaps.ShowDialog();
                    libraryDataLayer = new Library.DataLayer();
                    DataTable dtVeri = (gridXlsFile.DataSource as DataTable);
                    foreach (DataRow row in dtVeri.Rows)
                    {
                        Library.DataLayer.InsertedFieldType[] arrFields = new Library.DataLayer.InsertedFieldType[listColumns.Count];
                        int i = 0;
                        foreach (Library.ColumnMapping mapColumn in listColumns)
                        {
                            arrFields[i].FieldName  = mapColumn.ColumnName;
                            arrFields[i].FieldValue = row[i].ToString();
                            arrFields[i].IsNumeric  = mapColumn.IsNumeric;
                            i = i + 1;
                        }
                        try
                        {
                            string retMsg = libraryDataLayer.InsertRow2Table(cnn, tableName, arrFields);
                            if (retMsg != "")
                            {
                                throw new Exception(retMsg);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Hata oluştu(0x8800F56D).\nHata detayı : " + ex.Message, "Kayıt Hatası", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            if (MessageBox.Show("Diğer kayıtları aktarmaya devam edilsin mi?", "Onay", MessageBoxButtons.YesNo,
                                                MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.No)
                            {
                                return;
                            }
                        }
                    }
                    MessageBox.Show("Aktarım tamamlandı!");
                }
                Cursor.Current = cursor;
            }
        }