Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Getting a connection to database lost error
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data;
- using System.Windows.Forms;
- using FirebirdSql.Data.FirebirdClient;
- namespace _0912111
- {
- class DatabaseConnection
- {
- private FbConnection conn;
- private FbCommand sqlCommand;
- private FbDataAdapter DB;
- private DataSet DS = new DataSet();
- public DatabaseConnection()
- {
- conn = new FbConnection("User=myuser;" + "Password=mypw;" + "Database=dbpath;" + "DataSource=serverip;" + "Port=dbport;" + "Dialect=3;" + "Charset=UTF8;");
- }
- public void showDbError(string theError)
- {
- MessageBox.Show("Could not connect to databasennError Details:n" + theError);
- }
- public FbConnection Openconn()
- {
- if (conn.State == ConnectionState.Closed || conn.State == ConnectionState.Broken)
- {
- try
- {
- conn.Open();
- }
- catch (Exception e)
- {
- showDbError(e.Message.ToString());
- }
- }
- return conn;
- }
- public FbConnection Closeconn()
- {
- if (conn.State == ConnectionState.Open)
- {
- conn.Close();
- }
- return conn;
- }
- public void nonQuery(string txtQuery)
- {
- FbCommand cmd = new FbCommand(txtQuery);
- try
- {
- cmd.Connection = Openconn();
- cmd.ExecuteNonQuery();
- }
- catch (Exception Ex)
- {
- showDbError(Ex.Message.ToString());
- throw Ex;
- }
- finally
- {
- cmd = null;
- }
- }
- public FbDataReader returnDataReader(string txtQuery)
- {
- FbCommand cmd = new FbCommand();
- try
- {
- cmd.Connection = Openconn();
- cmd.CommandText = txtQuery;
- FbDataReader rd;
- rd = cmd.ExecuteReader();
- return rd;
- }
- catch (Exception Ex)
- {
- showDbError(Ex.Message.ToString());
- throw Ex;
- }
- finally
- {
- cmd = null;
- }
- }
- }
- }
- public void nonQuery(string txtQuery)
- {
- using(var conn = new FbConnection(GetMyConnectionString(...parameters...)))
- {
- using(var cmd = new FbCommand(txtQuery))
- {
- try
- {
- cmd.Connection = conn;
- conn.Open();
- cmd.ExecuteNonQuery();
- }
- catch (Exception ex)
- {
- showDbError(ex.Message);
- throw;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment