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

相关问答

更多

从VB.net中的模块中读取单选按钮(Reading radio buttons from module in VB.net)

问题已经解决了。 必须首先更改模块中单选按钮的顺序,然后将其更改为布尔值。 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

在批处理文件中调用vb.net中的参数..?(Calling Parameters in vb.net within a batch file..?)

在批处理脚本中,%~f0将返回批处理文件的路径和文件名。 启动应用程序时,使用%~f0将批处理文件名传递给.exe。 在VB应用程序中,您只需获取在命令行中传递的参数。 找出正在运行的批处理文件的文件名 因此,如果我正确理解您,可以将描述作为参数传递到批处理文件中,批处理文件可以将描述和脚本名称传递给vb应用程序。 c:\temp\test.bat "My Lovely Description" 在您的批处理文件中: c:\vbapp.exe "%1" "%~f0" 在VB中处理命令行参数非常简单...

在VB.NET中使用模块被认为是不好的做法?(Are using modules in VB.NET considered bad practice?)

Centro是正确的,一个模块(或带有共享成员的不可继承类)与C#静态类最接近 。 所以从技术上讲,没有什么是错的,因为它只是VB创建这种类的方法之一。 例如,您不能在VB中说Public Shared Class Settings ,因为您无法将Shared关键字放在类上。 就其本身而言,如果某个特定的情况需要一个模块,我不会称之为不好的做法,否则一个模块(或其他静态类的等价物)可能不是您想要的具有松散耦合,可测试代码的设计选择。 此外,尽管带有共享成员的不可继承类比描述模块更具描述性,但至少有...

模块中的Vb.Net设置连接(Vb.Net setting connection in Module)

“Main()”是一个功能。 并且您声明了一个局部变量“sConnection”。 您的表格是另一个类。 类只能访问其成员,全局成员或全局静态成员(或某些朋友方案,如C ++)。 从“Main”中取出声明,在表单范围内声明或将其声明为全局变量,表单可以访问。 或者将您的连接字符串放在配置文件中并从中读取。 (在以后的时间点很容易配置。) The "Main()" is a function. And you declared a local variable "sConnection". Your...

VB.NET如何在类模块中保存程序设置(VB.NET How to save Program settings inside class module)

您可以使用二进制序列化程序创建自己的设置文件。 此方法可用于将设置类的实例存储到文件中,该文件不是非常易读的。 如果需要人类可读性和可编辑性,则可以使用xml序列化程序。 设置文件将驻留在应用程序目录中。 您可以使用变量settingsFileName来控制它。 创建一个新的控制台应用程序并粘贴下面的代码。 运行它几次并注意“连接字符串”通过应用程序关闭和打开持久化。 Imports System.IO Imports System.Runtime.Serialization.Formatters...

如果禁用Application Framework,则将VB.Net托盘应用程序设置为单实例?(Setting a VB.Net tray app to single-instance if Application Framework is disabled?)

您找不到“SingleInstance”属性或类似的任何属性,因为它不存在。 单个实例的概念是当您在项目属性中选中“创建单个实例应用程序...”框时,Visual Basic编译器为您添加的编译器魔法。 如果您想将visual basic magic与仅托盘应用程序一起使用,则需要自己管理启动代码的几个部分。 你需要两段代码来完成这项工作。 继承自Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase并覆盖OnRu...

在VB.Net中更改IP地址(Change IP Address in VB.Net)

我可以回答我自己的问题。 我在洗澡时想到了它(陈词滥调怎么样?)。 因为这是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)

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 = Ne...

VB.Net模块行为(VB.Net module behavior)

Module在技术上并不意味着静态类。 VB.net中的静态(关于函数)是Shared ,并且没有Shared Class 。 我认为你想要的是一个带有静态/共享函数的密封/抽象/不可继承的类(你可以在没有父类实例的情况下调用函数,但是你仍然需要引用父类。调用函数)。 如果是这种情况,那么执行类似以下的操作: Public NotInheritable Class HelperA Public Shared Function FunctionA() as Boolean R...

将VB.net模块转换为C#(convert VB.net Module to C#)

您可以将常量放在public static class如下所示: public static class MyConnectionStringConstants { public const string strDatabase = "****"; public const string strUserID = "****"; public const string strPssWd = "****"; } 要使用它,您需要像这样引用常量: string strAcces...

相关文章

更多

Solr 4.6 | Setting Up an External ZooKeeper Ensemble | upgrade solr to Solr4.6

4.1----->4.6 Solr从4.1到4.6还是有不少改变的。。。 一、solr.xml ...

There is already an open DataReader associated with this Connection which must be closed first

使用MVC4 EF Linq获取foreach列表循环的时候遇到了如下的问题:报错提示 Ther ...

Setting up Nutch 2.1 with MySQL to handle UTF-8

原文地址:http://nlp.solutions.asia/?p=180 These instruc ...

