✅ Issues with setting up different classes and sql connection
Could someone help me understand how I can setup this SQL connection? it's my first time organizing my code into different folders and really structuring my app with different folders, etc.
I get the error "A field initializer cannot reference the non-static field, method or property 'Customer.connection' ".
I want to make it so that I have a connection class that handles the connection. Currently I have: model class that talks to the dal class that talks to the connection class
I have the following class:
My connection class looks like this
19 Replies
the problem is
you can't access
connection
in a field initializer because it's not guaranteed to be initialized at that point. you can assign db
in the constructor body instead
it looks like the real issue is that the whole Connection
class should be static - it doesn't contain any state and it doesn't make sense to ever have an instance of itwould i make the entire class static or just the connectionstring ?
the whole class, then you don't need a
Connection connection;
field
you can simply call Connection.CreateConnection()
i'm also suspicious why your model needs to contain a database connection, but one thing at a timeCurrently I get alot of errors in my customerDAO class
here's the code
when you make a class static you have to make all its members static too
and you can no longer have variables of that type because there is nothing to store
Wait so I would remove the connectionString then
like this
no, your first one was fine
what errors do you have left?
Jimmacle
and you can no longer have variables of that type because there is nothing to store
Quoted by
<@901546879530172498> from #Issues with setting up different classes and sql connection (click here)
React with ❌ to remove this embed.
How would i use the connection now ?
by calling it directly on the type name
currently doing it like this
using (SqlConnection sqlConnection = connection.CreateConnection())
Connection.CreateConnection()
you don't need any connection
variables anymoreAh okay, it works now, thanks
Wondering though and correct me if im wrong but isnt making it static unsafe because im storing my sql connection string in there ? any object could access the sql connection or am i wrong ?
no, because it's
private
frankly storing your connection string anywhere in your source code is unsafe, normally you load it from somewhere else like a config fileThis is for a school project so I’m not really worried about safety that much just wondering
Usually i would store it somewhere in .env right ?
environment variables are one option
back to this,
static
on a class just controls whether a class can have instances of itself or not - it doesn't affect accessibility of its members
so it's no less safe than it already was
the main thing is that it's bad to use static classes for anything that stores information that can change over your program's execution, it tends to make things messy and harder to debugWas this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.