Monday, March 26, 2012
Problem in Context connection of CLR Trigger
I'm using June CTP Visual Studio 2005.
I have Created CLR Object. In my CLR Method i have opened a
connection like below
public partial class Triggers
{
//In my CLR Method i have Opened the connection like below
[Microsoft.SqlServer.Server.SqlTrigger(Name="ClrTrigger",
Target="Triplets",Event="FOR INSERT")]
public static void CLRTrigger()
{
Triggers objTriggers = new Triggers();
objTriggers.Connection(true);
..
...
Test();
// Here i'm calling one more method Test();
...
...
objTriggers.Connection(false);
}
In this test method i'm Executing the command.
Public void Test()
{
...
SqlCommand cmd = new SqlCommand();
string cmdText = "select dirname from Docs where id=45";
cmd.CommandText = cmdText;
cmd.Connection = con;
try
{
sdr = cmd.ExecuteReader();
}
catch (Exception exp)
{
WriteLog("R E ::: "+exp.Message);
}
.....
}
//Connection open and Close method
public void Connection(bool status)
{
if (status)
{
con = new SqlConnection("context connection = true");
con.Open();
}
else
{
con.Close();
}
}
}
I'm getting the error of Connection already in use. I have properly
opened the connection in begin of
method and Closed in end of method. Inside the execution of method if i
called some method means then it
can also use the same connection. Then Why its giving this error.
Please anybody knows solution to my
problem let me know to solve it.
Thanks,
Vinoth
vinoth@.gsdindia.comHi
Support for SQL Server 2005:
http://communities.microsoft.com/ne...p=sqlserver2005
--
--
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland
MVP Program: http://www.microsoft.com/mvp
Blog: http://www.msmvps.com/epprecht/
"None" wrote:
> Hi,
> I'm using June CTP Visual Studio 2005.
> I have Created CLR Object. In my CLR Method i have opened a
> connection like below
> public partial class Triggers
> {
> //In my CLR Method i have Opened the connection like below
> [Microsoft.SqlServer.Server.SqlTrigger(Name="ClrTrigger",
> Target="Triplets",Event="FOR INSERT")]
> public static void CLRTrigger()
> {
> Triggers objTriggers = new Triggers();
> objTriggers.Connection(true);
> ...
> ....
> Test();
> // Here i'm calling one more method Test();
> ...
> ...
> objTriggers.Connection(false);
> }
> In this test method i'm Executing the command.
> Public void Test()
> {
> ...
> SqlCommand cmd = new SqlCommand();
> string cmdText = "select dirname from Docs where id=45";
> cmd.CommandText = cmdText;
> cmd.Connection = con;
> try
> {
> sdr = cmd.ExecuteReader();
> }
> catch (Exception exp)
> {
> WriteLog("R E ::: "+exp.Message);
> }
> ......
> }
> //Connection open and Close method
> public void Connection(bool status)
> {
> if (status)
> {
> con = new SqlConnection("context connection = true");
> con.Open();
> }
> else
> {
> con.Close();
> }
> }
> }
> I'm getting the error of Connection already in use. I have properly
> opened the connection in begin of
> method and Closed in end of method. Inside the execution of method if i
> called some method means then it
> can also use the same connection. Then Why its giving this error.
> Please anybody knows solution to my
> problem let me know to solve it.
> Thanks,
> Vinoth
> vinoth@.gsdindia.com
>sql
Problem in Context connection of CLR Trigger
I'm using June CTP Visual Studio 2005.
I have Created CLR Object. In my CLR Method i have
opened a
connection like below
public partial class Triggers
{
//In my CLR Method i have Opened the connection like below
[Microsoft.SqlServer.Server.SqlTrigger(Name="ClrTrigger",
Target="Triplets",Event="FOR INSERT")]
public static void CLRTrigger()
{
Triggers objTriggers = new Triggers();
objTriggers.Connection(true);
...
.....
Test();
// Here i'm calling one more method Test();
....
....
objTriggers.Connection(false);
}
In this test method i'm Executing the command.
Public void Test()
{
....
SqlCommand cmd = new SqlCommand();
string cmdText = "select dirname from Docs where id=45";
cmd.CommandText = cmdText;
cmd.Connection = con;
try
{
sdr = cmd.ExecuteReader();
}
catch (Exception exp)
{
WriteLog("R E ::: "+exp.Message);
}
.......
}
//Connection open and Close method
public void Connection(bool status)
{
if (status)
{
con = new SqlConnection("context connection = true");
con.Open();
}
else
{
con.Close();
}
}
}
I'm getting the error of Connection already in use. I have
properly
opened the connection in begin of
method and Closed in end of method. Inside the execution
of method if i
called some method means then it
can also use the same connection. Then Why its giving this
error.
Please anybody knows solution to my
problem let me know to solve it.
Thanks,
Vinoth
vinoth@.gsdindia.comThere are a couple of possibilities:
1) You can only have one open context connection at a time within a clr procedure ("context connection=true"). Is it possible that in the missing code snippets for your example (...'s) that you are attempting to open another context connection? For instance, calling Triggers.Connection(true) again anywhere prior to the Triggers.Connection(false) call would result in this exception. The call stack where the "Connection already in use" exception is thrown would be useful to track down this cause.
2) June CTP has a couple of bugs where an exception, especially if due to an attention (i.e. cancel) signal, can cause the connection to not properly clean itself up from one invocation of the procedure to the next. This problem would not be apparent on first invocation of the proc, and would temporarily be cleared by running "DBCC FREEPROCCACHE".
One other question I have is how you get the SqlConnection from the CLRTriggers() method to the Test() method. It would appear that the "con" field must be static, true? If this is the case, you will almost certainly run into problems, since your trigger can fire on multiple threads, yet the SqlConnection object is valid only in the procedure in which it was created.
I'd like to note that the recommended use pattern for SqlConnection (and cmd.ExecuteReader) is to put them in a "using" statement to guarantee cleanup and help see the lifetime scope of the connection. i.e.
using (SqlConnection con = new SqlConnection("context connection = true")) {
con.Open();
...
}
and
using (SqlDataReader rdr = cmd.ExecuteReader()) {
...
}
~Alazel
Saturday, February 25, 2012
Problem creating a secondary datafile on drive E:\
programming. I created a database, tsqldb, on C:\, where the ms sql server
files reside. Now, I used the alter database command to add a secondary
datafile, tsqldb_data2 to drive E:\ that I've created and that is where I
want to put this secondary datafile file, E:\. the alter database command
goes like this:
alter database tsqldb
add file
( name = tsqldb_data2,
filename = 'E:\tsqldb_data2.ndf',
size = 50mb,
maxsize = 150mb,
filegrowth = 25%
)
and this is the error I get:
Server: Msg 5123, Level 16, State 1, Line 1
CREATE FILE encountered operating system error 5(Access is denied.) while
attempting to open or create the physical file 'E:\tsqldb_data2.ndf'.
Server: Msg 5009, Level 16, State 1, Line 1
ALTER DATABASE failed. Some disk names listed in the statement were not
found. Check that the names exist and are spelled correctly before rerunning
the statement.
Extending database by 50.00 MB on disk 'tsqldb_data2'.
Can someone help me? Thanks.
DominickCheck whether the service account that runs the SQL Server instance has the
permission to create files in E:\.
Linchi
"Dominick D." wrote:
> Hi. I am a student, taking a class on MS SQL SERVER 2000 system admin and
> programming. I created a database, tsqldb, on C:\, where the ms sql server
> files reside. Now, I used the alter database command to add a secondary
> datafile, tsqldb_data2 to drive E:\ that I've created and that is where I
> want to put this secondary datafile file, E:\. the alter database command
> goes like this:
> alter database tsqldb
> add file
> ( name = tsqldb_data2,
> filename = 'E:\tsqldb_data2.ndf',
> size = 50mb,
> maxsize = 150mb,
> filegrowth = 25%
> )
> and this is the error I get:
> Server: Msg 5123, Level 16, State 1, Line 1
> CREATE FILE encountered operating system error 5(Access is denied.) while
> attempting to open or create the physical file 'E:\tsqldb_data2.ndf'.
> Server: Msg 5009, Level 16, State 1, Line 1
> ALTER DATABASE failed. Some disk names listed in the statement were not
> found. Check that the names exist and are spelled correctly before rerunning
> the statement.
> Extending database by 50.00 MB on disk 'tsqldb_data2'.
> Can someone help me? Thanks.
> Dominick|||How do I check that? I"m a newbie to SQL, file permissions, so I'd appreciate
your assistance. Thanks.
"Linchi Shea" wrote:
> Check whether the service account that runs the SQL Server instance has the
> permission to create files in E:\.
> Linchi
> "Dominick D." wrote:
> > Hi. I am a student, taking a class on MS SQL SERVER 2000 system admin and
> > programming. I created a database, tsqldb, on C:\, where the ms sql server
> > files reside. Now, I used the alter database command to add a secondary
> > datafile, tsqldb_data2 to drive E:\ that I've created and that is where I
> > want to put this secondary datafile file, E:\. the alter database command
> > goes like this:
> >
> > alter database tsqldb
> > add file
> > ( name = tsqldb_data2,
> > filename = 'E:\tsqldb_data2.ndf',
> > size = 50mb,
> > maxsize = 150mb,
> > filegrowth = 25%
> > )
> >
> > and this is the error I get:
> >
> > Server: Msg 5123, Level 16, State 1, Line 1
> > CREATE FILE encountered operating system error 5(Access is denied.) while
> > attempting to open or create the physical file 'E:\tsqldb_data2.ndf'.
> > Server: Msg 5009, Level 16, State 1, Line 1
> >
> > ALTER DATABASE failed. Some disk names listed in the statement were not
> > found. Check that the names exist and are spelled correctly before rerunning
> > the statement.
> > Extending database by 50.00 MB on disk 'tsqldb_data2'.
> >
> > Can someone help me? Thanks.
> >
> > Dominick|||Using Windows Explorer, right click on the drive or folder and bring up its
properties. It's under the Security tab.
--
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Toronto, ON Canada
.
"Dominick D." <DominickD@.discussions.microsoft.com> wrote in message
news:BE503C3C-EF2C-47DF-A91B-41C3E95959CB@.microsoft.com...
How do I check that? I"m a newbie to SQL, file permissions, so I'd
appreciate
your assistance. Thanks.
"Linchi Shea" wrote:
> Check whether the service account that runs the SQL Server instance has
> the
> permission to create files in E:\.
> Linchi
> "Dominick D." wrote:
> > Hi. I am a student, taking a class on MS SQL SERVER 2000 system admin
> > and
> > programming. I created a database, tsqldb, on C:\, where the ms sql
> > server
> > files reside. Now, I used the alter database command to add a secondary
> > datafile, tsqldb_data2 to drive E:\ that I've created and that is where
> > I
> > want to put this secondary datafile file, E:\. the alter database
> > command
> > goes like this:
> >
> > alter database tsqldb
> > add file
> > ( name = tsqldb_data2,
> > filename = 'E:\tsqldb_data2.ndf',
> > size = 50mb,
> > maxsize = 150mb,
> > filegrowth = 25%
> > )
> >
> > and this is the error I get:
> >
> > Server: Msg 5123, Level 16, State 1, Line 1
> > CREATE FILE encountered operating system error 5(Access is denied.)
> > while
> > attempting to open or create the physical file 'E:\tsqldb_data2.ndf'.
> > Server: Msg 5009, Level 16, State 1, Line 1
> >
> > ALTER DATABASE failed. Some disk names listed in the statement were not
> > found. Check that the names exist and are spelled correctly before
> > rerunning
> > the statement.
> > Extending database by 50.00 MB on disk 'tsqldb_data2'.
> >
> > Can someone help me? Thanks.
> >
> > Dominick|||Talk to the person who administers the machine in question. They should be able to check what
service account the service is using and see whether that account has permissions to create files in
the folder in question.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Dominick D." <DominickD@.discussions.microsoft.com> wrote in message
news:BE503C3C-EF2C-47DF-A91B-41C3E95959CB@.microsoft.com...
> How do I check that? I"m a newbie to SQL, file permissions, so I'd appreciate
> your assistance. Thanks.
>
> "Linchi Shea" wrote:
>> Check whether the service account that runs the SQL Server instance has the
>> permission to create files in E:\.
>> Linchi
>> "Dominick D." wrote:
>> > Hi. I am a student, taking a class on MS SQL SERVER 2000 system admin and
>> > programming. I created a database, tsqldb, on C:\, where the ms sql server
>> > files reside. Now, I used the alter database command to add a secondary
>> > datafile, tsqldb_data2 to drive E:\ that I've created and that is where I
>> > want to put this secondary datafile file, E:\. the alter database command
>> > goes like this:
>> >
>> > alter database tsqldb
>> > add file
>> > ( name = tsqldb_data2,
>> > filename = 'E:\tsqldb_data2.ndf',
>> > size = 50mb,
>> > maxsize = 150mb,
>> > filegrowth = 25%
>> > )
>> >
>> > and this is the error I get:
>> >
>> > Server: Msg 5123, Level 16, State 1, Line 1
>> > CREATE FILE encountered operating system error 5(Access is denied.) while
>> > attempting to open or create the physical file 'E:\tsqldb_data2.ndf'.
>> > Server: Msg 5009, Level 16, State 1, Line 1
>> >
>> > ALTER DATABASE failed. Some disk names listed in the statement were not
>> > found. Check that the names exist and are spelled correctly before rerunning
>> > the statement.
>> > Extending database by 50.00 MB on disk 'tsqldb_data2'.
>> >
>> > Can someone help me? Thanks.
>> >
>> > Dominick|||OK, I managed to do a little research on E:\ and I included the service
account that I set up to have full control over E:\ and it did the trick.
"Tom Moreau" wrote:
> Using Windows Explorer, right click on the drive or folder and bring up its
> properties. It's under the Security tab.
> --
> Tom
> ----
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
> SQL Server MVP
> Toronto, ON Canada
> ..
> "Dominick D." <DominickD@.discussions.microsoft.com> wrote in message
> news:BE503C3C-EF2C-47DF-A91B-41C3E95959CB@.microsoft.com...
> How do I check that? I"m a newbie to SQL, file permissions, so I'd
> appreciate
> your assistance. Thanks.
>
> "Linchi Shea" wrote:
> > Check whether the service account that runs the SQL Server instance has
> > the
> > permission to create files in E:\.
> >
> > Linchi
> >
> > "Dominick D." wrote:
> >
> > > Hi. I am a student, taking a class on MS SQL SERVER 2000 system admin
> > > and
> > > programming. I created a database, tsqldb, on C:\, where the ms sql
> > > server
> > > files reside. Now, I used the alter database command to add a secondary
> > > datafile, tsqldb_data2 to drive E:\ that I've created and that is where
> > > I
> > > want to put this secondary datafile file, E:\. the alter database
> > > command
> > > goes like this:
> > >
> > > alter database tsqldb
> > > add file
> > > ( name = tsqldb_data2,
> > > filename = 'E:\tsqldb_data2.ndf',
> > > size = 50mb,
> > > maxsize = 150mb,
> > > filegrowth = 25%
> > > )
> > >
> > > and this is the error I get:
> > >
> > > Server: Msg 5123, Level 16, State 1, Line 1
> > > CREATE FILE encountered operating system error 5(Access is denied.)
> > > while
> > > attempting to open or create the physical file 'E:\tsqldb_data2.ndf'.
> > > Server: Msg 5009, Level 16, State 1, Line 1
> > >
> > > ALTER DATABASE failed. Some disk names listed in the statement were not
> > > found. Check that the names exist and are spelled correctly before
> > > rerunning
> > > the statement.
> > > Extending database by 50.00 MB on disk 'tsqldb_data2'.
> > >
> > > Can someone help me? Thanks.
> > >
> > > Dominick
>
Problem creating a secondary datafile on drive E:\
programming. I created a database, tsqldb, on C:\, where the ms sql server
files reside. Now, I used the alter database command to add a secondary
datafile, tsqldb_data2 to drive E:\ that I've created and that is where I
want to put this secondary datafile file, E:\. the alter database command
goes like this:
alter database tsqldb
add file
( name = tsqldb_data2,
filename = 'E:\tsqldb_data2.ndf',
size = 50mb,
maxsize = 150mb,
filegrowth = 25%
)
and this is the error I get:
Server: Msg 5123, Level 16, State 1, Line 1
CREATE FILE encountered operating system error 5(Access is denied.) while
attempting to open or create the physical file 'E:\tsqldb_data2.ndf'.
Server: Msg 5009, Level 16, State 1, Line 1
ALTER DATABASE failed. Some disk names listed in the statement were not
found. Check that the names exist and are spelled correctly before rerunning
the statement.
Extending database by 50.00 MB on disk 'tsqldb_data2'.
Can someone help me? Thanks.
DominickCheck whether the service account that runs the SQL Server instance has the
permission to create files in E:\.
Linchi
"Dominick D." wrote:
> Hi. I am a student, taking a class on MS SQL SERVER 2000 system admin and
> programming. I created a database, tsqldb, on C:\, where the ms sql server
> files reside. Now, I used the alter database command to add a secondary
> datafile, tsqldb_data2 to drive E:\ that I've created and that is where I
> want to put this secondary datafile file, E:\. the alter database command
> goes like this:
> alter database tsqldb
> add file
> ( name = tsqldb_data2,
> filename = 'E:\tsqldb_data2.ndf',
> size = 50mb,
> maxsize = 150mb,
> filegrowth = 25%
> )
> and this is the error I get:
> Server: Msg 5123, Level 16, State 1, Line 1
> CREATE FILE encountered operating system error 5(Access is denied.) while
> attempting to open or create the physical file 'E:\tsqldb_data2.ndf'.
> Server: Msg 5009, Level 16, State 1, Line 1
> ALTER DATABASE failed. Some disk names listed in the statement were not
> found. Check that the names exist and are spelled correctly before rerunni
ng
> the statement.
> Extending database by 50.00 MB on disk 'tsqldb_data2'.
> Can someone help me? Thanks.
> Dominick|||How do I check that? I"m a newbie to SQL, file permissions, so I'd appreciat
e
your assistance. Thanks.
"Linchi Shea" wrote:
[vbcol=seagreen]
> Check whether the service account that runs the SQL Server instance has th
e
> permission to create files in E:\.
> Linchi
> "Dominick D." wrote:
>|||Using Windows Explorer, right click on the drive or folder and bring up its
properties. It's under the Security tab.
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Toronto, ON Canada
.
"Dominick D." <DominickD@.discussions.microsoft.com> wrote in message
news:BE503C3C-EF2C-47DF-A91B-41C3E95959CB@.microsoft.com...
How do I check that? I"m a newbie to SQL, file permissions, so I'd
appreciate
your assistance. Thanks.
"Linchi Shea" wrote:
[vbcol=seagreen]
> Check whether the service account that runs the SQL Server instance has
> the
> permission to create files in E:\.
> Linchi
> "Dominick D." wrote:
>|||Talk to the person who administers the machine in question. They should be a
ble to check what
service account the service is using and see whether that account has permis
sions to create files in
the folder in question.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Dominick D." <DominickD@.discussions.microsoft.com> wrote in message
news:BE503C3C-EF2C-47DF-A91B-41C3E95959CB@.microsoft.com...[vbcol=seagreen]
> How do I check that? I"m a newbie to SQL, file permissions, so I'd appreci
ate
> your assistance. Thanks.
>
> "Linchi Shea" wrote:
>|||OK, I managed to do a little research on E:\ and I included the service
account that I set up to have full control over E:\ and it did the trick.
"Tom Moreau" wrote:
> Using Windows Explorer, right click on the drive or folder and bring up it
s
> properties. It's under the Security tab.
> --
> Tom
> ----
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
> SQL Server MVP
> Toronto, ON Canada
> ..
> "Dominick D." <DominickD@.discussions.microsoft.com> wrote in message
> news:BE503C3C-EF2C-47DF-A91B-41C3E95959CB@.microsoft.com...
> How do I check that? I"m a newbie to SQL, file permissions, so I'd
> appreciate
> your assistance. Thanks.
>
> "Linchi Shea" wrote:
>
>