powered by simpleCommunicator - 2.0.51     © 2025 Programmizd 02
Форумы / ADO.NET, LINQ, Entity Framework, NHibernate, DAL, ORM [игнор отключен] [закрыт для гостей] / CLR SQL Server User-Defined Function
2 сообщений из 2, страница 1 из 1
CLR SQL Server User-Defined Function
    #37882306
Фотография Ex_Soft
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Чей-то тихо помирает на
Код: c#
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.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Text;
using Microsoft.SqlServer.Server;

namespace TestSQLCLRFunction
{
    public class TestSQLCLRFunction
    {
        [Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
        public static SqlString GetIds(SqlString tableName, SqlInt32 department)
        {
            string
                result = string.Format("tableName: \"{0}\", department: {1} (SqlContext.Pipe{2}=null)", tableName, department, SqlContext.Pipe != null ? "!" : "=");

            StringBuilder
                sb = new StringBuilder();

            if(SqlContext.Pipe!=null)
                SqlContext.Pipe.Send(string.Format("tableName: \"{0}\", department: {1}", tableName, department));

            using (SqlConnection conn = new SqlConnection("context connection=true"))
            {
                using (SqlCommand cmd = new SqlCommand(string.Format("select id from {0} where dep=@dep", tableName), conn))
                {
                    try
                    {
                        conn.Open();
                        result += " " + string.Format("SqlConnection.State = \"{0}\"", conn.State);

                        cmd.Parameters.Add("@dep", SqlDbType.Int).SqlValue = department;

                        result += " " + cmd.CommandText + string.Format(" \"{0}\" = {1} ({2})", cmd.Parameters[0].ParameterName, cmd.Parameters[0].SqlValue, cmd.Parameters[0].SqlDbType);

                        result += " before SqlCommand.ExecuteReader()";
                        /*using (*/
                        SqlDataReader rdr = cmd.ExecuteReader();/*)*/
                        result += " after SqlCommand.ExecuteReader()";
                        /*{*/
                            result += " " + string.Format("SqlDataReader.HasRows = {0}", rdr.HasRows);

                            while (rdr.Read())
                                sb.Append(string.Format(",{0}", rdr.GetSqlInt64(0)));

                            rdr.Close();
                        /*}*/

                        result += " after";
                    }
                    catch (Exception eException)
                    {
                        if (SqlContext.Pipe != null)
                            SqlContext.Pipe.Send(result = "Error executing SQL statement: " + eException.Message);
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }

            return new SqlString(/*!string.IsNullOrEmpty(result = sb.ToString()) ? result.Substring(1) : */result);
        }
    }
}


select dbo.getStaffIds(N'Staff', 1)tableName: "Staff", department: 1 (SqlContext.Pipe==null) SqlConnection.State = "Open" select id from Staff where dep=@dep "@dep" = 1 (Int) before SqlCommand.ExecuteReader()

Отдельно код работает. Кто виноватЪ? Что делатЪ?
_________________
"Helo, word!" - 17 errors 56 warnings
...
Рейтинг: 0 / 0
CLR SQL Server User-Defined Function
    #37883200
Фотография Ex_Soft
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Проблема была в прокладке между монитором и седушкой. Первоначальный вариант:
Код: c#
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.
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Text;
using Microsoft.SqlServer.Server;

namespace TestSQLCLRFunction
{
    public class TestSQLCLRFunction
    {
        [SqlFunction(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read)]
        public static SqlString GetIds(SqlString tableName, SqlInt32 department)
        {
            string
                result;

            StringBuilder
                sb = new StringBuilder();

            using (SqlConnection conn = new SqlConnection("context connection=true"))
            {
                using (SqlCommand cmd = new SqlCommand(string.Format("select id from {0} where dep=@dep", tableName), conn))
                {
                    conn.Open();
                    cmd.Parameters.AddWithValue("@dep", department);
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        while (rdr.Read())
                            sb.Append(string.Format(",{0}", rdr.GetSqlInt64(0)));
                    }
                }
            }

            return new SqlString(!string.IsNullOrEmpty(result = sb.ToString()) ? result.Substring(1) : result);
        }
    }
}


фунциклирует без проблем...
...
Рейтинг: 0 / 0
2 сообщений из 2, страница 1 из 1
Форумы / ADO.NET, LINQ, Entity Framework, NHibernate, DAL, ORM [игнор отключен] [закрыт для гостей] / CLR SQL Server User-Defined Function
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]