模块中的Vb.Net设置连接(Vb.Net setting connection in Module)
我正在创建一个新的Vb.Net项目,我正在寻找创建一个模块,该模块将在项目运行时启动,以便设置连接。 我创建了一个名为Connection的新模块,并将以下代码放在那里......
Imports System.Data.SqlClient Module Connection Sub main() Dim sConnection As String = "Data Source=Van;Initial Catalog=OP;User ID=userid;Password=password" End Sub End Module
现在,在我的Form1中,我添加了SQLConnection组件并尝试执行此类操作....
Using Con as New SQLConnection(sConnection)
'但这似乎不起作用。 连接字符串正常工作,因为如果我将其包含在表单本身中它完全正常工作。
发生这种情况有什么特别的原因吗? 另外,假设我在应用程序中有30个表单,是否需要将SqlConnection组件添加到需要与DB通信的每个表单中?
I'm creating a new Vb.Net project and I'm looking to create a module that will fire up when project is run so that the connection is set. I created a new module called Connection and placed the following code there...
Imports System.Data.SqlClient Module Connection Sub main() Dim sConnection As String = "Data Source=Van;Initial Catalog=OP;User ID=userid;Password=password" End Sub End Module
And now in my Form1 I added the SQLConnection component and attempt to do something like this....
Using Con as New SQLConnection(sConnection)
'but this does not seem to work. The connection string works properly since it's fully working if I include it in the form itself.
Any particular reason why this is happening? Also, say I have 30 forms in the app, do I need to add the SqlConnection component to each form that will need to talk to the DB?
满意答案
“Main()”是一个功能。 并且您声明了一个局部变量“sConnection”。
您的表格是另一个类。
类只能访问其成员,全局成员或全局静态成员(或某些朋友方案,如C ++)。
从“Main”中取出声明,在表单范围内声明或将其声明为全局变量,表单可以访问。
或者将您的连接字符串放在配置文件中并从中读取。 (在以后的时间点很容易配置。)
The "Main()" is a function. And you declared a local variable "sConnection".
Your Form is another class.
A class can access only its members, global members or global static members (or some friend scenarios like C++).
Take out that declaration from "Main", either declare in the scope of your Form or declare it as a global variable, where your form can access.
Or put your connection string in a config file and read from it. (easy to configure at later point of time.)
相关问答
更多从VB.net中的模块中读取单选按钮(Reading radio buttons from module in VB.net)
在批处理文件中调用vb.net中的参数..?(Calling Parameters in vb.net within a batch file..?)
在VB.NET中使用模块被认为是不好的做法?(Are using modules in VB.NET considered bad practice?)
模块中的Vb.Net设置连接(Vb.Net setting connection in Module)
VB.NET如何在类模块中保存程序设置(VB.NET How to save Program settings inside class module)
如果禁用Application Framework,则将VB.Net托盘应用程序设置为单实例?(Setting a VB.Net tray app to single-instance if Application Framework is disabled?)
在VB.Net中更改IP地址(Change IP Address in VB.Net)
VB.Net连接(VB.Net connection)
VB.Net模块行为(VB.Net module behavior)
将VB.net模块转换为C#(convert VB.net Module to C#)
相关文章
更多Solr 4.6 | Setting Up an External ZooKeeper Ensemble | upgrade solr to Solr4.6
There is already an open DataReader associated with this Connection which must be closed first
Setting up Nutch 2.1 with MySQL to handle UTF-8
Hadoop master connection reset by peer resolution
The connection to adb is down, and a severe error has occured.
myEclipse8.5 New DataBase Connection Driver时出错
solr.net实践(二)
Solrj的使用
8 个最棒的 .NET 开发相关工具
最新问答
更多绝地求生、荒野行动、香肠派对 哪个更好玩???(都是吃鸡类游戏)
如何在jQuery集合中选择第n个jQuery对象?(How to select the nth jQuery object in a jQuery collection?)
ASP NET使用jQuery和AJAX上传图像(ASP NET upload image with jQuery and AJAX)
SQL Server XML查询中包含名称空间的位置(SQL Server XML query with namespaces in the where exist)
宁夏银川永宁县望远镇哪里有修mp5的?
我想用更新的日期标记所有更新的行(I would like to mark all updated rows with the date that they have been updated)
郑州会计培训班
如何定位数组中的负数,并得到所有正数的总和?(How to target e negative number from an array, and get the sum of all positive numbers?)
在响应图像上叠加网格(Overlay grid on responsive image)
无法让POST在Azure网站上运行(Could not get POST to work on Azure Website)
Copyright ©2023 656463.com All Rights Reserved.滇ICP备2022006988号-50
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们,本站将尽快处理。谢谢合作!