在vb2010中做个子 [英] Make a sub in vb2010

查看:92
本文介绍了在vb2010中做个子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在vb2010中制作一个项目,并使用sql2005作为我的数据库,在这个项目中,我必须对每个表格进行大量数据操作,例如Insert,Update,Delete,Select和所有其他sql操作

我知道如何编写代码并进行sql查询. bt如果我像这样编写代码,我的项目将变得如此轻松.

我想在我的模块中为所有这些查询创建一个子对象,因此只需调用它们并为该子对象提供我要执行该任务的rquire查询



运算(选择"SQL查询此处",sqlc)
textbox1.text =数据
这样我就可以在一行中完成所有这些SQL操作

I am making a project in vb2010 and use sql2005 as my database, in this project i have to do lots of data opretion like Insert ,Update , Delete , Select and all other sql opration on every form

i know how to code for it and make sql queries . bt my proj will gona bi so lenthy if i code like this.

i want to make a sub in my module for all this queries , so by just call them and give the sub the rquire queries i want to do that task

smthing like

Opration(select,"THE SQL QUERIES HERE", sqlc)
textbox1.text = Data
so i can make all this sql opration in one line

Dim cammad As New SqlCommand(" SELECT [fldOcationName] FROM [FoodManagementSystem].[dbo].[OcationMaster] ", sqlc)
       rd = cammad.ExecuteReader
       While rd.Read
         Data = rd("fldOcationName")
       End While
       Try
           rd.Close()
       Catch ex As Exception
           MsgBox(ex.Message)
       End Try



有任何想法吗 ?请帮助所有.........



any ideas ? plz help all ........

推荐答案

例如,有一个小函数可以根据作为参数传递的SQL字符串填充数据表,可以尝试以下操作:
For example to have a small function to fill a datatable based on a SQL string passed as a parameter, you can try something like the following:
Module SqlModule
    Public Function ExecuteSelect(sql As String, connection As System.Data.SqlClient.SqlConnection) As System.Data.DataTable
        Dim command As New System.Data.SqlClient.SqlCommand(sql, connection)
        Dim table As New System.Data.DataTable
        Dim adapter As New System.Data.SqlClient.SqlDataAdapter(command)

        Try
            adapter.Fill(table)
        Catch ex As Exception
            MsgBox(ex.Message)
            Return Nothing
        End Try

        Return table
    End Function
End Module


我是使用插入查询完成操作,但是每当我尝试运行时都会在选择查询中出现问题,我收到错误提示,当rd关闭时,inveliad尝试调用读取器

我使用模块,并在其中放置我的sql opration代码,并将其称为另一种形式

我的模块代码是:
i am done with the insert query but have a problem in select query whenever i try to run i get the error that inveliad attempt to call reader when rd is close

i use module ,and put my sql opration code there and call it to another form

my code for module is :
Imports System.Data.SqlClient
Module Database_Module
    Public sqlc As New SqlClient.SqlConnection("Data Source=AIS-QUARDCORE\SQLEXPRESS;Initial Catalog= Student Record App ;Integrated Security=True;multipleactiveresultsets = true ")
    Dim sqlad As New SqlClient.SqlDataAdapter
    Public rd As SqlDataReader

    Public Function ExecuteSelect(ByVal Query As String, ByVal sqlc As System.Data.SqlClient.SqlConnection) As System.Data.DataTable

        Dim cammad As New SqlCommand(Query, sqlc)
        rd = cammad.ExecuteReader

        Try
            rd.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
            Return Nothing
    End Function
End Module



而我要调用的代码是在表单加载事件上.



and the code where i want to call is on form load event .

ExecuteSelect("SELECT  fldName FROM    dbo.Test", sqlc)
      While rd.Read
          ListBox1.Items.Add("fldName")
      End While


想要在加载时将数据添加到列表框
请帮助... ......


want to add data to listbox on load
plz help ......


这篇关于在vb2010中做个子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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