保存Listbox的所有项目以访问VB.Net中的数据库表 [英] Save all items of Listbox to access Database Table in VB.Net

查看:92
本文介绍了保存Listbox的所有项目以访问VB.Net中的数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友

我的名字是Sarfaraz。我正在VB.Net和MS Access 2007中开发一个项目,我试图在数据库中保存患者的详细信息,如姓名,年龄,票号和实验室调查。

我在表格上有两个列表框,一个是可以完成所有可用的调查,另一个是选定的调查。当我点击保存按钮时,我想将所有选定的调查保存到数据库表中。

请帮助我。实际上我已经在vb6中完成了但在VB.Net中没有想法。

要从数据库加载可用的调查,我使用下面的代码,它工作正常。



Hello friends
My name is Sarfaraz. I am developing a project in VB.Net and MS Access 2007 where I am trying to save the details of a patient like name ,age, Ticket No and Lab investigations in the database.
I have two listboxes on the form one is having all the available investigations which can be done and the other is the selected investigations. When I click on the save button i want to save all the selected investigations to be stored in the database table.
Please help me in that. Actually i have done it in vb6 but no idea in VB.Net.
To load the available investigations from the database i use the following code which is working fine.

Sub loadinvestigations()
       Con.Open()
       Dim cmd As New OleDbCommand("select * from investigation", Con)
       Dim dr As OleDbDataReader = cmd.ExecuteReader
       While dr.Read
           ListBox1.Items.Add(dr(0).ToString.ToUpper)
       End While
       dr.Close()
       Con.Close()
   End Sub
Thank you

推荐答案

以下是答案:< br $> b $ b

Here is the answer :

Imports System.Data.OleDb
Public Class Form1

    Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click


        Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=PatientLabInvestigations.accdb;")

        Dim cmd As New OledbCommand

        Dim var1 As String

        con.Open()

        cmd.Connection = con

        For l_index As Integer = 0 To lstlabinvestigations.Items.Count - 1
            var1 = String.Concat("'", txtpatientname.Text.ToString(), "','", txtpatintage.Text.ToString(), "','", txtpatientticketno.Text.ToString(), "','", CStr(lstlabinvestigations.Items(l_index)).ToString(), "'")
            cmd.CommandText = "INSERT INTO tblLabInvestigations(PatientName,Age,TicketNo,LabInvestigation) VALUES(" + var1 + ")"
            cmd.ExecuteNonQuery()
        Next
        MessageBox.Show("Record saved successfully")
    End Sub

End Class





希望这会有所帮助。尝试,如果确实有效,那么接受并投票给出这个答案



Hope this helps. Try and if does work then accept and vote this answer


我尝试了以下代码并且工作正常

I have tried the following code and that worked correctly
<br />
 Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click<br />
        Dim Str As String<br />
        Try<br />
            Dim i As Integer<br />
            con.Open()<br />
            For i = 0 To ListBox2.Items.Count - 1<br />
                Str = "insert into login values('" + ListBox2.Items.Item(i) + "')"<br />
<br />
<br />
<br />
<br />
                Dim cmd As OleDbCommand = New OleDbCommand(Str, con)<br />
                cmd.ExecuteNonQuery()<br />
                '  Dim Dst As DataSet<br />
<br />
                Dim dad As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM login ORDER BY username", con)<br />
                ' Dad.Fill(Dst, "login")<br />
            Next<br />
            MsgBox("Record inserted successfully...")<br />
            ';   con.Close()<br />
        Catch ex As Exception<br />
            MessageBox.Show("Could Not Insert Record!!!")<br />
            MsgBox(ex.Message & " -  " & ex.Source)<br />
<br />
            con.Close()<br />
        End Try<br />
<br />
<br />
<br />
        Con.Close()<br />
<br />
    End Sub<br />


这篇关于保存Listbox的所有项目以访问VB.Net中的数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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