Exception handling in windows Application of C# language from Sql Server 2005 - c#

I want List of all types exceptions which may be generated during remotely connection between C# application and sql server2005 like updating, inserting etc..

When connected to a Sql Server, whenever an exception is thrown, it will be a SqlException.
From documentation:
This class is created whenever the .NET Framework Data Provider for
SQL Server encounters an error generated from the server.

In the SQL Server instance, a list of all defined error codes is available in the sysmessages table.
SQLException is always thrown when the error originates from the server. The SQLException object will identify which of those errors occurred in its Number property.

Most exceptions that move up from the db layer to the application layer deal with the sqlexception class. In this case, these have to do with the following:
time out exceptions
incorrect credentials (security)
incorrect server name / ip address
SQL Server does not exist (etc)
To handle exceptions that are caused by inserts or updates involves exception handling within your actual procedure (sproc). You could also handle these by custom exceptions that you create.

Related

How can I identify all connection related exception types in an SqlException?

I am writing a VB.net winforms application which connects to Sql server 2008. (C# answers also accepted)
I am trying to differentiate between any SqlExceptions related to the connection to the database and other SQL specific errors (such as incorrect syntax etc.)
I am using a try/catch around the connection like so:
Try
//code to open the connection/access the DB/etc.
Catch ex As SqlException When ex.Number=???
//Do stuff
End Try
I am presuming that checking the error code is the correct way to go. My issue is that there are a number of different error codes (i'm not sure how many) relating to the connection to the database. For example:
121 = Timeout (could be caused by disconnecting from the network).
53 = Could not connect (again, could be caused by not being connected to the network).
1326 = You are connected to the physical server machine but the Sql Server is not running.
And I am sure that there are many, many more. Do I have to check for each of these individually or is there a better way? If I have to check for them individually, is there a list of the connection related ones anywhere that I can refer to?
As #jdweng Suggested in comments, I added a try/catch block around just the query. So that if an exception occurred, it couldn't be anything else but connection related.
So simple! Thanks again.

U-SQL data source as SQL Server inside a UDF

I need to extract rows from a SQL table where some columns are encrypted using SQL Server's new 'Always Encrypted' feature. I see that I cannot use the 'AZURESQLDB' DataSource feature and there needs to be decryption done before reading the data in plain text. Are there plans to add this capability?. Meanwhile, I tried to write a user defined function that will do the same operation(connect, decrypt data and return object) in a registered assembly but when it runs, I get the following error:
Inner exception from user expression: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
I have checked the code and everything seems correct. The connection string is used by the SqlConnection object and works fine in all other applications. I am guessing that the connectivity to external data sources from within a UDF is blocked. Is there any way around this?
Are you using the DATA SOURCE in U-SQL for representing your SQL Server instance and you cannot get it to read encrypted data? If so, please file a feature request at http://aka.ms/adlfeedback.
You cannot call out to network resources directly from within U-SQL user code for the reasons explained here.
One way around this might be to create a stored procedure which does the hard work, the decryption then renders the data. Then use Azure Data Factory with a Stored Proc Task to access the decrypted data and move what you need to the Data Lake - not including the secure data. From there you could then access it using a U-SQL script. One idea? Let me know if you need me to work up more of an example.

How to avoid distributed transaction?

There is a C# program calling a stored proc which has insert linkedserver... from ..... The C# program using Entity framework (ctx.Database.ExecuteSqlCommand("....")).
I've tried
Change the property "Enable Promotion of Distributed Transaction" to false.
Add "Enlist=false" in the connection string of the C# program
Change the MSDTC of the SQL server to allow anonymous authentication...
But it still gets the following error.
A severe error occurred on the current command. The results, if any, should be discarded.
OLE DB provider "SQLNCLI11" for linked server "...." returned message "No transaction is active.".

Oracle Managed ODP.NET: Connection is forcibly closed due to network error

I use Oracle ODP.NET connector to connect to Oracle database server.
The problem is a connection sometimes is lost due to several different problems (like network failure or server forcibly closes a connetion by peer).
This causes an uncaught exception because I cannot catch exceptions while a connection object is not used in a query or during an idle time of an application.
The question is how can I catch Oracle connection exceptions when a connection is lost?
Is there any callback technique or something that can inform me about disconnection?
See my comment above about Validate Connection = true in the connection pool connect string.
You can also write your own "connection tester" routine that does a "Select sysdate from dual" against the database to test the connection.
Both of these solutions cost a roundtrip to the database. Neither of these solutions completely prevent network errors as they can happen right after you test it.
Bottom line is you need to be catching exceptions anytime you use an ODP.NET object if you don't want an unhandled exception.
There's no callback mechanism available that will prevent this.
I don't really understand how you seem to be saying you are getting exceptions when you are not using the odp.net objects. If you are getting an exception, you must be using the odp.net object at that moment and therefore you can catch the exception in that code.

Servicestack OrmLite - Execute as User/Impersonation

I am using Servicestack OrmLite as a data layer for my application (.NET C# 3.5/SQL Server).
One of the design requirements (It isn't greenfield, so it is mandatory) is to have commands executed as a particular user which has a schema attached on the SQL server side.
After creating the DbContext with OpenDbConnection() I send an Execute as User command to SQL server so that they are executing with the correct login, and they are switched over to the correct schema for that login.
The error I am getting back for selects against that connection later in the process is:
A severe error occurred on the current command. The results, if any, should be discarded.
Thoughts:
Connection pooling is losing the current user command sent to SQL server?
Is there a built in User/schema handler extension to OrmLite that I haven't seen?
RegisterConnection?
Thanks for your input.

Categories

Resources