0

I must use a text file "db.txt" which inherits the names of the Server and Database to make my connection string complete.

db.txt looks like this:

<Anfang>
SERVER==dbServer\SQLEXPRESS
DATABASE==studentweb
<Ende>

The connection string:

string constr = ConfigurationManager.ConnectionStrings["DRIVER={SQL Server}; SERVER=SERVER DATABASE=DB UID=;PWD=;LANGUAGE=Deutsch;Trusted_Connection=YES"].ConnectionString;

Unfortunatly we are only allowed to use Classic ASPX.net (C# 2.0) and not the web.config. I've searched a lot, but found nothing close to help me. Somebody got an Idea how to make it work?

5
  • 3
    The web.config was available in dotnet 2.0. What kind of strange restriction will not allow you to use a web.config. That is the right way to do this. Anything else seems like a bizarre contrived project from a misguided professor. Commented Dec 15, 2016 at 15:02
  • So you need to read the text file using something like System.IO.StreamReader, extract the DB info and use it accordingly. Is there anything else in the text file, or is what you posted the whole content of the DB reference file? Could there be more than 1 db listed in your file? Commented Dec 15, 2016 at 15:15
  • @SeanLange I'm aware that this is not conventional. If it was, the solution would be much easier. But our teacher wants it this way. Commented Dec 15, 2016 at 15:16
  • @blaze_125 no just one Server one DB (for now??) Commented Dec 15, 2016 at 15:18
  • Read the comment from @blaze_125. You have to read the contents of that file. I fell bad for you as it seems clear your teacher is coming up with projects with really strange limitations. I am sure the idea is to teach you how to read a file but doing for the connection string is just a horrible approach and is likely to teach some people bad habits about connection strings. Commented Dec 15, 2016 at 15:24

1 Answer 1

0

Here is something to get you going. In a nutshell, I put the DBInfo file through a method that reads the file line by line. When I see the line <anfang> I know the next line will be important, and when I see the line <ende> I know it's the end, so I need to grab everything in between. Hence why I came up with the booleans areWeThereYet and isItDoneYet which I use to start and stop gathering data from the file.

In this snippet I use a Dictionary<string, string> to store and return the values but, you could use something different. At first I was going to create a custom class that would hold all the DB information but, since this is a school assignment, we'll go step by step and start by using what's already available.

    using System;
using System.Collections.Generic;

namespace _41167195
{
    class Program
    {
        static void Main(string[] args)
        {
            string pathToDBINfoFile = @"M:\StackOverflowQuestionsAndAnswers\41167195\41167195\sample\DBInfo.txt";//the path to the file holding the info
            Dictionary<string, string> connStringValues = DoIt(pathToDBINfoFile);//Get the values from the file using a method that returns a dictionary
            string serverValue = connStringValues["SERVER"];//just for you to see what the results are
            string dbValue = connStringValues["DATABASE"];//just for you to see what the results are

            //Now you can adjust the line below using the stuff you got from above.
            //string constr = ConfigurationManager.ConnectionStrings["DRIVER={SQL Server}; SERVER=SERVER DATABASE=DB UID=;PWD=;LANGUAGE=Deutsch;Trusted_Connection=YES"].ConnectionString;
        }


        private static Dictionary<string, string> DoIt(string incomingDBInfoPath)
        {
            Dictionary<string, string> retVal = new Dictionary<string, string>();//initialize a dictionary, this will be our return value

            using (System.IO.StreamReader sr = new System.IO.StreamReader(incomingDBInfoPath))
            {
                string currentLine = string.Empty;
                bool areWeThereYet = false;
                bool isItDoneYet = false;
                while ((currentLine = sr.ReadLine()) != null)//while there is something to read
                {
                    if (currentLine.ToLower() == "<anfang>")
                    {
                        areWeThereYet = true;
                        continue;//force the while to go into the next iteration
                    }
                    else if (currentLine.ToLower() == "<ende>")
                    {
                        isItDoneYet = true;
                    }

                    if (areWeThereYet && !isItDoneYet)
                    {
                        string[] bleh = currentLine.Split(new string[] { "==" }, StringSplitOptions.RemoveEmptyEntries);
                        retVal.Add(bleh[0], bleh[1]);//add the value to the dictionary
                    }
                    else if (isItDoneYet)
                    {
                        break;//we are done, get out of here
                    }
                    else
                    {
                        continue;//we don't need this line
                    }
                }
            }

            return retVal;
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

WOW :-O thank you a thousand times. This ist much more than i expected.
Anytime. This is a school assignment, so I suggest you re-write my snippet instead of copy/paste. I think you will remember the "concept" better if you re-write than if you were to copy/paste it.
All the other Parts of the Programm run like a charm with your code. You saved my week. Was hoping for some small code snippets. But this is a really big help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.