如何使用C#项目中现有app.config文件中的sql连接信息
我正在尝试用C#编写一个小应用程序,在这里我需要连接到SQL以获取一些数据,为此我需要SQL用户id和密码,对于同一个客户端,还有其他正在运行的应用程序连接到SQL Server并以加密的形式提供配置文件。 我在C#和.Net方面不是很好,并且很难理解这些概念,我试图在帖子中搜索答案,但没有得到任何适合我问题的答案。 我可以在我的新项目中使用已在使用的配置文件,并读取加密形式的用户信息和密码信息,并用于连接SQL server吗 如果有任何好的资料可以帮助您理解C#中配置文件的使用,我们将不胜感激,并提供好的示例。请看:如何使用C#项目中现有app.config文件中的sql连接信息,c#,encryption,configuration-files,C#,Encryption,Configuration Files,我正在尝试用C#编写一个小应用程序,在这里我需要连接到SQL以获取一些数据,为此我需要SQL用户id和密码,对于同一个客户端,还有其他正在运行的应用程序连接到SQL Server并以加密的形式提供配置文件。 我在C#和.Net方面不是很好,并且很难理解这些概念,我试图在帖子中搜索答案,但没有得到任何适合我问题的答案。 我可以在我的新项目中使用已在使用的配置文件,并读取加密形式的用户信息和密码信息,并用于连接SQL server吗 如果有任何好的资料可以帮助您理解C#中配置文件的使用,我们将不胜感
该示例适用于MSSQL,其他示例几乎相同。如果您具有对SQL server的管理访问权限,则可以创建一个可用于连接的帐户
如果你没有管理员权限,那么你必须要求管理员为你创建一个帐户。。。或者告诉您可以使用的凭据。在app.config中,在
中添加以下行:
在主程序中:
/// <summary>
/// Get values of the connection string in the following order :
/// Server , Database,
/// If the length of the list is 3 then the 3rd object is True (Integrated Security)
/// If it is 4 then the 3rd object is the 3rd object is the username and the 4th is the password.
///
/// </summary>
/// <returns></returns>
public static List<string> GetCurrentConnectionString()
{
try
{
List<string> lstConnStrData = new List<string>();
string[] aryConnStrData = new string[1];
// OPen the Config file as XML document and loop over it.
XmlDocument XmlDoc = new XmlDocument();
//Loading the Config file
XmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
foreach (XmlElement xElement in XmlDoc.DocumentElement)
{
if (xElement.Name == "connectionStrings")
{
//setting the coonection string
aryConnStrData = xElement.ChildNodes[1].Attributes[1].Value.Split(';');
break;
}
}
// Now loop over the array that holds the conn str data and split each item on "=",
// and add the second splitted item to the list, so at the end the list will contains
// the values of the connection string.
for (int i = 0; i <= aryConnStrData.Length - 2; i++)
{
lstConnStrData.Add(aryConnStrData[i].Split('=')[1]);
}
return lstConnStrData;
}
catch (Exception)
{
throw;
}
}
//
///按以下顺序获取连接字符串的值:
///服务器、数据库、,
///如果列表长度为3,则第三个对象为真(集成安全)
///如果是4,则第三个对象是用户名,第四个对象是密码。
///
///
///
公共静态列表GetCurrentConnectionString()
{
尝试
{
List lstConnStrData=新列表();
字符串[]aryConnStrData=新字符串[1];
//以XML文档的形式打开配置文件并在其上循环。
XmlDocument XmlDoc=新的XmlDocument();
//加载配置文件
Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
foreach(XmlDoc.DocumentElement中的xmlementXelement)
{
if(xElement.Name==“ConnectionString”)
{
//设置连接字符串
aryConnStrData=xElement.ChildNodes[1]。属性[1]。值。拆分(“;”);
打破
}
}
//现在在保存conn str数据的数组上循环,并在“=”上拆分每个项,
//并将第二个拆分的项添加到列表中,以便在列表末尾包含
//连接字符串的值。
对于(int i=0;感谢您的回复,但实际上我想重用具有加密用户id和密码的配置文件,从其他应用程序的配置文件中读取这些用户id和密码,并在我的代码中用于连接SQL server,我在SQL连接中没有硬编码值的问题,它工作正常。只需加入这些信息即可d将其设置为连接字符串:例如:conn.connectionstring=“数据源=“+ipAddress+”;初始目录=“+databaseName+”;用户id=“+decryptedUser+”;密码=“+decryptedPassword+”;”;请看我的回答是否对您有帮助!?是的,这可能是一个选项,让我与客户联系。通常他们更喜欢配置文件。我不能直接从其他应用程序的配置文件中读取加密密码,而不是将加密密码放在配置文件中,例如,EMailSetting.config是一个配置文件,它具有与电子邮件相关的设置,我的应用程序也是se正在发送电子邮件,并将使用与EMailSetting.config中相同的信息发送电子邮件。
/// <summary>
/// Get values of the connection string in the following order :
/// Server , Database,
/// If the length of the list is 3 then the 3rd object is True (Integrated Security)
/// If it is 4 then the 3rd object is the 3rd object is the username and the 4th is the password.
///
/// </summary>
/// <returns></returns>
public static List<string> GetCurrentConnectionString()
{
try
{
List<string> lstConnStrData = new List<string>();
string[] aryConnStrData = new string[1];
// OPen the Config file as XML document and loop over it.
XmlDocument XmlDoc = new XmlDocument();
//Loading the Config file
XmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
foreach (XmlElement xElement in XmlDoc.DocumentElement)
{
if (xElement.Name == "connectionStrings")
{
//setting the coonection string
aryConnStrData = xElement.ChildNodes[1].Attributes[1].Value.Split(';');
break;
}
}
// Now loop over the array that holds the conn str data and split each item on "=",
// and add the second splitted item to the list, so at the end the list will contains
// the values of the connection string.
for (int i = 0; i <= aryConnStrData.Length - 2; i++)
{
lstConnStrData.Add(aryConnStrData[i].Split('=')[1]);
}
return lstConnStrData;
}
catch (Exception)
{
throw;
}
}
public static List<string> SplitConnectionString(string ConnectionString)
{
try
{
List<string> lstConnStrData = new List<string>();
string[] aryConnStrData = new string[1];
aryConnStrData = ConnectionString.Split(';');
// Now loop over the array that holds the conn str data and split each item on "=",
// and add the second splitted item to the list, so at the end the list will contains
// the values of the connection string.
for (int i = 0; i <= aryConnStrData.Length - 2; i++)
{
lstConnStrData.Add(aryConnStrData[i].Split('=')[1]);
}
return lstConnStrData;
}
catch (Exception)
{
throw;
}
}