公共连接字符串 [英] Public Coonection string

查看:75
本文介绍了公共连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我知道这听起来很有趣,但是,作为使用VB(6个月大)的新手,在没有适当的教与学指导的情况下,

每次打开表单时,我都会调用数据库连接.

如果我有5个表单,那么我将打开我的连接5次或更多次,并在关闭打开的表单时释放它..

如何在我的主窗体上打开数据库连接,然后单击其他窗体,就可以识别并读取在主窗体上打开的连接字符串.

这是我的代码

 公共 共享 oCon  As   OleDb.OleDbConnection
公共 共享 oDs  As  新建数据集
公共 共享 oDa  As  OleDb.OleDbDataAdapter
公共 共享 oSQL,oTblName  As  字符串 



专用子DBConn()

 oCon.ConnectionString = " 
oCon.Open()


结束子

然后,在窗体的Load Event上调用DBConn(),在释放窗体时处理.

解决方案

一种选择是将连接字符串保存在app.config文件中,并从那里引用它. ..

这里是有关如何执行此操作的文章:用于提供应用程序配置数据的配置设置文件 [ ^ ]


Hi,

I know this may sound funny but, as a newbie in using VB (6months old), without proper teaching or learning guides,

I do call my db connection everytime I open a form.

If i have 5 forms, then i will open my connection 5 times or more than, and releases it when the opened form is closed..

How can i do opening my db connection on my Main form, then when i clck on the other form, the connection string that i opened on my Main Form is recognized and read.

Here''s my code

Public Shared oCon As New OleDb.OleDbConnection
Public Shared oDs As New DataSet
Public Shared oDa As OleDb.OleDbDataAdapter
Public Shared oSQL, oTblName As String



Private Sub DBConn()

oCon.ConnectionString = "Provider=SQLOLEDB;UID=sa;PWD=citadbo;Initial Catalog=Citadeldbo;Data Source=ANALYST04NEW\SQLEXPRESS"
oCon.Open()


End Sub

DBConn() is then called at the Form''s Load Event, Disposes when the form is released.

And to which i repeatedly copy and paste to my other forms.

解决方案

One option is to save your connection string in the app.config file and reference it from there...

Here''s an article about how to do this: Configuration Settings File for providing application configuration data[^]


这篇关于公共连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