如何从文本文件完成aspx连接字符串
How to complete aspx connection string from text file
我必须使用继承服务器和数据库名称的文本文件 "db.txt" 来使我的连接字符串完整。
db.txt 看起来像这样:
<Anfang>
SERVER==dbServer\SQLEXPRESS
DATABASE==studentweb
<Ende>
连接字符串:
string constr = ConfigurationManager.ConnectionStrings["DRIVER={SQL Server}; SERVER=SERVER DATABASE=DB UID=;PWD=;LANGUAGE=Deutsch;Trusted_Connection=YES"].ConnectionString;
不幸的是,我们只允许使用经典 ASPX.net (C# 2.0) 而不是 web.config。
我搜索了很多,但没有找到任何可以帮助我的东西。
有人知道如何让它发挥作用吗?
这里有一些东西可以让你继续前进。
简单来说,我把DBInfo文件通过一种逐行读取文件的方法。当我看到 <anfang>
行时,我知道下一行很重要,当我看到 <ende>
行时,我知道它结束了,所以我需要抓住中间的所有内容。因此,为什么我想出了布尔值 areWeThereYet
和 isItDoneYet
,我用它们来开始和停止从文件中收集数据。
在此代码段中,我使用 Dictionary<string, string>
来存储值并 return 值,但是,您可以使用不同的东西。起初我打算创建一个自定义 class 来保存所有数据库信息,但由于这是一项学校作业,我们将逐步进行并从使用已经可用的信息开始。
using System;
using System.Collections.Generic;
namespace _41167195
{
class Program
{
static void Main(string[] args)
{
string pathToDBINfoFile = @"M:\WhosebugQuestionsAndAnswers167195167195\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;
}
}
}
我必须使用继承服务器和数据库名称的文本文件 "db.txt" 来使我的连接字符串完整。
db.txt 看起来像这样:
<Anfang>
SERVER==dbServer\SQLEXPRESS
DATABASE==studentweb
<Ende>
连接字符串:
string constr = ConfigurationManager.ConnectionStrings["DRIVER={SQL Server}; SERVER=SERVER DATABASE=DB UID=;PWD=;LANGUAGE=Deutsch;Trusted_Connection=YES"].ConnectionString;
不幸的是,我们只允许使用经典 ASPX.net (C# 2.0) 而不是 web.config。 我搜索了很多,但没有找到任何可以帮助我的东西。 有人知道如何让它发挥作用吗?
这里有一些东西可以让你继续前进。
简单来说,我把DBInfo文件通过一种逐行读取文件的方法。当我看到 <anfang>
行时,我知道下一行很重要,当我看到 <ende>
行时,我知道它结束了,所以我需要抓住中间的所有内容。因此,为什么我想出了布尔值 areWeThereYet
和 isItDoneYet
,我用它们来开始和停止从文件中收集数据。
在此代码段中,我使用 Dictionary<string, string>
来存储值并 return 值,但是,您可以使用不同的东西。起初我打算创建一个自定义 class 来保存所有数据库信息,但由于这是一项学校作业,我们将逐步进行并从使用已经可用的信息开始。
using System;
using System.Collections.Generic;
namespace _41167195
{
class Program
{
static void Main(string[] args)
{
string pathToDBINfoFile = @"M:\WhosebugQuestionsAndAnswers167195167195\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;
}
}
}