Click here to Skip to main content
15,886,919 members
Home / Discussions / C#
   

C#

 
Questionc# Pin
Impana D patel24-Jul-23 0:54
Impana D patel24-Jul-23 0:54 
AnswerRe: c# Pin
Richard Deeming24-Jul-23 1:08
mveRichard Deeming24-Jul-23 1:08 
AnswerRe: c# Pin
Richard MacCutchan24-Jul-23 1:33
mveRichard MacCutchan24-Jul-23 1:33 
AnswerRe: c# Pin
OriginalGriff24-Jul-23 1:55
mveOriginalGriff24-Jul-23 1:55 
Questionc# Pin
Impana D patel23-Jul-23 23:46
Impana D patel23-Jul-23 23:46 
AnswerRe: c# Pin
Richard MacCutchan24-Jul-23 0:03
mveRichard MacCutchan24-Jul-23 0:03 
QuestionWhat should be the principles of multilayer software design? Pin
farshad valizade 202323-Jul-23 22:00
farshad valizade 202323-Jul-23 22:00 
AnswerRe: What should be the principles of multilayer software design? Pin
Richard Deeming23-Jul-23 23:11
mveRichard Deeming23-Jul-23 23:11 
There are quite a few problems with the code you've shown.

Firstly, your code won't compile. You can't have line-breaks in a standard string. You would need to use either a verbatim string[^] or a raw string[^] for your query.

You should make the query a local const so that you're not tempted to try to inject parameter values into it incorrectly and introduce a SQL Injection[^] vulnerability into your code.

You seem to be using a shared database connection instance. That's a terrible idea - either your code must be restricted to only service one request at a time, or you'll end up with cross-contamination when multiple users try to access your application, since the connection is not thread-safe. Instead, create the connection when you need it, and wrap it in a using block to ensure it's disposed of properly.

Similarly, the SqlCommand and SqlDataReader instances need to be wrapped in using blocks.

Your code currently swallows any exceptions, and returns an empty DataTable instead. The caller is expected to examine the Error property to determine whether an error occurred, and retrieve a tiny portion of the details of that error - assuming the property hasn't been overwritten by a call from a different user in the meantime. Instead, you should let the exception propagate to the caller naturally. If you need to add more details, then throw a different exception, making sure to include the original exception as the InnerException.
C#
public static DataTable GetAll()
{
    const string Query = """
        SELECT
            ...
        FROM
            ...
        """;

    try
    {
        using (var connection = Connection.CreateAndOpenConnection()) // Make this method open the connection before returning it...
        using (var command = new SqlCommand(connection, Query))
        using (var reader = command.ExecuteReader())
        {
            DataTable dt = new DataTable();
            dt.Load(reader);
            return dt;
        }
    }
    catch (SqlException ex)
    {
        throw new YourCustomException("There was an error retrieving the list of all things. See the inner exception for details.", ex);
    }
}

Once you've dealt with those issues, then you can start working out what you need to put in the other layers. For example, having a BLL which simply passes all calls through to the DAL and returns the results unchanged doesn't make a lot of sense.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

QuestionMulti-threading Pin
MAW3017-Jul-23 9:52
MAW3017-Jul-23 9:52 
AnswerRe: Multi-threading Pin
Gerry Schmitz17-Jul-23 15:32
mveGerry Schmitz17-Jul-23 15:32 
AnswerRe: Multi-threading Pin
Dave Kreskowiak17-Jul-23 15:53
mveDave Kreskowiak17-Jul-23 15:53 
QuestionParameterized Query Pin
Richard Andrew x6416-Jul-23 8:47
professionalRichard Andrew x6416-Jul-23 8:47 
AnswerRe: Parameterized Query Pin
Dave Kreskowiak16-Jul-23 10:44
mveDave Kreskowiak16-Jul-23 10:44 
GeneralRe: Parameterized Query Pin
Richard Andrew x6416-Jul-23 11:39
professionalRichard Andrew x6416-Jul-23 11:39 
AnswerRe: Parameterized Query Pin
OriginalGriff16-Jul-23 11:24
mveOriginalGriff16-Jul-23 11:24 
GeneralRe: Parameterized Query Pin
Richard Andrew x6416-Jul-23 11:42
professionalRichard Andrew x6416-Jul-23 11:42 
GeneralRe: Parameterized Query Pin
OriginalGriff16-Jul-23 18:34
mveOriginalGriff16-Jul-23 18:34 
GeneralRe: Parameterized Query Pin
Richard Andrew x6417-Jul-23 5:35
professionalRichard Andrew x6417-Jul-23 5:35 
AnswerRe: Parameterized Query Pin
Gerry Schmitz16-Jul-23 19:32
mveGerry Schmitz16-Jul-23 19:32 
GeneralRe: Parameterized Query Pin
Richard Andrew x6417-Jul-23 5:33
professionalRichard Andrew x6417-Jul-23 5:33 
QuestionGetting a VST3 effect name and vendor Pin
spice3d8-Jul-23 9:19
spice3d8-Jul-23 9:19 
AnswerRe: Getting a VST3 effect name and vendor Pin
Pete O'Hanlon8-Jul-23 10:09
mvePete O'Hanlon8-Jul-23 10:09 
GeneralRe: Getting a VST3 effect name and vendor Pin
spice3d8-Jul-23 11:18
spice3d8-Jul-23 11:18 
QuestionAdvices for my learning path Pin
coco2437-Jul-23 21:10
coco2437-Jul-23 21:10 
AnswerRe: Advices for my learning path Pin
OriginalGriff7-Jul-23 21:19
mveOriginalGriff7-Jul-23 21:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.