Mês: julho 2007
Proxy Para Enterprise Library- Application Block 3.0
Dim db As Database = DatabaseFactory.CreateDatabase()
Dim sqlCommand As String = txtProcedure
Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)
Catch ex As Exception
Finally
db = Nothing
dbCommand.Dispose()
Imports System.Text
Imports System.Data.Common
For linha = 0 To arrParametros.GetUpperBound(0)
Next
End Sub
(ByVal txtProcedure As String, ByVal arrParametros() As clsParametro) _
As String
Dim db As Database = DatabaseFactory.CreateDatabase()
Dim sqlCommand As String = txtProcedure
Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)
criarParametro(arrParametros, db, dbCommand, linha)
txtRetorno = db.ExecuteScalar(dbCommand)
Catch ex As Exception
Finally
db = Nothing
dbCommand.Dispose()
(ByVal txtProcedure As String, ByVal arrParametros() As clsParametro) _
As IDataReader
Dim db As Database = DatabaseFactory.CreateDatabase()
Dim sqlCommand As String = txtProcedure
Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)
Dim linha As Int32 = 0
Catch ex As Exception
(ByVal txtProcedure As String, ByVal arrParametros() As clsParametro) _
As DataTable
Dim db As Database = DatabaseFactory.CreateDatabase()
Dim sqlCommand As String = txtProcedure
Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)
Dim linha As Int32 = 0
Try
criarParametro(arrParametros, db, dbCommand, linha)
dtrRetorno = db.ExecuteDataSet(dbCommand).Tables(0)
Catch ex As Exception
db = Nothing
dbCommand.Dispose()
(ByVal txtProcedure As String, ByVal arrParametros() As clsParametro) _
As DataSet
Dim db As Database = DatabaseFactory.CreateDatabase()
Dim sqlCommand As String = txtProcedure
Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)
Dim linha As Int32 = 0
criarParametro(arrParametros, db, dbCommand, linha)
Finally
db = Nothing
dbCommand.Dispose()
(ByVal txtProcedure As String) _
As String
Dim db As Database = DatabaseFactory.CreateDatabase()
Dim sqlCommand As String = txtProcedure
Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)
Catch ex As Exception
Finally
db = Nothing
dbCommand.Dispose()
(ByVal txtProcedure As String) _
As IDataReader
Dim db As Database = DatabaseFactory.CreateDatabase()
Dim sqlCommand As String = txtProcedure
Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)
Dim linha As Int32 = 0
Catch ex As Exception
Finally
(ByVal txtProcedure As String) _
As DataTable
Dim db As Database = DatabaseFactory.CreateDatabase()
Dim sqlCommand As String = txtProcedure
Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)
Dim linha As Int32 = 0
Catch ex As Exception
Finally
db = Nothing
dbCommand.Dispose()
(ByVal txtProcedure As String) _
As DataSet
Dim db As Database = DatabaseFactory.CreateDatabase()
Dim sqlCommand As String = txtProcedure
Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)
Catch ex As Exception
Finally
db = Nothing
dbCommand.Dispose()
Criei outra classe para os parametros
Imports System.Data.Common
Imports System
Public Class clsParametro
Private _TipoParametro As DbType
Public Property TipoParametro() As DbType
Get
Return _TipoParametro
End Get
Set(ByVal value As DbType)
_TipoParametro = value
End Set
End Property
Private _Valor As Object
Public Property Valor() As Object
Get
Return _Valor
End Get
Set(ByVal value As Object)
_Valor = value
End Set
End Property
Private _NomeProcedure As String
Public Property Nome() As String
Get
Return _NomeProcedure
End Get
Set(ByVal value As String)
_NomeProcedure = value
End Set
End Property
Sub New(ByVal txtNome As String, ByVal Tipo As DbType, ByVal objValor As Object)
Nome = txtNome
TipoParametro = Tipo
Valor = objValor
End Sub
End Class
Utilizando essas classes voltamos a facilidade do Application Block 2.0, veja como fica mais simples .
DataTable dtbRetorno;
BD.clsParametro[] arrParametro = new BD.clsParametro[11];
arrParametro[0] = new BD.clsParametro("@IDSENHA", DbType.Int32, IDSENHA);
arrParametro[1] = new BD.clsParametro("@IDEMPRESA", DbType.Int32, IDEMPRESA);
arrParametro[2] = new BD.clsParametro("@IDVENDEDOR_DESTINO", DbType.Int32, IDVENDEDOR_DESTINO);
arrParametro[3] = new BD.clsParametro("@IDOS", DbType.Int32, IDOS);
arrParametro[4] = new BD.clsParametro("@NR_ITEM", DbType.Int16, NR_ITEM);
arrParametro[5] = new BD.clsParametro("@NR_SERIE", DbType.String, NR_SERIE);
arrParametro[6] = new BD.clsParametro("@OBS_MOVTO", DbType.String, OBS_MOVTO);
arrParametro[7] = new BD.clsParametro("@IDSTATUSOS_NOVO", DbType.Int32, 0);
arrParametro[8] = new BD.clsParametro("@VENDEDOR_ORIGINAL_OS", DbType.String, "F");
arrParametro[9] = new BD.clsParametro("@FINALIZAR_OS", DbType.String, FINALIZAR_OS);
arrParametro[10] = new BD.clsParametro("@CLIENTE_VISUALIZA_OBS", DbType.String, "T");
dtbRetorno = BD.clsDataBase.retornarDataTable("MANUT_OS_ENCAMINHAR", arrParametro);
No webconfig eu configuro a conexão com o banco,
<connectionStrings>
<add name="FireBird" providerName="FirebirdSql.Data.FirebirdClient" connectionString="Server=localhost;User=XXXX;Password=XXXXX;Charser=WIN1252;Database=c:\XXXXX.GDB"/>
</connectionStrings>
<dataConfiguration defaultDatabase="FireBird"/>
Assim eu continuo com o poder do novo Application Block sem perder a facilidade do antigo…
Links WINDOWS WORKFLOW FOUNDATION – WWF

