Skip to content

Instantly share code, notes, and snippets.

@RicardoACS
Last active August 29, 2015 14:20
Show Gist options
  • Save RicardoACS/e6e1ca8e77e894b44eb8 to your computer and use it in GitHub Desktop.
Save RicardoACS/e6e1ca8e77e894b44eb8 to your computer and use it in GitHub Desktop.
Clase Conexion DLL
//referencias
//System.configuration;
//System.Data.SQLite;
//System.Windows.Forms;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SQLite;
using System.Text;
using System.Windows.Forms;
public class BaseDeDatosLite
{
private string connection = string.Empty;
private SQLiteConnection connect;
private SQLiteCommand command;
private SQLiteDataAdapter da;
private DataTable dt;
private DataSet ds;
public BaseDeDatosLite()
{
connect = new SQLiteConnection();
try
{
connection = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
}
catch
{
connection = ConfigurationManager.AppSettings.Get("connection");
}
}
private SQLiteConnection connecttodb()
{
if (connect.State != ConnectionState.Open)
{
try
{
connect.ConnectionString = connection;
connect.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
return connect;
}
private void closeconnection()
{
if (connect.State != ConnectionState.Closed)
connect.Close();
}
public string selectstring(string query)
{
string cadena = string.Empty;
try
{
connecttodb();
command = new SQLiteCommand(query, connect);
cadena = command.ExecuteScalar().ToString();
}
catch
{
cadena = string.Empty;
}
finally
{
closeconnection();
}
return cadena;
}
public bool executecommand(string query)
{
bool exito;
try
{
connecttodb();
command = new SQLiteCommand(query, connect);
command.ExecuteNonQuery();
exito = true;
}
catch
{
exito = false;
}
finally
{
closeconnection();
}
return exito;
}
public bool ExecuteStoreProcedure(string namestoreprocedure)
{
try
{
connecttodb();
command = new SQLiteCommand(namestoreprocedure, connect);
command.CommandType = CommandType.StoredProcedure;
command.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
finally
{
closeconnection();
}
}
public DataTable SelectDataTableFromStoreProcedure(string namestoreprocedure)
{
dt = new DataTable();
try
{
connecttodb();
command = new SQLiteCommand(namestoreprocedure, connect);//
command.CommandType = CommandType.StoredProcedure;
dt = new DataTable();
da = new SQLiteDataAdapter();
da.SelectCommand = command;
da.Fill(dt);
return dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
closeconnection();
}
return dt;
}
public DataTable SelectDataTable(string query)
{
dt = new DataTable();
try
{
connecttodb();
da = new SQLiteDataAdapter(query, connect);
da.Fill(dt);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
connecttodb();
}
return dt;
}
public DataSet SelectDataSet(string query, string table)
{
ds = new DataSet();
try
{
connecttodb();
da = new SQLiteDataAdapter(query, connect);
da.Fill(ds, table);
}
catch //(Exception ex)
{
// ds = null;
}
finally
{
closeconnection();
}
return ds;
}
}
@rurtubia
Copy link

weeeena

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment