连接数据库用什么方法?(我指的是当打开网页时会连接到数据库,这个数据库的位置是动态的,数据库的信息应该用什么方法保存一个配置文件?我试了一下XML好象不能用,我用的是vb.net写asp.net程序)谢谢另外大家能推荐几本ASP.NET的书吗?
解决方案 »
- 关于正则表达式的问题
- gridview导出到cxcel
- 关于js获取节点的问题
- 【在线等答案】基于asp.net用JQuery动态生成下拉导航条??哪位高人指点下小弟
- 关于昨天那个问题的更新
- 一个变量 两个事件,怎么办啊!!!!!!!
- 高分求助(在线等,万分急啊!):我的DataGrid的数据源是SqlDataReader,DataGrid自定义分页已经设置了VirtualItemCount,为什么点击分
- 请问如何精确定位动态插入的插件?
- 关于运行的问题
- 买过wrox的《ASP.NET web站点高级编程》这本书的大侠请进!立刻给分,拜托了!
- WebPart的最小化按钮自动消失????-在线等
- JavaScript版没人理,只好请这边的朋友也帮忙看看了,100分
修改时,可以用asp.net 操作 xml的方法来修改web.config的值。楼主看一下system.xml吧
用的是时候读取就可以了
谢谢!
----------
<configuration>
......
<appSettings>
<!--
设置数据库连接字符串配置
-->
<add key = "ConnectionString" value= "server=localhost;database=hotop100;User ID=sa;password=sa;"/>
</appSettings>
............
</configuration>
-----------
.cs
System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
---------------------
可以读取,但不能修改吧
---------------------
可以读取,但不能修改吧可以更改的,但是,如果更改了,那么Session将会丢失!http://community.csdn.net/Expert/topic/4708/4708648.xml?temp=.377331
<appSettings>
<add key="strConn" value="workstation id=All;packet size=4096;UID=sa;password=;data source=(local);persist security info=True;initial catalog=TestOnline;Max Pool Size=500;Min Pool Size=5"/>
</appSettings>
也有的在程序中,用static变量存储数据库信息。明白?
<!--
设置数据库连接字符串配置
-->
<add key = "ConnectionString" value= "server=localhost;database=hotop100;User ID=sa;password=sa;"/>
</appSettings>
............
</configuration>
-----------
.cs
System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
难道你还动态的生成新的数据库?
要是固定的多个数据库无非就在web.config里多add 几个key就是了。
然后根据不同情况读取不同的配置
我的意思是管理员可以指定数据库的位置,这样数据库的位置就不是固定的了。
所以我需要在管理员修改数据库位置后能连接上,就导致了这个问题的产生。
<configuration>
<appSettings>
<!--数据库位置-->
<DataS>C:\11.mdb</DataS>
<!--数据库密码-->
<DataP>123456</DataP>
</appSettings>
<system.web>
……
</system.web>
</configuration>然后在一个BUTTON的CLICK事件里添加下面代码 Dim XmlDoc As New System.Xml.XmlDocument()
Dim XmlRead As System.Xml.XmlNode = XmlDoc.DocumentElement
Dim XmlSelect As System.Xml.XmlNode
XmlDoc.Load(Server.MapPath("web.config"))
XmlSelect = XmlRead.SelectSingleNode("/configuration/appSettings/DataS")
TextBox1.Text = XmlSelect.InnerText.ToString但是结果却是报错。Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.Source Error:
Line 35: Dim XmlSelect As System.Xml.XmlNode
Line 36: XmlDoc.Load(Server.MapPath("web.config"))
Line 37: XmlSelect = XmlRead.SelectSingleNode("/configuration/appSettings/DataS")‘这行出错!!!!
Line 38: TextBox1.Text = XmlSelect.InnerText.ToString
Stack Trace: [NullReferenceException: Object reference not set to an instance of an object.]
wdw.WebForm1.Button1_Click(Object sender, EventArgs e) in C:\Inetpub\wwwroot\wdw\index.aspx.vb:37
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102 什么原因啊?
谢谢