模块中的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.)
相关问答
更多-
问题已经解决了。 必须首先更改模块中单选按钮的顺序,然后将其更改为布尔值。 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方法。 ...
-
在VB.Net中更改IP地址(Change IP Address in VB.Net)[2022-09-12]
我可以回答我自己的问题。 我在洗澡时想到了它(陈词滥调怎么样?)。 因为这是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. -
VB.Net连接(VB.Net connection)[2022-05-16]
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 ... -
VB.Net模块行为(VB.Net module behavior)[2022-06-25]
Module在技术上并不意味着静态类。 VB.net中的静态(关于函数)是Shared ,并且没有Shared Class 。 我认为你想要的是一个带有静态/共享函数的密封/抽象/不可继承的类(你可以在没有父类实例的情况下调用函数,但是你仍然需要引用父类。调用函数)。 如果是这种情况,那么执行类似以下的操作: Public NotInheritable Class HelperA Public Shared Function FunctionA() as Boolean Return ... -
将VB.net模块转换为C#(convert VB.net Module to C#)[2023-03-05]
您可以将常量放在public static class如下所示: public static class MyConnectionStringConstants { public const string strDatabase = "****"; public const string strUserID = "****"; public const string strPssWd = "****"; } 要使用它,您需要像这样引用常量: string strAccessConn ...