程序修改数据库连接字符串

zj_312 2010-05-21 01:42:10
用户第一次发布、或数据库信息改了后能可进入配置页面设置连接数据库的用户、密码、连接串名,同时对其加密
...全文
155 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zj_312 2010-05-21
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 foxdave 的回复:]

答案
LZ结贴~
[/Quote]

终于找到了,谢谢!!!
Justin-Liu 2010-05-21
  • 打赏
  • 举报
回复
答案
LZ结贴~
Justin-Liu 2010-05-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 deknight 的回复:]
呵呵,ls的你是转法师啊还战士啊...
[/Quote]
转隐士 哈哈
zj_312 2010-05-21
  • 打赏
  • 举报
回复

字符串就放在web.config文件中

<connectionStrings>
<add name="test" connectionString="test" />
</connectionStrings>

lester19872007 2010-05-21
  • 打赏
  • 举报
回复
这个很好改啊,就如月月说的,要看你的连接字符串放在哪里了,这样才好对症下药。
deknight 2010-05-21
  • 打赏
  • 举报
回复
呵呵,ls的你是转法师啊还战士啊...
Justin-Liu 2010-05-21
  • 打赏
  • 举报
回复
关键是看你数据库连接串也在哪了 才能对症下药

LZ快结贴 我要转生了
lanfengsese 2010-05-21
  • 打赏
  • 举报
回复
        public bool IsConnecd
{
get { return isConnecd; }
set { isConnecd = value; }
}
private bool isConnecd = false;
private void btn_Config_Click(object sender, EventArgs e)
{
DataConnectionDialog dlg = new DataConnectionDialog();
dlg.DataSources.Add(DataSource.SqlDataSource);
dlg.SelectedDataSource = DataSource.SqlDataSource;
dlg.SelectedDataProvider = DataProvider.SqlDataProvider;
//dlg.ConnectionString = ZooSystemUpdate.Properties.Settings.Default.ZooDataConnectString;
dlg.ConnectionString = ConfigurationManager.AppSettings["connectstring"].ToString().Trim();
if (DataConnectionDialog.Show(dlg, this) == DialogResult.OK)
{
string abc = ConfigurationManager.AppSettings["connectstring"].ToString().Trim();
//判断是否能连接上数据库
ConfigurationManager.AppSettings["connectstring"] = dlg.ConnectionString;
Manage.DataConnect da = new Manage.DataConnect();
if (da.IsConnect())
{

string miwen = DESEncrypt.Encrypt(dlg.ConnectionString);
SetKeyValue("connectstring",miwen);
IsConnecd = true;
this.Close();
}
else
{
ConfigurationManager.AppSettings["connectstring"] =abc;
}
}
}
//// <summary>
/// 设置连接字符串
/// </summary>
/// <param name="AppKey"></param>
/// <param name="AppValue"></param>
private void SetKeyValue(string AppKey, string AppValue)
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");

XmlNode xNode;
XmlElement xElem1;
XmlElement xElem2;

xNode = xDoc.SelectSingleNode("//appSettings");

xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
if (xElem1 != null) xElem1.SetAttribute("value", AppValue);
else
{
xElem2 = xDoc.CreateElement("add");
xElem2.SetAttribute("key", AppKey);
xElem2.SetAttribute("value", AppValue);
xNode.AppendChild(xElem2);
}
xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
}
private void btn_Exit_Click(object sender, EventArgs e)
{
this.Close();
}
}

上面是把数据库配置修改到config文件的方法,DESEncrypt是DES加密解密类,网上很多源码的
zj_312 2010-05-21
  • 打赏
  • 举报
回复
高手在哪里啊,别沉了.......

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