Hadoop master connection reset by peer resolution

一次分布式程序运行下来,发现执行到最后的时候出错造成了 job异常退出,然后重新启动namenode是 ...

The connection to adb is down, and a severe error has occured.

启动android模拟器时.有时会报The connection to adb is down, an ...

myEclipse8.5 New DataBase Connection Driver时出错

大侠看看,菜鸟求教。 这个密码指的是什么密码? 我把可能的秘密输出后有弹出这样的框, 问题补充 ...

solr.net实践(二)

Setting up the library Once you have created your ...

Solrj的使用

http://wiki.apache.org/solr/Solrj Solr1.3 ...

8 个最棒的 .NET 开发相关工具

本文向你介绍 8 款跟 .NET 开发相关的一些工具。 1)Open Source – Sharp D ...

最新问答

更多

绝地求生、荒野行动、香肠派对 哪个更好玩???(都是吃鸡类游戏)

PC上的绝地求生,是最早也是最火的大逃杀游戏。 荒野行动是网易抄袭蓝洞绝地求生制作的手游。相似度90%,还有他一起出的终结折2,这2款正在被蓝洞告,打官司呢。 手游上的绝地求生有2部都是蓝洞授权(收钱)给腾讯开发的正版ID手游。所以跟PC上做的一模一样,蓝洞也没话说。 加上吃鸡国服也是腾讯独家代理,所以根本没有什么可说的。只要这个类型的 过于相似的,腾讯都可以借蓝洞之手起诉。打压同行是国内BAT最爱干的事嘛! 香肠派对画风虽然不一样,但核心玩法还是跟人家正版的一样的,同样也是没有被授权的。 98

如何在jQuery集合中选择第n个jQuery对象?(How to select the nth jQuery object in a jQuery collection?)

你可以使用eq : var rootElement = $('.grid').find('.box').eq(0); rootElement.find('.a'); /* Use chaining to do more work */ You can use eq: var rootElement = $('.grid').find('.box').eq(0); rootElement.find('.a'); /* Use chaining to do more work */

ASP NET使用jQuery和AJAX上传图像(ASP NET upload image with jQuery and AJAX)

您可以自己手动设置FormData键和值。 Upload 创建FormData并设置新的键/值 $("#btnUpload").on("click", function(e) { e.preventDefault(); var file = $("#imguploader").get(0).file

SQL Server XML查询中包含名称空间的位置(SQL Server XML query with namespaces in the where exist)

您可能希望使用#temp.identXml.query而不是#temp.identXml.query 。 您可以在这里阅读更多相关信息SQL Server XML exists() 我相信你也可以像这样使用它 Select #temp.identXml.value('(/*:PersonIdentity/*:MasterIndexes/*:PersonIndex/*:SourceIndex)[1]','varchar(100)') as Ident ,#temp.identXml.value(

宁夏银川永宁县望远镇哪里有修mp5的?

胜利街有家电维修,电脑城,银川商场多得很…

我想用更新的日期标记所有更新的行(I would like to mark all updated rows with the date that they have been updated)

您可以使用更新后触发的触发器来执行此操作。 给出如下表: create table your_table (id int primary key, val int, last_update datetime) 每当您更新表中的内容时,此触发器将设置last_update值。 CREATE TRIGGER trigger_name ON your_table AFTER UPDATE AS BEGIN UPDATE your_table SET your_ta

郑州会计培训班

招生的,至于时间吗,就看你自己的时间段了,你可以致电0371-63300220.他们会帮你选择一下的。离你最近,最专业的培训班。

如何定位数组中的负数,并得到所有正数的总和?(How to target e negative number from an array, and get the sum of all positive numbers?)

只需创建一个条件来检查它是正数还是负数,然后定义一个空的数组negatives ,如果数字是负数,则将其推到负数组中,如果是正数,则将其添加到sum变量中,请查看下面的工作示例。 function SummPositive( numbers ) { var negatives = []; var sum = 0; for(var i = 0; i < numbers.length; i++) { if(numbers[i] < 0) { negati

在响应图像上叠加网格(Overlay grid on responsive image)

使用两个linear-gradient s,我们可以创建两个简单的线条,然后每隔n%重复一次background-size 。 它看起来像这样: background: linear-gradient(to bottom, #000 2px, transparent 2px), linear-gradient(to right, #000 2px, transparent 2px); background-size: 10%; 两个渐变创建两条相交的线,长度为百分比,如下所示: 使用默认的b

无法让POST在Azure网站上运行(Could not get POST to work on Azure Website)

最后我找到了答案......我不得不删除尾随的斜线! 我使用了“ https://example.com/api/messages/ ”,这将自动产生GET,无论我使用PostAsync还是PostAsJsonAsync。 使用“ https://example.com/api/messages”,GET和POST似乎都运行良好! Finally I've found the answer.... I had to remove the trailing slash! I've used "ht