Sunday, February 13, 2011

 
 
 
 
 
 
AliasCLR type
stringSystem.String
sbyteSystem.SByte
byteSystem.Byte
shortSystem.Int16
ushortSystem.UInt16
intSystem.Int32
uintSystem.UInt32
longSystem.Int64
ulongSystem.UInt64
charSystem.Char
floatSystem.Single
doubleSystem.Double
boolSystem.Boolean
decimalSystem.Decimal

Thursday, January 6, 2011

Import Excel using OLEDB

You may need to impor a namespace:
using System.Data.OleDb;

//New DataTAble
table = new DataTable();

//Conection Straig For Excel Files
//FilePath is the direccion where the file is stored
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + FilePath + "; Jet OLEDB:Engine Type=5;" +
"Extended Properties=Excel 8.0;";

//Establish Coneccion
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
OleDbCommand comm = new OleDbCommand();
comm.CommandType = CommandType.Text;
comm.Connection = conn;

//Query for the Sheet
//Hoja is the name of the sheet
comm.CommandText = "SELECT * FROM ["+ Hoja +"$]";

//Execute Query and stores in a DataTable
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = comm;
adapter.Fill(table);
conn.Close();

//Then you can put the informacion in a grid
//MyGrid.DataSource = table
//Or take the table and insert into a databases

Export to Excel using StreamWriter Interop

You need to impor:
using excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
 
//Variables
excel.Application App;
excel.Workbook WB;
excel.Worksheet wsheet;
excel.Range range;

//Open New Instance of Excel
App = new excel.Application();
App.Visible = true;
App.DisplayAlerts = false;

//New Workbook
WB = App.Workbooks.Add(Missing.Value);

//New Sheet
wsheet = (excel.Worksheet)WB.ActiveSheet;
wsheet.Name = "Hoja";

//Procces DataTable
int Aux = 1;
foreach (DataRow row in table.Rows)
{
     Aux+=1;
     for (int i = 1; i < table.Columns.Count + 1; i++)
     {
          //Add Headers
          if (Aux == 2)
          {
               wsheet.Cells[1, i] = table.Columns[i - 1].ColumnName;
          }
          wsheet.Cells[Aux, i] = row[i - 1].ToString();
     }
}

Export to Excel using StreamWriter

I assume that you had filled a datagriview with a datatable and add namespace System.IO

// Set location to save the file, name and extension
SaveFileDialog SaveFile = new SaveFileDialog();
SaveFile.DefaultExt = "csv";
SaveFile.FileName = "HojaTest";
SaveFile.Filter = "csv";
 
if (SaveFile.ShowDialog() == DialogResult.OK)
{
SaveFile.AddExtension = true;
//Create File
StreamWriter sw = new StreamWriter(SaveFile.FileName, false);
string Headers = "";
 
//Write headers to the file
foreach (DataGridViewColumn col in grid.Columns)
{
    if (Headers != "")
         Headers += ",";
    Headers += col.Name;
}
sw.Write(Headers);
sw.Write(sw.NewLine); 
 
//Write the informacion to excel file
foreach (DataRow row in table.Rows)
{
     for (int i = 0; i < table.Columns.Count ; i++)
     {
           if (!row[i].Equals(DBNull.Value))
                sw.Write(row[i].ToString().Replace(",", "."));
           if (i < table.Columns.Count - 1)
                 sw.Write(",");
      }
sw.Write(sw.NewLine);
}
sw.Close();
MessageBox.Show("Datos Exportados");

Wednesday, October 13, 2010

Get a selected object in datagridview and GridControl

///******GridControl******\\\
private void gridView_DoubleClick(object sender, EventArgs e)
            {
                   CustomObject  ObjectX = (CustomObject)((GridView)sender).GetFocusedRow();
                   // Do what ever you need with the objectX
            }

The following sample code can be used to get the value of the Row within the focused row.

DataRow row = gridView1.GetDataRow(gridView1.FocusedRowHandle);


///******DataBridView******\\\ 

private void gridRowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
           foreach (DataGridViewRow row in gridCategorias.SelectedRows)
           {
               Categoria categ = row.DataBoundItem as Categoria;
               // Do what ever you want with the "Customn Object" Categoria
          }
        }

Thursday, October 7, 2010

how to detect wich key was press

This code detects when any kay is pressed

Thursday, July 8, 2010

Import Excel File Into SQL Server

First we need to enable the use of "Ad Hoc Distributed Queries" by using "SP_CONFIGURE"

1.- Open SQL server & New Query
2.- Run the command sp_configure
















3.- If we can not see the option "Ad Hoc Distributed Queries", we need activate "show advanced options"
     with the command "sp_configure 'show advanced options', 1", and then the commando        "reconfigure".

4.- If you run again the command "sp_configure" you will see the "Ad Hoc Distributed Queries" 

 
5.- Now we need anable Ad Hoc Distributed Queries, with the follow command "sp_configure 'Ad Hoc Distributed Queries', 1 " and the "reconfigure"

6.- Finally we are going to execute the following Query.

SELECT * INTO table FROM  OPENROWSET ('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=D:\Book1.xls', 'SELECT * FROM [Sheet1$]')

Where:
table = New table name.
Database = Excel file path
Sheet1 = Name Sheet