asp.net(c#)中动态更改web.config连接数据库参数 50
<connectionStrings><addname="connstring"connectionString="server=192.111.1.1;database...
<connectionStrings>
<add name="connstring" connectionString="server=192.111.1.1;database=data;uid=sa;pwd=sa;Max Pool Size=512;" />
</connectionStrings>
就是像上面那个连接的ip 数据库名 用户 和密码 能在前台web页面中修改
在此先谢谢各位了!
再等!!!!!!! 展开
<add name="connstring" connectionString="server=192.111.1.1;database=data;uid=sa;pwd=sa;Max Pool Size=512;" />
</connectionStrings>
就是像上面那个连接的ip 数据库名 用户 和密码 能在前台web页面中修改
在此先谢谢各位了!
再等!!!!!!! 展开
7个回答
展开全部
修改web.config的方法2007-05-07 14:23最早学.net代码时,修改web.config,直接绑定到table里去就好了。。
代码如下:
读
Dim ds As DataSet
ds = New DataSet()
ds.ReadXml(Server.MapPath("web.config"))
setname.Text = ds.Tables(1).Rows(0)(1)
addr.Text = ds.Tables(1).Rows(1)(1)
wangzhi.Text = ds.Tables(1).Rows(2)(1)
tel.Text = ds.Tables(1).Rows(3)(1)
email.Text = ds.Tables(1).Rows(4)(1)
ds.Clear()
ds.Dispose()
改
Dim ds As DataSet
ds = New DataSet()
ds.ReadXml(Server.MapPath("web.config"))
ds.Tables(1).Rows(0)(1) = setname.Text
ds.Tables(1).Rows(1)(1) = addr.Text
ds.Tables(1).Rows(2)(1) = wangzhi.Text
ds.Tables(1).Rows(3)(1) = tel.Text
ds.Tables(1).Rows(4)(1) = email.Text
ds.AcceptChanges()
ds.WriteXml(Server.MapPath("web.config"))
ds.Clear()
ds.Dispose()
msg.Text = "修改成功!"
最近做项目,用上了静态刷新,研究了下ajax,阿哦,要在web.config里加上一句话
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
</httpHandlers>
用上面的读取就完蛋了。。
怎么办?来个最恶心的做法吧。。。
读:
etname.Text = ConfigurationSettings.AppSettings("website")
addr.Text = ConfigurationSettings.AppSettings("addr")
wangzhi.Text = ConfigurationSettings.AppSettings("url")
tel.Text = ConfigurationSettings.AppSettings("tel")
email.Text = ConfigurationSettings.AppSettings("email")
Dim zt As Integer = ConfigurationSettings.AppSettings("denglu_rizhi")
改:
Dim XPath As String = "/configuration/appSettings/add[@key='?']"
Dim domWebConfig As New XmlDocument
domWebConfig.Load(HttpContext.Current.Server.MapPath("web.config"))
Dim websites As XmlNode = domWebConfig.SelectSingleNode(XPath.Replace("?", "website"))
websites.Attributes("value").InnerText = setname.Text
Dim addrs As XmlNode = domWebConfig.SelectSingleNode(XPath.Replace("?", "addr"))
addrs.Attributes("value").InnerText = addr.Text
Dim wangzhis As XmlNode = domWebConfig.SelectSingleNode(XPath.Replace("?", "url"))
wangzhis.Attributes("value").InnerText = wangzhi.Text
Dim tels As XmlNode = domWebConfig.SelectSingleNode(XPath.Replace("?", "tel"))
tels.Attributes("value").InnerText = tel.Text
Dim emails As XmlNode = domWebConfig.SelectSingleNode(XPath.Replace("?", "email"))
emails.Attributes("value").InnerText = email.Text
domWebConfig.Save(HttpContext.Current.Server.MapPath("web.config"))
msg.Text = "修改成功!"
代码如下:
读
Dim ds As DataSet
ds = New DataSet()
ds.ReadXml(Server.MapPath("web.config"))
setname.Text = ds.Tables(1).Rows(0)(1)
addr.Text = ds.Tables(1).Rows(1)(1)
wangzhi.Text = ds.Tables(1).Rows(2)(1)
tel.Text = ds.Tables(1).Rows(3)(1)
email.Text = ds.Tables(1).Rows(4)(1)
ds.Clear()
ds.Dispose()
改
Dim ds As DataSet
ds = New DataSet()
ds.ReadXml(Server.MapPath("web.config"))
ds.Tables(1).Rows(0)(1) = setname.Text
ds.Tables(1).Rows(1)(1) = addr.Text
ds.Tables(1).Rows(2)(1) = wangzhi.Text
ds.Tables(1).Rows(3)(1) = tel.Text
ds.Tables(1).Rows(4)(1) = email.Text
ds.AcceptChanges()
ds.WriteXml(Server.MapPath("web.config"))
ds.Clear()
ds.Dispose()
msg.Text = "修改成功!"
最近做项目,用上了静态刷新,研究了下ajax,阿哦,要在web.config里加上一句话
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
</httpHandlers>
用上面的读取就完蛋了。。
怎么办?来个最恶心的做法吧。。。
读:
etname.Text = ConfigurationSettings.AppSettings("website")
addr.Text = ConfigurationSettings.AppSettings("addr")
wangzhi.Text = ConfigurationSettings.AppSettings("url")
tel.Text = ConfigurationSettings.AppSettings("tel")
email.Text = ConfigurationSettings.AppSettings("email")
Dim zt As Integer = ConfigurationSettings.AppSettings("denglu_rizhi")
改:
Dim XPath As String = "/configuration/appSettings/add[@key='?']"
Dim domWebConfig As New XmlDocument
domWebConfig.Load(HttpContext.Current.Server.MapPath("web.config"))
Dim websites As XmlNode = domWebConfig.SelectSingleNode(XPath.Replace("?", "website"))
websites.Attributes("value").InnerText = setname.Text
Dim addrs As XmlNode = domWebConfig.SelectSingleNode(XPath.Replace("?", "addr"))
addrs.Attributes("value").InnerText = addr.Text
Dim wangzhis As XmlNode = domWebConfig.SelectSingleNode(XPath.Replace("?", "url"))
wangzhis.Attributes("value").InnerText = wangzhi.Text
Dim tels As XmlNode = domWebConfig.SelectSingleNode(XPath.Replace("?", "tel"))
tels.Attributes("value").InnerText = tel.Text
Dim emails As XmlNode = domWebConfig.SelectSingleNode(XPath.Replace("?", "email"))
emails.Attributes("value").InnerText = email.Text
domWebConfig.Save(HttpContext.Current.Server.MapPath("web.config"))
msg.Text = "修改成功!"
展开全部
代码如下
Imports System.Web.Util
Imports System.Xml
Imports System.Xml.XPath
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
Dim ConnString As String
Dim strdatabase As String
strdatabase = ConfigurationSettings.AppSettings("database")//换上你自己的
ConnString = Server.MapPath("page\data\" + strdatabase)
If ConnString <> Nothing Then
Dim filename As String = Server.MapPath("web.config")
Dim xmldoc As XmlDocument = New XmlDocument
xmldoc.Load(filename)
'修改连接字符串
root = xmldoc.SelectSingleNode("descendant::configuration/appSettings/add[@key='ConnString']")
root.Attributes("value").Value = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ConnString + ";Persist Security Info=False"
xmldoc.Save(filename)
End If
Dim strUrl As String = ""
strUrl = "page\index.htm"
Response.Redirect(strUrl)//改完重登
End Sub
Imports System.Web.Util
Imports System.Xml
Imports System.Xml.XPath
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
Dim ConnString As String
Dim strdatabase As String
strdatabase = ConfigurationSettings.AppSettings("database")//换上你自己的
ConnString = Server.MapPath("page\data\" + strdatabase)
If ConnString <> Nothing Then
Dim filename As String = Server.MapPath("web.config")
Dim xmldoc As XmlDocument = New XmlDocument
xmldoc.Load(filename)
'修改连接字符串
root = xmldoc.SelectSingleNode("descendant::configuration/appSettings/add[@key='ConnString']")
root.Attributes("value").Value = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ConnString + ";Persist Security Info=False"
xmldoc.Save(filename)
End If
Dim strUrl As String = ""
strUrl = "page\index.htm"
Response.Redirect(strUrl)//改完重登
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果要修改的话最好别用web.config
放在类中
class DB
{
private static string _connstring = "";//赋个默认的
public static string ConnStrinig
{
get { return _connstring; }
set { _connstring = Value; }
}
}
放在类中
class DB
{
private static string _connstring = "";//赋个默认的
public static string ConnStrinig
{
get { return _connstring; }
set { _connstring = Value; }
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你可以多写几个链接地址的!这样在你要对数据库操作时你可以传你的链接实例就行了!每个实例对应一个数据库链接!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
哥几个一看就知道是.NET 1.1出身,对.NET 2.0不熟悉。
using System.Web.Configuration;
可以直接通过WebConfigurationManager.ConnectionStrings["connstring"].ConnectionString来获取或者设置这个connectionStrings节。
using System.Web.Configuration;
可以直接通过WebConfigurationManager.ConnectionStrings["connstring"].ConnectionString来获取或者设置这个connectionStrings节。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询