首页 \ 问答 \ 模块中的Vb.Net设置连接(Vb.Net setting connection in Module)

模块中的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?

更新时间:2022-05-16 22:05

最满意答案

“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.)

相关问答

更多
  • 问题已经解决了。 必须首先更改模块中单选按钮的顺序,然后将其更改为布尔值。 Dim student As Boolean problem has been solved. The order of the radio buttons within the module had to be changed first, and then changed to a Boolean. Dim student As Boolean
  • 在批处理脚本中,%~f0将返回批处理文件的路径和文件名。 启动应用程序时,使用%~f0将批处理文件名传递给.exe。 在VB应用程序中,您只需获取在命令行中传递的参数。 找出正在运行的批处理文件的文件名 因此,如果我正确理解您,可以将描述作为参数传递到批处理文件中,批处理文件可以将描述和脚本名称传递给vb应用程序。 c:\temp\test.bat "My Lovely Description" 在您的批处理文件中: c:\vbapp.exe "%1" "%~f0" 在VB中处理命令行参数非常简单,但如果您 ...
  • Centro是正确的,一个模块(或带有共享成员的不可继承类)与C#静态类最接近 。 所以从技术上讲,没有什么是错的,因为它只是VB创建这种类的方法之一。 例如,您不能在VB中说Public Shared Class Settings ,因为您无法将Shared关键字放在类上。 就其本身而言,如果某个特定的情况需要一个模块,我不会称之为不好的做法,否则一个模块(或其他静态类的等价物)可能不是您想要的具有松散耦合,可测试代码的设计选择。 此外,尽管带有共享成员的不可继承类比描述模块更具描述性,但至少有一种情况需 ...
  • “Main()”是一个功能。 并且您声明了一个局部变量“sConnection”。 您的表格是另一个类。 类只能访问其成员,全局成员或全局静态成员(或某些朋友方案,如C ++)。 从“Main”中取出声明,在表单范围内声明或将其声明为全局变量,表单可以访问。 或者将您的连接字符串放在配置文件中并从中读取。 (在以后的时间点很容易配置。) The "Main()" is a function. And you declared a local variable "sConnection". Your Form ...
  • 您可以使用二进制序列化程序创建自己的设置文件。 此方法可用于将设置类的实例存储到文件中,该文件不是非常易读的。 如果需要人类可读性和可编辑性,则可以使用xml序列化程序。 设置文件将驻留在应用程序目录中。 您可以使用变量settingsFileName来控制它。 创建一个新的控制台应用程序并粘贴下面的代码。 运行它几次并注意“连接字符串”通过应用程序关闭和打开持久化。 Imports System.IO Imports System.Runtime.Serialization.Formatters.Bina ...
  • 您找不到“SingleInstance”属性或类似的任何属性,因为它不存在。 单个实例的概念是当您在项目属性中选中“创建单个实例应用程序...”框时,Visual Basic编译器为您添加的编译器魔法。 如果您想将visual basic magic与仅托盘应用程序一起使用,则需要自己管理启动代码的几个部分。 你需要两段代码来完成这项工作。 继承自Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase并覆盖OnRun方法。 ...
  • 我可以回答我自己的问题。 我在洗澡时想到了它(陈词滥调怎么样?)。 因为这是Windows 7,所以我只需右键单击并以管理员身份运行程序。 I can answer my own question. I thought of it in the shower (how cliche right?). Because this is Windows 7, all I needed to was right-click and run the program as an administrator.
  • Dim custAdapter As SqlDataAdapter = New SqlDataAdapter( _ "SELECT * FROM dbo.Customers", customerConnection) Dim ordAdapter As OleDbDataAdapter = New OleDbDataAdapter( _ "SELECT * FROM Orders", orderConnection) Dim customerOrders As DataSet = New Dat ...
  • Module在技术上并不意味着静态类。 VB.net中的静态(关于函数)是Shared ,并且没有Shared Class 。 我认为你想要的是一个带有静态/共享函数的密封/抽象/不可继承的类(你可以在没有父类实例的情况下调用函数,但是你仍然需要引用父类。调用函数)。 如果是这种情况,那么执行类似以下的操作: Public NotInheritable Class HelperA Public Shared Function FunctionA() as Boolean Return ...
  • 您可以将常量放在public static class如下所示: public static class MyConnectionStringConstants { public const string strDatabase = "****"; public const string strUserID = "****"; public const string strPssWd = "****"; } 要使用它,您需要像这样引用常量: string strAccessConn ...

相关文章

更多

最新问答

更多
  • 在csproj中使用appdata环境变量(Use appdata environment variable in csproj)
  • 从背景返回后,Skobbler Map崩溃(Skobbler Map crashes after returning from background)
  • 如何保持对绑定服务的轮询?(How to keep polling a bound service?)
  • ASP.NET单选按钮jQuery处理(ASP.NET radio button jQuery handling)
  • Linux上的FORTRAN图形库(FORTRAN graphic library on Linux)
  • 我们如何根据索引更新dynamodb表(不基于primary has和range key)(how can we update dynamodb table based on index(not based on primary has and range key))
  • 功能包装避免重复(wrap of functions avoid duplicating)
  • Android BroadcastReceiver和Activity.onPause()(Android BroadcastReceiver and Activity.onPause())
  • 无法使用phonegap 2.4在Android上播放录音(unable to play audio recordings on android using phonegap 2.4)
  • VS2015 + Resharper:不要使用C#6(VS2015 + Resharper: Don't use C#6)
  • 大学电脑四级对初学者来说要多久能过
  • 特殊字符删除?(Special characters remove?)
  • Android视频教程现在网上的都比较零散呢?有些太坑爹了,感觉老师就是在想当然的讲
  • 计算同一个表中不同行之间的差异[重复](Calculate delta's between different rows in same table [duplicate])
  • Javaweb开发,技术路线是什么?该怎么写?
  • JavaScript只在php代码中执行一次(JavaScript only executes once inside php code)
  • 不兼容的字符编码:ASCII-8BIT和UTF-8(incompatible character encodings: ASCII-8BIT and UTF-8)
  • Clojure(加载文件)给出错误(Clojure (load-file) gives an error)
  • 为具有瞬态scala依赖性的spring-xd项目优化gradle(Optimize gradle for spring-xd project with transient scala dependency)
  • 如何才能在Alpha测试模式下发布我的应用程序?(How can I publish my app in Alpha test mode only?)
  • “没有为此目标安装系统映像”Xamarin AVD Manager(“No system images installed for this target” Xamarin AVD Manager)
  • maven中的Scalatest:JUnit结果(Scalatest in maven: JUnit results)
  • 使用android SDK将文件直接上传到存储桶中的文件夹(Upload a file directly to a folder in bucket using android SDK)
  • 是否应将plists导入CoreData?(Should plists be imported to CoreData?)
  • java.lang.reflect.InvocationTargetException JavaFX TableView(java.lang.reflect.InvocationTargetException JavaFX TableView)
  • 根据唯一列值动态创建多个子集(Dynamically create multiple subsets based on unique column values)
  • 使用CSS可以使HTML锚标签不可点击/可链接吗?(Is it possible to make an HTML anchor tag not clickable/linkable using CSS?)
  • 嵌套的模板可能性(Nested template possibilities)
  • 任何方式在iOS7 +上以编程方式打开蓝牙(Any way to turn on bluetooth programmatically on iOS7+)
  • 如何为给定的SQL查询编写JPA查询(How I can write JPA query for given SQL query)