Showing posts with label row. Show all posts
Showing posts with label row. Show all posts

Monday, March 19, 2012

CLR TVF Error: Msg 6260, Level 16, State 1, Line 1

Dear All, I always got this error in CLR TVF:

Msg 6260, Level 16, State 1, Line 1
An error occurred while getting new row from user defined Table Valued Function :
System.InvalidOperationException: Invalid attempt to FieldCount when reader is closed.
System.InvalidOperationException:
at System.Data.SqlClient.SqlDataReaderSmi.get_FieldCount()
at System.Data.Common.DbEnumerator.BuildSchemaInfo()
at System.Data.Common.DbEnumerator.MoveNext()

Here is my code:

using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using Microsoft.SqlServer.Server;
using System.Collections;


public static class UserDefinedFunctions
{
[Microsoft.SqlServer.Server.SqlFunction(FillRowMethodName = "rowfiller",DataAccess=DataAccessKind.Read,TableDefinition = "ActID int, ActName nvarchar(50), ActCreatorID int,ActDesp nvarchar(200),ActCreateDate datetime,ActModifyDate datetime, ActStartDate datetime, ActEndDate datetime, Status int, Cost int")]
public static IEnumerable Func_GetSchCatActivityIDTable(int CatActivityID)
{
using (SqlConnection connection = new SqlConnection("context connection=true"))
{
string sqlstring = "select * from Activity where CatActivityID=@.CatActivityID;";

connection.Open();
SqlCommand command = new SqlCommand(sqlstring, connection);
command.Parameters.AddWithValue("@.CatActivityID", CatActivityID);

return command.ExecuteReader(CommandBehavior.CloseConnection);

}
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft Performance","CA1811:AvoidUncalledPrivateCode")]
public static void rowfiller(Object obj,
out SqlInt32 ActID,
out SqlString ActName,
out SqlInt32 ActCreatorID,
out SqlString ActDesp,
out SqlDateTime ActCreateDate,
out SqlDateTime ActModifyDate,
out SqlDateTime ActStartDate,
out SqlDateTime ActEndDate,
out SqlInt32 Status,
out SqlInt32 Cost,
)
{

SqlDataRecord row = (SqlDataRecord)obj;
int column = 0;


ActID = (row.IsDBNull(column)) ? SqlInt32.Null : new SqlInt32(row.GetInt32(column)); column++;
ActName = (row.IsDBNull(column)) ? SqlString.Null : new SqlString(row.GetString(column)); column++;
ActCreatorID = (row.IsDBNull(column)) ? SqlInt32.Null : new SqlInt32(row.GetInt32(column)); column++;
ActDesp = (row.IsDBNull(column)) ? SqlString.Null : new SqlString(row.GetString(column)); column++;
ActCreateDate = (row.IsDBNull(column)) ? SqlDateTime.Null : new SqlDateTime(row.GetDateTime(column)); column++;
ActModifyDate = (row.IsDBNull(column)) ? SqlDateTime.Null : new SqlDateTime(row.GetDateTime(column)); column++;
ActStartDate = (row.IsDBNull(column)) ? SqlDateTime.Null : new SqlDateTime(row.GetDateTime(column)); column++;
ActEndDate = (row.IsDBNull(column)) ? SqlDateTime.Null : new SqlDateTime(row.GetDateTime(column)); column++;
Status = (row.IsDBNull(column)) ? SqlInt32.Null : new SqlInt32(row.GetInt32(column)); column++;
Cost = (row.IsDBNull(column)) ? SqlInt32.Null : new SqlInt32(row.GetInt32(column)); column++;
}

};

Can anyone tell me what I am doing wrong? Many thanks

.

You can not do data access from a Data Reader in a CLR TVF. Basically, after accessing the first row in the reader the data reader closes down.

Niels
|||Just a correction, as soon as you leave the TVF method the datareader closes down - you won't be able to get even the first row.

Niels
|||

Hi, Niels

Thanks for your responding. Do you have any idea how to get arround this problem, if I do need a table value by query other tables?

|||Use T-SQL!! T-SQL is much, much better than CLR for in-database data access anyway. From your code above I ca not see any reason per se to use CLR (however, there may be more stuff going on than what you show in your code).

Niels

CLR Trigger to write file.......

Hello, theres,

I have a request to write to a file whenever new record added to the table.

When records insert row by row, it goes well. but when more than 2 session insert at the same time, sometimes it duplicate some record in the file. I try to add synchonize code ( like lock , Monitor) but it doesn't work. any idea ?

Regards,

Agi

I assume you are using this to generate an Audit log or something similar. Here is the route I took.

http://sqljunkies.com/Article/4CD01686-5178-490C-A90A-5AEEF5E35915.scuk

|||

Jonathan,

Thanks for your reply, I just want to write everything to the flat file whenever new record inserted to my table. I write a trigger using c#. I just wonder if more than 2 threads (sessions) insert new record at the same time, what will be write to the flat file ?

Regards,

Agi

|||Hi,
I think the “CLR Triggers for SQL Server 2005” article on
http://aspalliance.com/1273_CLR_Triggers_for_SQL_Server_2005.all
may be helpful in this discussion.

This popular white paper is written by a software engineer from our organization Mindfire Solutions (http://www.mindfiresolutions.com).

I hope you find it useful!

Cheers,
Byapti

Sunday, March 11, 2012

CLR test script SELECT returns no row data

Hi,

The test.sql scripts I write to test CLR stored procedures run successfully, but when I want to display the resulting data in the database with a simple "SELECT * from Employee"

I get the result as:
Name Address
- -
No rows affected.
(1 row(s) returned)

But not the actual row is displayed whereas I would expect to see something like:

Name Address
- -
John Doe
No rows affected.
(1 row(s) returned)

I have another database project where doing the same thing displays the row information but there doesn't seem to be a lot different between the two.

Why no results in first case?

Thanks,
Bahadir
You maybe still have the transaction open and uncommitted, thats why you don′t see the actual row.

HTH; Jens SUessmeyer.

http://www.sqlserver2005.de

Saturday, February 25, 2012

Clogged Database?

Hi,
I am having a really strange situation in my database, where a table
seems "clogged" until I manually insert a row to that table using
Enterprise Manager.
I have MSMQ set up so that it receives transactions pending for
insertion. When someone complains that is not seeing data in the apps, i
see that the MSMQ is loaded with data. As part of the troubleshooting, i
start testing connnections and test queries to diagnose the problem.
However, as soon as I insert a row to the problematic table, the MSMQ
empties agaig
Can you please give me some light regarding this problem'
Thanks, and Happy Thanksgiving.
--eval
Until then The database starts timing outHave you looked to see if someone is blocking the table? It's possable you
began a transaction with EM some how and didn't commit it. I have seen
similiar issues when people use EM to view the rows and if you touch any of
the data it can put a lock that you are not aware of.
Andrew J. Kelly SQL MVP
"eval" <eval@.eval.com> wrote in message
news:%23wL3JUj0EHA.3840@.TK2MSFTNGP10.phx.gbl...
> Hi,
> I am having a really strange situation in my database, where a table seems
> "clogged" until I manually insert a row to that table using Enterprise
> Manager.
> I have MSMQ set up so that it receives transactions pending for insertion.
> When someone complains that is not seeing data in the apps, i see that the
> MSMQ is loaded with data. As part of the troubleshooting, i start testing
> connnections and test queries to diagnose the problem. However, as soon as
> I insert a row to the problematic table, the MSMQ empties agaig
> Can you please give me some light regarding this problem'
> Thanks, and Happy Thanksgiving.
> --eval
> Until then The database starts timing out|||Actually it has happened sporadically in the past several weeks, and
happens when no one is connected to the database (usually very early in
the morning).
Andrew J. Kelly wrote:
> Have you looked to see if someone is blocking the table? It's possable yo
u
> began a transaction with EM some how and didn't commit it. I have seen
> similiar issues when people use EM to view the rows and if you touch any o
f
> the data it can put a lock that you are not aware of.
>|||"eval" <eval@.eval.com> wrote in message
news:elZKNlj0EHA.4028@.TK2MSFTNGP15.phx.gbl...
> Actually it has happened sporadically in the past several weeks, and
> happens when no one is connected to the database (usually very early in
> the morning).
>
Next time it happens, before you do your "fix" check for blocking.
At the very least a sp_who2 may give you some useful info.
[vbcol=seagreen]
> Andrew J. Kelly wrote:
you[vbcol=seagreen]
of[vbcol=seagreen]

Clogged Database?

Hi,
I am having a really strange situation in my database, where a table
seems "clogged" until I manually insert a row to that table using
Enterprise Manager.
I have MSMQ set up so that it receives transactions pending for
insertion. When someone complains that is not seeing data in the apps, i
see that the MSMQ is loaded with data. As part of the troubleshooting, i
start testing connnections and test queries to diagnose the problem.
However, as soon as I insert a row to the problematic table, the MSMQ
empties agaig
Can you please give me some light regarding this problem?
Thanks, and Happy Thanksgiving.
--eval
Until then The database starts timing out
Have you looked to see if someone is blocking the table? It's possable you
began a transaction with EM some how and didn't commit it. I have seen
similiar issues when people use EM to view the rows and if you touch any of
the data it can put a lock that you are not aware of.
Andrew J. Kelly SQL MVP
"eval" <eval@.eval.com> wrote in message
news:%23wL3JUj0EHA.3840@.TK2MSFTNGP10.phx.gbl...
> Hi,
> I am having a really strange situation in my database, where a table seems
> "clogged" until I manually insert a row to that table using Enterprise
> Manager.
> I have MSMQ set up so that it receives transactions pending for insertion.
> When someone complains that is not seeing data in the apps, i see that the
> MSMQ is loaded with data. As part of the troubleshooting, i start testing
> connnections and test queries to diagnose the problem. However, as soon as
> I insert a row to the problematic table, the MSMQ empties agaig
> Can you please give me some light regarding this problem?
> Thanks, and Happy Thanksgiving.
> --eval
> Until then The database starts timing out
|||Actually it has happened sporadically in the past several weeks, and
happens when no one is connected to the database (usually very early in
the morning).
Andrew J. Kelly wrote:
> Have you looked to see if someone is blocking the table? It's possable you
> began a transaction with EM some how and didn't commit it. I have seen
> similiar issues when people use EM to view the rows and if you touch any of
> the data it can put a lock that you are not aware of.
>
|||"eval" <eval@.eval.com> wrote in message
news:elZKNlj0EHA.4028@.TK2MSFTNGP15.phx.gbl...
> Actually it has happened sporadically in the past several weeks, and
> happens when no one is connected to the database (usually very early in
> the morning).
>
Next time it happens, before you do your "fix" check for blocking.
At the very least a sp_who2 may give you some useful info.
[vbcol=seagreen]
> Andrew J. Kelly wrote:
you[vbcol=seagreen]
of[vbcol=seagreen]

Clogged Database?

Hi,
I am having a really strange situation in my database, where a table
seems "clogged" until I manually insert a row to that table using
Enterprise Manager.
I have MSMQ set up so that it receives transactions pending for
insertion. When someone complains that is not seeing data in the apps, i
see that the MSMQ is loaded with data. As part of the troubleshooting, i
start testing connnections and test queries to diagnose the problem.
However, as soon as I insert a row to the problematic table, the MSMQ
empties agaig
Can you please give me some light regarding this problem'
Thanks, and Happy Thanksgiving.
--eval
Until then The database starts timing outHave you looked to see if someone is blocking the table? It's possable you
began a transaction with EM some how and didn't commit it. I have seen
similiar issues when people use EM to view the rows and if you touch any of
the data it can put a lock that you are not aware of.
--
Andrew J. Kelly SQL MVP
"eval" <eval@.eval.com> wrote in message
news:%23wL3JUj0EHA.3840@.TK2MSFTNGP10.phx.gbl...
> Hi,
> I am having a really strange situation in my database, where a table seems
> "clogged" until I manually insert a row to that table using Enterprise
> Manager.
> I have MSMQ set up so that it receives transactions pending for insertion.
> When someone complains that is not seeing data in the apps, i see that the
> MSMQ is loaded with data. As part of the troubleshooting, i start testing
> connnections and test queries to diagnose the problem. However, as soon as
> I insert a row to the problematic table, the MSMQ empties agaig
> Can you please give me some light regarding this problem'
> Thanks, and Happy Thanksgiving.
> --eval
> Until then The database starts timing out|||Actually it has happened sporadically in the past several weeks, and
happens when no one is connected to the database (usually very early in
the morning).
Andrew J. Kelly wrote:
> Have you looked to see if someone is blocking the table? It's possable you
> began a transaction with EM some how and didn't commit it. I have seen
> similiar issues when people use EM to view the rows and if you touch any of
> the data it can put a lock that you are not aware of.
>|||"eval" <eval@.eval.com> wrote in message
news:elZKNlj0EHA.4028@.TK2MSFTNGP15.phx.gbl...
> Actually it has happened sporadically in the past several weeks, and
> happens when no one is connected to the database (usually very early in
> the morning).
>
Next time it happens, before you do your "fix" check for blocking.
At the very least a sp_who2 may give you some useful info.
> Andrew J. Kelly wrote:
> > Have you looked to see if someone is blocking the table? It's possable
you
> > began a transaction with EM some how and didn't commit it. I have seen
> > similiar issues when people use EM to view the rows and if you touch any
of
> > the data it can put a lock that you are not aware of.
> >