Classe de conexão
Falae galera beleza? bom fiz uma classe de conexão com o sql server que achei interessante compartilhar com a galera.
Espero receber algumas melhorias nesta classe e também espero que possa ajudar vocês no desenvolvimento.
Flwww
Grande abraço a todos.
VB.NET
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.DataTable
Public Class Conexao
Private connectionString As String
Private connection As SqlConnection
Private command As SqlCommand
Public Sub open()
Me.connectionString = "ADICIONE AQUI SUA CONNECTION STRING"
Me.connection = New SqlConnection(Me.connectionString)
Me.connection.Open()
End Sub
Public Sub executeQuery(ByVal query As String)
Me.command = New SqlCommand()
Me.command.Connection = Me.connection
Me.command.CommandText = query
Me.command.CommandType = CommandType.Text
Me.command.ExecuteNonQuery()
End Sub
Public Function getResult() As System.Data.DataTable
Dim dataTable As DataTable
Dim dataReader As SqlDataReader
dataTable = New DataTable()
dataReader = Me.command.ExecuteReader(CommandBehavior.CloseConnection)
dataTable.Load(dataReader, LoadOption.OverwriteChanges)
dataReader.Close()
dataReader.Dispose()
Return (dataTable)
End Function
Public Sub close()
If ((Me.connection Is Nothing) = False) Then
If (Me.connection.State = ConnectionState.Open) Then
Me.connection.Close()
End If
End If
End Sub
End Class
C#
using System;
using System.Data;
using System.Data.SqlClient;
namespace br.conexao
{
public class Conexao
{
private String connectionString;
private SqlConnection connection;
private SqlCommand command;
public void open()
{
this.connectionString = "ADICIONE AQUI SUA CONNECTION STRING";
this.connection = new SqlConnection(this.connectionString);
this.connection.Open();
}
public void executeQuery(String query)
{
this.command = new SqlCommand();
this.command.Connection = this.connection;
this.command.CommandText = query;
this.command.CommandType = CommandType.Text;
this.command.ExecuteNonQuery();
}
public System.Data.DataTable getResult()
{
DataTable dataTable;
SqlDataReader dataReader;
dataTable = new DataTable();
dataReader =this.command.ExecuteReader(CommandBehavior.CloseConnection);
dataTable.Load(dataReader, LoadOption.OverwriteChanges);
dataReader.Close();
dataReader.Dispose();
return (dataTable);
}
public void close()
{
if (this.connection.Equals(null) == false)
{
if (this.connection.State == ConnectionState.Open)
{
this.connection.Close();
}
}
}
}
}Discussão (36)
Carregando comentários...