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");
}
Thursday, January 6, 2011
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
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
1.- Open SQL server & New Query
2.- Run the command sp_configure
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
Saturday, May 8, 2010
Monday, April 19, 2010
Restore a Data Base from C#
private void btnRestore_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
try
{
if (File.Exists(txtPath.Text + txtDireccion.Text + ".bak"))
{
if (MessageBox.Show("¿Está seguro de restaurar?", "Respaldo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
if (Program.laconexion.State != System.Data.ConnectionState.Open)
Program.laconexion.Open();
SqlCommand command = new SqlCommand("use master", Program.laconexion);
command.ExecuteNonQuery();
command = new SqlCommand(@"restore database FABRINOX from disk ='"+txtPath.Text + txtDireccion.Text + ".bak'", Program.laconexion);
command.ExecuteNonQuery();
Program.laconexion.Close();
MessageBox.Show("Se ha restaurado la base de datos", "Restauración", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}
}
else
MessageBox.Show(@"No haz hecho ningun respaldo anteriormente (o no está en la ruta correcta)", "Restauracion", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
{
Cursor.Current = Cursors.WaitCursor;
try
{
if (File.Exists(txtPath.Text + txtDireccion.Text + ".bak"))
{
if (MessageBox.Show("¿Está seguro de restaurar?", "Respaldo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
if (Program.laconexion.State != System.Data.ConnectionState.Open)
Program.laconexion.Open();
SqlCommand command = new SqlCommand("use master", Program.laconexion);
command.ExecuteNonQuery();
command = new SqlCommand(@"restore database FABRINOX from disk ='"+txtPath.Text + txtDireccion.Text + ".bak'", Program.laconexion);
command.ExecuteNonQuery();
Program.laconexion.Close();
MessageBox.Show("Se ha restaurado la base de datos", "Restauración", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}
}
else
MessageBox.Show(@"No haz hecho ningun respaldo anteriormente (o no está en la ruta correcta)", "Restauracion", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
Back up a Data Base from C#
private void btnGuardar_Click(object sender, EventArgs e)
{
bool desea_respaldar = true;
Cursor.Current = Cursors.WaitCursor;
if (Directory.Exists(txtPath.Text))
{
if (File.Exists(txtPath.Text + txtDireccion.Text+ ".bak"))
{
if (MessageBox.Show(@"Archivo existe ¿desea remplazarlo?", "Respaldo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
File.Delete(txtPath.Text + txtDireccion.Text + ".bak");
}
else
desea_respaldar = false;
}
}
else
Directory.CreateDirectory(txtPath.Text);
if (desea_respaldar)
{
if (Program.laconexion.State != System.Data.ConnectionState.Open)
Program.laconexion.Open();
SqlCommand command;
command = new SqlCommand(@"backup database FABRINOX to disk ='" + txtPath.Text + txtDireccion.Text + ".bak" + "' with init,stats=10", Program.laconexion);
command.ExecuteNonQuery();
Program.laconexion.Close();
MessageBox.Show("Respaldo Exitoso");
this.Close();
}
}
{
bool desea_respaldar = true;
Cursor.Current = Cursors.WaitCursor;
if (Directory.Exists(txtPath.Text))
{
if (File.Exists(txtPath.Text + txtDireccion.Text+ ".bak"))
{
if (MessageBox.Show(@"Archivo existe ¿desea remplazarlo?", "Respaldo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
File.Delete(txtPath.Text + txtDireccion.Text + ".bak");
}
else
desea_respaldar = false;
}
}
else
Directory.CreateDirectory(txtPath.Text);
if (desea_respaldar)
{
if (Program.laconexion.State != System.Data.ConnectionState.Open)
Program.laconexion.Open();
SqlCommand command;
command = new SqlCommand(@"backup database FABRINOX to disk ='" + txtPath.Text + txtDireccion.Text + ".bak" + "' with init,stats=10", Program.laconexion);
command.ExecuteNonQuery();
Program.laconexion.Close();
MessageBox.Show("Respaldo Exitoso");
this.Close();
}
}
Subscribe to:
Posts (Atom)







