是否需要在单独的模块中分离连接代码 [英] Is it necessary to separate the connection code in a separate module

查看:54
本文介绍了是否需要在单独的模块中分离连接代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.在我的vb.net学习新旅程中,我朝着真正的编程实践迈进了一步.我在SQL Server 2008中有几种形式和2个表.数据发送和接收一切正常.

Hi. In my new journey of vb.net learning, I am stepping ahead in real programming practices. I have a couple of forms and 2 tables in SQL Server 2008. Data Sending and receiving is going all fine.

这是我在form1和Business_Info表单上的代码.

Here is my code on form1 and Business_Info Form.

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Dim con As New SqlConnection(cnStr) ' cnStr is declare as public in module
        Dim insQue As String = "Insert into Contact (Contact_ID, Name, Address, Cont_No) values (@Contact_ID, @Name, @Address, @Cont_No)"

        Dim cmd As New SqlCommand(insQue, con)

        con.Open()

        With cmd

            .Parameters.Add("@Contact_ID", SqlDbType.Int).Value = txtCID.Text.Trim
            .Parameters.Add("@Name", SqlDbType.VarChar).Value = txtName.Text.Trim
            .Parameters.Add("@Address", SqlDbType.VarChar).Value = txtAdd.Text.Trim
            .Parameters.Add("@Cont_No", SqlDbType.VarChar).Value = txtContNum.Text.Trim

        End With

        cmd.ExecuteNonQuery()

        con.Close()
        MessageBox.Show("its inserted")

        If chkShowBusiness.Checked = True Then
            My.Forms.Business_Information.Show()
        End If
    End Sub


和业务信息"表单加载事件代码:

Private Sub Business_Information_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim con As New SqlConnection(cnStr)
        Dim da As New SqlDataAdapter
        Dim ds As New DataSet

        Dim selQue As String = "Select * from Contact"
        da.SelectCommand = New SqlCommand(selQue, con)

        con.Open()

        da.Fill(ds, "Contact")

        cmbContName.DataSource = ds.Tables("Contact")

        cmbContName.DisplayMember = "Name"
        cmbContName.ValueMember = "Contact_ID"

    End Sub

并保存按钮代码:

Private Sub btnSaveBus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveBus.Click

        Dim con As New SqlConnection(cnStr)
        Dim insQue As String = "Insert into Business_Info (Bus_ID, Contact_ID, Bus_Name, Bus_Loc, Bus_Status) values (@Bus_ID, @Contact_ID, @Bus_Name, @Bus_Loc, @Bus_Status)"

        Dim cmd As New SqlCommand(insQue, con)

        con.Open()

        With cmd
            .Parameters.AddWithValue("@Bus_ID", txtBID.Text.Trim)
            .Parameters.AddWithValue("@Contact_ID", cmbContName.SelectedValue)
            .Parameters.AddWithValue("@Bus_Name", txtBusName.Text.Trim)
            .Parameters.AddWithValue("@Bus_Loc", txtBusLoc.Text.Trim)
            .Parameters.AddWithValue("@Bus_Status", cmbBusStatus.Text)
        End With

        cmd.ExecuteNonQuery()
        con.Close()

        MessageBox.Show("its inserted")
    End Sub

我在互联网上搜索了一些信息,因此发现是在论坛上写的,在一个单独的模块中只能建立一次连接,这与我的代码不同, 我正在为每个代码建立连接.我不知道他们指的是什么,但我推断需要在单独的地方放置通用代码,并在各处使用通用代码?

I was googling some information on internet, so I found that it was written in forums that a connection should be made only once in a separate module not like my code where I am making connection for each code. I don't know what they were referring to but what I inferred that there need to be a separate place where the common code shall be put and used everywhere??

问题

1.我以正确的方式推断出来了吗?

2.我试图做的是添加一个模块内容(我不太了解它的细节),然后在那儿写了代码.

出于测试目的,我制作了一个模块

For test purpose I made a module

Module modConn

    Public cnStr As String = "Data Source = (local); Initial Catalog = AddressBook; User ID = sa; Password = colrh"

End Module

我不知道如何在单独的地方编写代码,但是我尝试过了,对吗?

I don't know that how to write the code in a separate place but I tried this, Is it right?

 

3.如果我必须在单独的模块中声明整个连接代码,那么我将如何在任何地方使用相同的东西,并且大多数人有必要这样做吗?

谢谢.

推荐答案

也许您应该看看另一幅图片.

Maybe you should look at a different picture.

https://en.wikipedia.org/wiki/关注的问题

https://en.wikipedia.org/wiki/Separation_of_concerns

https://msdn.microsoft.com/zh-CN/library/bb384398.aspx

https://msdn.microsoft.com/en-us/library/bb384398.aspx

http://www.codeproject.com/Articles/21115/Building-an-N-Tier-Application-in-VB-NET-in-Step

http://www.codeproject.com/Articles/21115/Building-an-N-Tier-Application-in-VB-NET-in-Step

http://www.codeproject.com/Articles/228214/Understanding-Basics-of-UI-Design-Pattern-MVC-MVP

http://www.codeproject.com/Articles/228214/Understanding-Basics-of-UI-Design-Pattern-MVC-MVP

您必须使用Bing或Google找到一个VB.NET示例,但使用C#的示例不是这样

You'll have to find a VB.NET example they are out there using Bing or Google, but not at the rate examples are in C#

http://www.codeproject.com/Articles/522809/WinForms-MVP-An-MVP-Framework-for-WinForms

http://www.codeproject.com/Articles/522809/WinForms-MVP-An-MVP-Framework-for-WinForms


这篇关于是否需要在单独的模块中分离连接代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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