LINQ OnConnectionOpen??

W

wdudek

Not sure if this is the best place to post this, but I didn't see any linq
specific forums. Anyway, I'm using DLINQ to insert records into a database,
which works great. However for our security before I do this I also need to
execute a stored procedure, and was wondering if there was any event, partial
method, etc that I can hook into to execute the stored proc whenever a
connection to the datbase is opened. I see the procedure partial void
InsertOrderComment, but would like a more generic proc so that in the future
when the dbml does more than just one function I do not have to write
duplicate code. I will play with this, but does anyone know if the OnCreated
method fires after the connection is established in the data context?

Thanks,

Bill
 
W

Wen Yuan Wang [MSFT]

Hello Bill,

According to your description, what you need is an event, partial method,
etc that will fire when DataConext tries to connect underlying database,
correct? If I misunderstood anything here, please don't hesitate to correct
me.

OnCreated method will fire after we create DataContext instance. This
doesn't mean DataContext connects underlying database. InsertTable partial
method is good option, but we have to write duplicate code in other update
methods (UpdateTable,DeleteTable) for each operation. In my opnion, a
generic method is to hook on DataContext.Connection.StateChange event.
StateChange event fires each you connect to underyly database.
Eg:
static void Main(string[] args)
{
DataClasses1DataContext dcdc = new DataClasses1DataContext();
dcdc.Connection.StateChange += new
System.Data.StateChangeEventHandler(Connection_StateChange);
}

static void Connection_StateChange(object sender,
System.Data.StateChangeEventArgs e)
{
if (e.CurrentState == System.Data.ConnectionState.Open)
{
//add code
}
}

Hope this helps. Please try the above method and let me know if this is
what you need. We are glad to assist you.
Have a great day,
Best regards,
Wen Yuan

Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

wdudek

I was hoping for something I could hide from the programmer a little more,
but I can stick this into a factory class that will return the data context
having already added the event handler. Thanks.

"Wen Yuan Wang [MSFT]" said:
Hello Bill,

According to your description, what you need is an event, partial method,
etc that will fire when DataConext tries to connect underlying database,
correct? If I misunderstood anything here, please don't hesitate to correct
me.

OnCreated method will fire after we create DataContext instance. This
doesn't mean DataContext connects underlying database. InsertTable partial
method is good option, but we have to write duplicate code in other update
methods (UpdateTable,DeleteTable) for each operation. In my opnion, a
generic method is to hook on DataContext.Connection.StateChange event.
StateChange event fires each you connect to underyly database.
Eg:
static void Main(string[] args)
{
DataClasses1DataContext dcdc = new DataClasses1DataContext();
dcdc.Connection.StateChange += new
System.Data.StateChangeEventHandler(Connection_StateChange);
}

static void Connection_StateChange(object sender,
System.Data.StateChangeEventArgs e)
{
if (e.CurrentState == System.Data.ConnectionState.Open)
{
//add code
}
}

Hope this helps. Please try the above method and let me know if this is
what you need. We are glad to assist you.
Have a great day,
Best regards,
Wen Yuan

Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Wen Yuan Wang [MSFT]

Hello Bill,
Thanks for your reply.

We can hook on [Connection.ChangeStatus] event in [DataContext.OnCreated()]
Partial method. Thereby, programmer won't know we executes some stored
procedures when connecting. Eg:

partial class DataClasses1DataContext
{
partial void OnCreated()
{
this.Connection.StateChange += new
System.Data.StateChangeEventHandler(Connection_StateChange);
}

void Connection_StateChange(object sender,
System.Data.StateChangeEventArgs e)
{
if (e.CurrentState == System.Data.ConnectionState.Open)
{
//add code
}
}
.....

Hope this helps. If you have any more concern, please also feel free to let
us know. We are glad to assist you.
Have a great day,
Best regards,
Wen Yuan

Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top