vb中怎么把连接数据库的 字符串 用一个文件保存

k1014247445 2009-12-26 02:56:05

连接数据库时,每窗个口都要写那个连接的 字符串 很麻烦
关键是更改连接字符串是还得一个一个改
有什么方法把那个字符串存在一个文件里,然后各个窗口从那里读取连接字符串?
具体该怎么做?
...全文
100 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
k1014247445 2009-12-26
  • 打赏
  • 举报
回复
谢谢了
huming_h 2009-12-26
  • 打赏
  • 举报
回复
是的。
k1014247445 2009-12-26
  • 打赏
  • 举报
回复
哦,突然发现我的vb工程目录里有一个app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="WindowsApplication1.My.MySettings.Supermarket1ConnectionString"
connectionString="Data Source=localhost;Initial Catalog=supermarket;Persist Security Info=True;User ID=sa;Password=123"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.diagnostics>
<sources>
<!-- 本部分定义 My.Application.Log 的登录配置-->
<source name="DefaultSource" switchName="DefaultSwitch">
<listeners>
<add name="FileLog"/>
<!-- 取消以下部分的注释,以写入应用程序事件日志-->
<!--<add name="EventLog"/>-->
</listeners>
</source>
</sources>
<switches>
<add name="DefaultSwitch" value="Information" />
</switches>
<sharedListeners>
<add name="FileLog"
type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
initializeData="FileLogWriter"/>
<!-- 取消以下部分的注释,并用应用程序名替换 APPLICATION_NAME,以写入应用程序事件日志-->
<!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
</sharedListeners>
</system.diagnostics>
</configuration>






应该在代码里把连接字符串用这个connectionString代替就可以了吧?
huming_h 2009-12-26
  • 打赏
  • 举报
回复
这两个文件都有 <connectionStrings>节点,配置这个节点信息。
用的地方根据ConfigurationManager对象来获取配置好的连接字符串
web.config
<connectionStrings>
<add name="dbsql" connectionString="server=localhost;database=dbName;uid=sa;pwd=sa"/>
</connectionStrings>

cs文件
public static string connectionString = ConfigurationManager.ConnectionStrings["dbsql"].ConnectionString;
k1014247445 2009-12-26
  • 打赏
  • 举报
回复
可以再详细点吗?
谢谢
huming_h 2009-12-26
  • 打赏
  • 举报
回复
webform---》web.config文件
winform---》app.config文件

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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