Click here to Skip to main content
14,661,273 members
Home » Discussions » C#
   

C#

 
SuggestionRe: How to convert C++ 2005 project to 2016 Pin
Richard Deeming23-Jul-20 6:49
mveRichard Deeming23-Jul-20 6:49 
Questionclass to open its database Pin
ago248621-Jul-20 0:17
Memberago248621-Jul-20 0:17 
GeneralRe: class to open its database Pin
Richard MacCutchan21-Jul-20 0:46
mveRichard MacCutchan21-Jul-20 0:46 
GeneralRe: class to open its database Pin
ago248621-Jul-20 0:55
Memberago248621-Jul-20 0:55 
GeneralRe: class to open its database Pin
Richard MacCutchan21-Jul-20 1:13
mveRichard MacCutchan21-Jul-20 1:13 
GeneralRe: class to open its database Pin
ago248621-Jul-20 1:22
Memberago248621-Jul-20 1:22 
GeneralRe: class to open its database Pin
ago248621-Jul-20 1:32
Memberago248621-Jul-20 1:32 
AnswerRe: class to open its database Pin
OriginalGriff21-Jul-20 1:19
mveOriginalGriff21-Jul-20 1:19 
First off, don't hard code connection strings - they need to be in a config file or similar, if only so you don't release the password in clear text with your app...

Second, never store databases in your app directory: although it works in development, it will fail in production as the application folder rarely has write permissions anymore for anti-virus reasons. See here: Where should I store my data?[^] for some better ideas.

Thirdly, you are much, much better off creating your Connection object in your code when you need it inside a using block, as they are scarce resources and should be Disposed when you are finished with them:
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT Age, Description FROM myTable WHERE ID = @ID", con))
        {
        cmd.Parameters.AddWithValue("@ID", myTextBox.Text);
        using (SqlDataReader reader = cmd.ExecuteReader())
            {
            while (reader.Read())
                {
                int age = (int) reader["Age"];
                string desc = (string) reader["Description"];
                Console.WriteLine($"{age}\n{desc}");
                }
            }
        }
    }
That way, whatever happens to your code, the objects will be closed and disposed for you automatically.

Fourthly, although you are trying to separate the DB from the form (which is a good idea) don't use MessageBox to report problems with DB related stuff - it makes too many assumptions about how your code will be used. Error handling and reporting is the province of the Presentation Layer, not the Data Layer: there is no guarantee that the user will be able to see a MessageBox! Let the code that calls the DB stuff handle errors - it can report or not, but it may log instead, or just terminate what it is doing. Your way, the user gets the MessageBox, then your app crashes because the connection isn't open anyway.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!

GeneralRe: class to open its database Pin
ago248621-Jul-20 1:40
Memberago248621-Jul-20 1:40 
GeneralRe: class to open its database Pin
ago248621-Jul-20 5:59
Memberago248621-Jul-20 5:59 
QuestionCreating my own enhanced dos language Pin
Member 1270775219-Jul-20 5:30
MemberMember 1270775219-Jul-20 5:30 
AnswerRe: Creating my own enhanced dos language Pin
Dave Kreskowiak19-Jul-20 6:09
mveDave Kreskowiak19-Jul-20 6:09 
AnswerRe: Creating my own enhanced dos language Pin
Richard MacCutchan19-Jul-20 6:42
mveRichard MacCutchan19-Jul-20 6:42 
AnswerRe: Creating my own enhanced dos language Pin
Gerry Schmitz19-Jul-20 7:39
mveGerry Schmitz19-Jul-20 7:39 
AnswerRe: Creating my own enhanced dos language Pin
OriginalGriff19-Jul-20 8:20
mveOriginalGriff19-Jul-20 8:20 
AnswerRe: Creating my own enhanced dos language Pin
Member 1270775219-Jul-20 9:26
MemberMember 1270775219-Jul-20 9:26 
GeneralRe: Creating my own enhanced dos language Pin
Dave Kreskowiak19-Jul-20 10:14
mveDave Kreskowiak19-Jul-20 10:14 
GeneralRe: Creating my own enhanced dos language Pin
Member 1270775220-Jul-20 9:19
MemberMember 1270775220-Jul-20 9:19 
GeneralRe: Creating my own enhanced dos language Pin
Dave Kreskowiak20-Jul-20 12:22
mveDave Kreskowiak20-Jul-20 12:22 
GeneralRe: Creating my own enhanced dos language Pin
Member 1270775221-Jul-20 2:15
MemberMember 1270775221-Jul-20 2:15 
GeneralRe: Creating my own enhanced dos language Pin
Richard MacCutchan21-Jul-20 2:55
mveRichard MacCutchan21-Jul-20 2:55 
GeneralRe: Creating my own enhanced dos language Pin
Dave Kreskowiak21-Jul-20 4:51
mveDave Kreskowiak21-Jul-20 4:51 
GeneralRe: Creating my own enhanced dos language Pin
Member 1270775221-Jul-20 8:22
MemberMember 1270775221-Jul-20 8:22 
GeneralRe: Creating my own enhanced dos language Pin
Dave Kreskowiak21-Jul-20 8:58
mveDave Kreskowiak21-Jul-20 8:58 
GeneralRe: Creating my own enhanced dos language Pin
Richard MacCutchan21-Jul-20 10:25
mveRichard MacCutchan21-Jul-20 10:25 

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.