Advertisement

06.07.2008 at 03:18PM PDT, ID: 23466565
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.5

SQL Server CLR Procedure Problem

Asked by andrepires in SQL Server 2005, C# Programming Language

Tags: , , ,

Hello friends.
I'm trying to create a CLR procedure that should return data combined from 2 tables called Pessoas and Documentos (Pessoas and Documentos stands for Persons and Documents).
When I execute the procedure it gives an error saying that a DataReader that uses the Command is already open and
should be closed first.
After some research I found that the CRL uses the context connection which allows only one reader using the connection each time, so now I have a big problem...
Question 1)
Is there any way to user a connection other than the context connection in a CLR procedure?

Question 2)
Please, take a look on the  attached code and tell me if there a way I can implement it:

Thanks for helping!Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
[Microsoft.SqlServer.Server.SqlProcedure]
    public static void spMontaMala()
    {
        SqlCommand cmdPessoas = null;
        SqlDataReader reader = null;
        SqlDataRecord record = null;
 
        record = new SqlDataRecord(CreateFields());
 
        ArrayList campos = new ArrayList();
 
        SqlPipe pipe = SqlContext.Pipe;
        pipe.SendResultsStart(record);
 
        using (SqlConnection cn = new SqlConnection("context connection=true"))
        {
            try
            {
                
                cn.Open();
                cmdPessoas = new SqlCommand("SELECT Id, NomeRazao FROM Pessoas WHERE Status = 'A'", cn);
                reader = cmdPessoas.ExecuteReader();
                while (reader.Read())
                {
                    record.SetInt32(record.GetOrdinal("Id"), reader.GetInt32(reader.GetOrdinal("Id")));
                    record.SetString(record.GetOrdinal("NomeRazao"), reader.GetString(reader.GetOrdinal("NomeRazao")));
 
                    int pessoa = reader.GetInt32(reader.GetOrdinal("Id"));
 
                    SetDocuments(pessoa, ref record, cn);
                    pipe.SendResultsRow(record);
                }
                pipe.SendResultsEnd();
            }
            catch
            {
            }
            finally
            {
                reader.Close();
            }
 
        }
 
    }
 
    private static SqlMetaData[] CreateFields()
    {
        SqlMetaData[] campos = new SqlMetaData[19];
 
        SqlMetaData campo;
        //Pessoa
        campo = new SqlMetaData("Id", SqlDbType.Int);
        campos[0] = campo;
        campo = new SqlMetaData("NomeRazao", SqlDbType.VarChar, 100);
        campos[1] = campo;
        //Documentos
        campo = new SqlMetaData("DocIdentidade", SqlDbType.NVarChar, 20);
        campos[2] = campo;
        campo = new SqlMetaData("DocCPF", SqlDbType.NVarChar, 20);
        campos[3] = campo;
        campo = new SqlMetaData("DocCNPJ", SqlDbType.NVarChar, 20);
        campos[4] = campo;
        campo = new SqlMetaData("DocInscrEstad", SqlDbType.NVarChar, 20);
        campos[5] = campo;
        campo = new SqlMetaData("DocHabilitacao", SqlDbType.NVarChar, 20);
        campos[6] = campo;
        campo = new SqlMetaData("DocCTrabalho", SqlDbType.NVarChar, 20);
        campos[7] = campo;
        campo = new SqlMetaData("DocPassaporte", SqlDbType.NVarChar, 20);
        campos[8] = campo;
        //Endereço
        campo = new SqlMetaData("EndCEP", SqlDbType.Int);
        campos[9] = campo;
        campo = new SqlMetaData("EndRuaAV", SqlDbType.VarChar, 100);
        campos[10] = campo;
        campo = new SqlMetaData("EndComplemento", SqlDbType.VarChar, 50);
        campos[11] = campo;
        campo = new SqlMetaData("EndBairro", SqlDbType.VarChar, 50);
        campos[12] = campo;
        campo = new SqlMetaData("EndCidade", SqlDbType.VarChar, 40);
        campos[13] = campo;
        campo = new SqlMetaData("EndEstado", SqlDbType.Char, 2);
        campos[14] = campo;
        campo = new SqlMetaData("EndPais", SqlDbType.Char, 2);
        campos[15] = campo;
        //Formas de Contato
        campo = new SqlMetaData("Telefone", SqlDbType.NVarChar, 40);
        campos[16] = campo;
        campo = new SqlMetaData("Celular", SqlDbType.NVarChar, 40);
        campos[17] = campo;
        campo = new SqlMetaData("Email", SqlDbType.NVarChar, 40);
        campos[18] = campo;
 
        return campos;
    }
 
    private static void SetDocuments(int Pessoa, ref SqlDataRecord record, SqlConnection cn)
    {
        SqlPipe pipe = SqlContext.Pipe;
        SqlCommand cmdDocumentos;
        SqlDataReader reader = null;
 
        try
        {
            cmdDocumentos = new SqlCommand("SELECT NumeroDoc, TipoDoc from Documentos WHERE Pessoa = " + Pessoa, cn);
            reader = cmdDocumentos.ExecuteReader();
            while (reader.Read())
            {
                int TipoDoc = reader.GetInt32(reader.GetOrdinal("TipoDoc"));
                switch (TipoDoc)
                {
                    case 1://Identidade
                        record.SetString(record.GetOrdinal("DocIdentidade"), reader.GetString(reader.GetOrdinal("NumeroDoc")));
                        break;
                    case 2://CPF
                        record.SetString(record.GetOrdinal("DocCPF"), reader.GetString(reader.GetOrdinal("NumeroDoc")));
                        break;
                    case 3://CNPJ
                        record.SetString(record.GetOrdinal("DocCNPJ"), reader.GetString(reader.GetOrdinal("NumeroDoc")));
                        break;
                    case 4://Inscrição Estadual
                        record.SetString(record.GetOrdinal("DocInscrEstad"), reader.GetString(reader.GetOrdinal("NumeroDoc")));
                        break;
                    case 5://Habilitação
                        record.SetString(record.GetOrdinal("DocHabilitacao"), reader.GetString(reader.GetOrdinal("NumeroDoc")));
                        break;
                    case 6://Cateira de Trabalho
                        record.SetString(record.GetOrdinal("DocCTrabalho"), reader.GetString(reader.GetOrdinal("NumeroDoc")));
                        break;
                    case 7://Passaporte
                        record.SetString(record.GetOrdinal("DocPassaporte"), reader.GetString(reader.GetOrdinal("NumeroDoc")));
                        break;
                }
            }
        }
        catch (Exception ex)
        {
            if (pipe.IsSendingResults) { pipe.SendResultsEnd(); }
            pipe.Send(ex.Message);
        }
        finally
        {
            reader.Close();
        }
    }
[+][-]06.09.2008 at 12:53PM PDT, ID: 21746095

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: SQL Server 2005, C# Programming Language
Tags: CLR Procedure, SQL Server 2005, C#, .Net
Sign Up Now!
Solution Provided By: junges
Participating Experts: 1
Solution Grade: A
 
 
[+][-]06.09.2008 at 01:13PM PDT, ID: 21746248

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_EXPERT_20070906