Use e abuse do SQLDataSource
- Dados Pessoais
- Endereço
- Telefone
3) Criar e configurar SQLDataSource para cada região.
4) Criar um grid e um formview para endereço e outro para telefone. E criar um formview para os dados pessoais.
Agora veja a enorme quantidade de código do userControl Participante … rs
Public Partial Class ucParticipante
Inherits AkameUserControl
Enum enTipoParticipante
Segurado = 1
MotoristaSegurado = 2
Terceiro = 3
MotoristaTerceiro = 4
ProprietarioSegurado = 5
ProprietarioTerceiro = 6
TerceiroSegurado = 7
End Enum
Private _idParticipante As Integer
Public Property idParticipante() As Integer
Get
Return ViewState("_idParticipante")
End Get
Set(ByVal value As Integer)
ViewState("_idParticipante") = value
End Set
End Property
Private _TipoParticipante As enTipoParticipante
Public Property TipoParticipante() As enTipoParticipante
Get
Return _TipoParticipante
End Get
Set(ByVal value As enTipoParticipante)
_TipoParticipante = value
End Set
End Property
Private Sub sqlEndereco_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles sqlEndereco.Inserting
e.Command.Parameters(e.Command.Parameters.Count – 1).Value = idParticipante
End Sub
Private Sub sqlEndereco_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles sqlEndereco.Selecting
e.Command.Parameters(0).Value = idParticipante
End Sub
Private Sub sqlTelefone_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles sqlTelefone.Inserting
e.Command.Parameters(e.Command.Parameters.Count – 1).Value = idParticipante
End Sub
Private Sub sqlTelefone_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles sqlTelefone.Selecting
e.Command.Parameters(0).Value = idParticipante
End Sub
Private Sub sqlEmail_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles sqlEmail.Inserting
e.Command.Parameters(e.Command.Parameters.Count – 1).Value = idParticipante
End Sub
Private Sub sqlEmail_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles sqlEmail.Selecting
e.Command.Parameters(0).Value = idParticipante
End Sub
End Class
O unico trabalho que tive foi criar uma propriedade idParticipante e setar esse valor, como pode-se reparar foi necessário salvar o valor em viewstate para que ele fosse persistido. Feito isso foi só setar o valor do parametro nos eventos Selecting e Inserting , no meu caso como não altero o participante a alterção não precisei fazer o mesmo para o evento Updating.
Esse foi a maior sacada do pessoal, pois permite um flexibilidade incrivel ao SQLDataSource , melhor que isso somente se pudesse utilizar direto a propriedade idParticipante igual eu faço com session.
Veja a quantidade de código foi ridiculo, proporcionando eu fazer esse user control em menos de 1 hora. O que poderia levar até 2 dias dependendo do jeito de se fazer.
Veja como ficou o arquivo ascx, deixei somente a parte de endereço para não ocupar muito espaço. No meu caso optei por fazer a alteração direto no grid , e deixar a inclusão com o formview.
<asp:GridView ID="gvEndereco" runat="server"
DataKeyNames="id_Endereco" DataSourceID="sqlEndereco" PageSize="3">
</asp:GridView>
<asp:FormView ID="fvEndereco" runat="server" DataKeyNames="id_Endereco" DataSourceID="sqlEndereco" AllowPaging="True" DefaultMode="Insert"
EmptyDataText="Nenhum registro encontrado" >
<EditItemTemplate>
txt_CEP:
<asp:TextBox ID="txt_CEPTextBox" runat="server" Text='<%# Bind("txt_CEP") %>’>
</asp:TextBox><br />
txt_Logradouro:
<asp:TextBox ID="txt_LogradouroTextBox" runat="server" Text='<%# Bind("txt_Logradouro") %>’>
</asp:TextBox><br />
txt_Numero:
<asp:TextBox ID="txt_NumeroTextBox" runat="server" Text='<%# Bind("txt_Numero") %>’>
</asp:TextBox><br />
bln_Principal:
<asp:CheckBox ID="bln_PrincipalCheckBox" runat="server" Checked='<%# Bind("bln_Principal") %>’ /><br />
txt_TipoEndereco:
<asp:TextBox ID="txt_TipoEnderecoTextBox" runat="server" Text='<%# Bind("txt_TipoEndereco") %>’>
</asp:TextBox><br />
txt_Bairro:
<asp:TextBox ID="txt_BairroTextBox" runat="server" Text='<%# Bind("txt_Bairro") %>’>
</asp:TextBox><br />
txt_Cidade:
<asp:TextBox ID="txt_CidadeTextBox" runat="server" Text='<%# Bind("txt_Cidade") %>’>
</asp:TextBox><br />
txt_UF:
<asp:TextBox ID="txt_UFTextBox" runat="server" Text='<%# Bind("txt_UF") %>’>
</asp:TextBox><br />
txt_Complemento:
<asp:TextBox ID="txt_ComplementoTextBox" runat="server" Text='<%# Bind("txt_Complemento") %>’>
</asp:TextBox><br />
id_Endereco:
<asp:Label ID="id_EnderecoLabel1" runat="server" Text='<%# Eval("id_Endereco") %>’>
</asp:Label><br />
id_Participante:
<asp:TextBox ID="id_ParticipanteTextBox" runat="server" Text='<%# Bind("id_Participante") %>’>
</asp:TextBox><br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
Text="Update">
</asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
txt_CEP:
<asp:TextBox ID="txt_CEPTextBox" runat="server" Text='<%# Bind("txt_CEP") %>’>
</asp:TextBox><br />
txt_Logradouro:
<asp:TextBox ID="txt_LogradouroTextBox" runat="server" Text='<%# Bind("txt_Logradouro") %>’>
</asp:TextBox><br />
txt_Numero:
<asp:TextBox ID="txt_NumeroTextBox" runat="server" Text='<%# Bind("txt_Numero") %>’>
</asp:TextBox><br />
bln_Principal:
<asp:CheckBox ID="bln_PrincipalCheckBox" runat="server" Checked='<%# Bind("bln_Principal") %>’ /><br />
txt_TipoEndereco:
<asp:TextBox ID="txt_TipoEnderecoTextBox" runat="server" Text='<%# Bind("txt_TipoEndereco") %>’>
</asp:TextBox><br />
txt_Bairro:
<asp:TextBox ID="txt_BairroTextBox" runat="server" Text='<%# Bind("txt_Bairro") %>’>
</asp:TextBox><br />
txt_Cidade:
<asp:TextBox ID="txt_CidadeTextBox" runat="server" Text='<%# Bind("txt_Cidade") %>’>
</asp:TextBox><br />
txt_UF:
<asp:TextBox ID="txt_UFTextBox" runat="server" Text='<%# Bind("txt_UF") %>’>
</asp:TextBox><br />
txt_Complemento:
<asp:TextBox ID="txt_ComplementoTextBox" runat="server" Text='<%# Bind("txt_Complemento") %>’>
</asp:TextBox><br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
Text="Insert">
</asp:LinkButton>
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp:LinkButton>
</InsertItemTemplate>
<ItemTemplate>
txt_CEP:
<asp:Label ID="txt_CEPLabel" runat="server" Text='<%# Bind("txt_CEP") %>’></asp:Label><br />
txt_Logradouro:
<asp:Label ID="txt_LogradouroLabel" runat="server" Text='<%# Bind("txt_Logradouro") %>’>
</asp:Label><br />
txt_Numero:
<asp:Label ID="txt_NumeroLabel" runat="server" Text='<%# Bind("txt_Numero") %>’>
</asp:Label><br />
bln_Principal:
<asp:CheckBox ID="bln_PrincipalCheckBox" runat="server" Checked='<%# Bind("bln_Principal") %>’
Enabled="false" /><br />
txt_TipoEndereco:
<asp:Label ID="txt_TipoEnderecoLabel" runat="server" Text='<%# Bind("txt_TipoEndereco") %>’>
</asp:Label><br />
txt_Bairro:
<asp:Label ID="txt_BairroLabel" runat="server" Text='<%# Bind("txt_Bairro") %>’>
</asp:Label><br />
txt_Cidade:
<asp:Label ID="txt_CidadeLabel" runat="server" Text='<%# Bind("txt_Cidade") %>’>
</asp:Label><br />
txt_UF:
<asp:Label ID="txt_UFLabel" runat="server" Text='<%# Bind("txt_UF") %>’></asp:Label><br />
txt_Complemento:
<asp:Label ID="txt_ComplementoLabel" runat="server" Text='<%# Bind("txt_Complemento") %>’>
</asp:Label><br />
id_Endereco:
<asp:Label ID="id_EnderecoLabel" runat="server" Text='<%# Eval("id_Endereco") %>’>
</asp:Label><br />
id_Participante:
<asp:Label ID="id_ParticipanteLabel" runat="server" Text='<%# Bind("id_Participante") %>’>
</asp:Label><br />
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit">
</asp:LinkButton>
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
Text="Delete">
</asp:LinkButton>
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
Text="New">
</asp:LinkButton>
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="sqlEndereco" runat="server" ConnectionString="<%$ ConnectionStrings:LocalSqlServerASPNET %>"
DeleteCommand="DELETE FROM [tbl_proc_ParticipanteEndereco] WHERE [id_Endereco] = @id_Endereco"
InsertCommand="INSERT INTO [tbl_proc_ParticipanteEndereco] ([txt_Logradouro], [txt_Numero], [txt_Complemento], [txt_CEP], [txt_Bairro], [txt_Cidade], [txt_UF], [txt_TipoEndereco], [bln_Principal], [id_Participante]) VALUES (@txt_Logradouro, @txt_Numero, @txt_Complemento, @txt_CEP, @txt_Bairro, @txt_Cidade, @txt_UF, @txt_TipoEndereco, @bln_Principal, @id_Participante)"
SelectCommand="SELECT [txt_Logradouro], [txt_Numero], [txt_Complemento], [txt_CEP], [txt_Bairro], [txt_Cidade], [txt_UF], [txt_TipoEndereco], [bln_Principal], [id_Participante], [id_Endereco] FROM [tbl_proc_ParticipanteEndereco] WHERE ([id_Participante] = @id_Participante)"
UpdateCommand="UPDATE [tbl_proc_ParticipanteEndereco] SET [txt_Logradouro] = @txt_Logradouro, [txt_Numero] = @txt_Numero, [txt_Complemento] = @txt_Complemento, [txt_CEP] = @txt_CEP, [txt_Bairro] = @txt_Bairro, [txt_Cidade] = @txt_Cidade, [txt_UF] = @txt_UF, [txt_TipoEndereco] = @txt_TipoEndereco, [bln_Principal] = @bln_Principal WHERE [id_Endereco] = @id_Endereco">
<DeleteParameters>
<asp:Parameter Name="id_Endereco" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="txt_Logradouro" Type="String" />
<asp:Parameter Name="txt_Numero" Type="String" />
<asp:Parameter Name="txt_Complemento" Type="String" />
<asp:Parameter Name="txt_CEP" Type="String" />
<asp:Parameter Name="txt_Bairro" Type="String" />
<asp:Parameter Name="txt_Cidade" Type="String" />
<asp:Parameter Name="txt_UF" Type="String" />
<asp:Parameter Name="txt_TipoEndereco" Type="String" />
<asp:Parameter Name="bln_Principal" Type="Boolean" />
<asp:Parameter Name="id_Endereco" Type="Int32" />
</UpdateParameters>
<SelectParameters>
<asp:Parameter Name="id_Participante" Type="Int32" />
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="txt_Logradouro" Type="String" />
<asp:Parameter Name="txt_Numero" Type="String" />
<asp:Parameter Name="txt_Complemento" Type="String" />
<asp:Parameter Name="txt_CEP" Type="String" />
<asp:Parameter Name="txt_Bairro" Type="String" />
<asp:Parameter Name="txt_Cidade" Type="String" />
<asp:Parameter Name="txt_UF" Type="String" />
<asp:Parameter Name="txt_TipoEndereco" Type="String" />
<asp:Parameter Name="bln_Principal" Type="Boolean" />
<asp:Parameter Name="id_Participante" Type="Int32" />
</InsertParameters>
</asp:SqlDataSource>
Links Uteis sobre ASP.NET 2.0
- www.codeproject.com
- www.15seconds.com
- www.4guysfromrolla (eita nome … )
- www.asp.net
- msdn.microsoft.com (normalmente uso a versão americana)
- www.linhadecodigo.com.br
- www.codeplex.com
Fora isso costumo sempre dar uma olha nos blogs do ScotGu, Tarifa, Israel …