Executing SQLCommand from within UDT code
hello
i need obtain current @@language within clr code of user defined type. when try execute sqlcommand "select @@language" on connection current context ("context connection=true") error "system.invalidoperationexception: data access not allowed in context. either context function or method not marked dataaccesskind.read or systemdataaccesskind.read, callback obtain data fillrow method of table valued function, or udt validation method."
is there way current sql connection langugae inside clr code of udt?
robert
hi robert,
as error message indicated, please set dataaccesskind.read enable read-only data access user-defined functions.
please see:
[microsoft.sqlserver.server.sqlfunction(dataaccess = dataaccesskind.read)] public static sqlstring getlanguage() { using (sqlconnection connection = new sqlconnection("context connection=true")) { connection.open(); sqlcommand cmd = new sqlcommand("select @@language", connection); string strlanguage = (string)cmd.executescalar(); return new sqlstring(strlanguage); } }
reference:
data access clr database objects
http://msdn.microsoft.com/en-us/library/ms131109.aspx
please remember mark replies answers if , unmark them if provide no help.
welcome all-in-one code framework! if have feedback, please tell us.
SQL Server > .NET Framework inside SQL Server
Comments
Post a Comment