根据用户选择添加多个房间 [英] Add multiple room based on user selected

查看:54
本文介绍了根据用户选择添加多个房间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我在做添加房间功能。我被困在添加多个房间。



例如:用户从下拉列表中选择数字10,然后点击添加。我希望它在数据库表中插入10行。这样可以避免我添加10次。



我自动生成房间ID

Currently i doing the add room function. I m stuck at the add multiple room.

example: user select the number "10" from dropdownlist, and clicks the add. I want it to insert 10 rows into the database table. This to avoid me to add 10 times.

I have autogenerate the roomID

Dim mySqlConn As MySqlConnection = New MySqlConnection("server=localhost;userid=root;password=1234;database=ace")
    Dim cmd As MySqlCommand




Private Sub autoGenerate()
        Dim curValue As Integer
        Dim result As String
        Dim cmd = New MySqlCommand("Select MAX(RID) FROM ROOMS", mySqlConn)
        result = cmd.ExecuteScalar().ToString()
        If String.IsNullOrEmpty(result) Then
            result = "RI0000"
        End If

        result = result.Substring(2)
        Int32.TryParse(result, curValue)
        curValue = curValue + 1
        result = "RI" + curValue.ToString("D4")
        TextBox2.Text = result

    End Sub





我的添加按钮





My Add button

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'User select room number
        Dim i as Integer = ComboBox2.Text
        If Button1.Text = "Button1" Then
            Try
                mySqlConn.Open()
                Dim query As String
                query = "Insert into ace.rooms (RID,ROOMID,STATUS) values (@RID, @ROOMID, @STATUS) "
                cmd = New MySqlCommand(query, mySqlConn)
                cmd.Parameters.AddWithValue("@RID", TextBox2.Text)
                cmd.Parameters.AddWithValue("@ROOMID", ComboBox1.SelectedValue)
                cmd.Parameters.AddWithValue("@STATUS", "No")
                cmd.ExecuteNonQuery()

                MessageBox.Show("Success")
                mySqlConn.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            Finally
                mySqlConn.Dispose()
            End Try
        ElseIf Button1.Text = "Update" Then


        End If

What I have tried:

Currently i no idea how to loop it. Can anyone help me to code this? Thanks and appreciate.

推荐答案

我会创建一个子程序(或者在VB.Net中调用的任何内容)并放置此代码



I would create a sub-procedure (or whatever its called in VB.Net) and put this code

cmd.Parameters.AddWithValue("@RID", TextBox2.Text)
cmd.Parameters.AddWithValue("@ROOMID", ComboBox1.SelectedValue)
cmd.Parameters.AddWithValue("@STATUS", "No")
cmd.ExecuteNonQuery()





,除了进入子程序,你必须传递你的参数,所以你有大概像





in it, with the exception that into the sub-procedure, you have to pass your parameters, so you have something roughly like

Private Sub  multipleRoomAdd(loopValue As Integer, cmd as MySqlCommand, tb2_Value As String, cb1_Value  As String, Status as String)
                cmd.Parameters.AddWithValue("@RID", tb2_Value)
                cmd.Parameters.AddWithValue("@ROOMID", cb1_Value)
                cmd.Parameters.AddWithValue("@STATUS", Status)
                cmd.ExecuteNonQuery()
End Sub





这样你就可以了做





so that you can then do

cmd = ... // As Per your code 

For addLoopIndex As Integer = 1 to i
    multipleRoomAdd(addLoopIndex, cmd, TextBox2.Text, ComboBox1.SelectedValue, "No")
End For





但是,你还没有走出困境!你需要考虑: -



1.使用LoopValue,从1到10(例如)改变/形成@ RID / tb2_Value和/或@ ROOMID / cb1_Value - 如果你不创建新的值,可能是基于循环#,你最终会得到所有相同的行,我怀疑那是你想要的(顺便说一下为什么这两个相似?) - 我d可能在这里使用Sub Procedure来变异/获取每次迭代所需的值



2.可能重置cmd参数



所以你可能最终得到





but, you're not out of the woods yet ! you need to think about :-

1. using LoopValue which will go from 1 to 10 (for example) to alter/form the values for @RID/tb2_Value and/or @ROOMID/cb1_Value - if you dont create new values, possibly based on the loop #, you'll end up with rows all the same, I doubt thats what you want (btw why are these two similar ?) - I'd likely use a Sub Procedure here to mutate/get the values required on each iteration

2. possibly reset the cmd parameters

so you possibly end up with

Private Sub  multipleRoomAdd(loopValue As Integer, cmd As MySqlCommand, tb2_Value As String, cb1_Value As String, Status As String)

                // Use loopValue 1..n and tb2_Value from TextBox2 to get new RID value 
                newRIDValue = getNewRID(loopValue, tb2_Value) 
                cmd.Parameters.AddWithValue("@RID", newRIDValue)

                // Use loopValue 1..n and cb1_Value from ComboBox1 to get new ROOMID value 
                newROOMIDValue = getNewROOMID(loopValue, cb1_Value)
                cmd.Parameters.AddWithValue("@ROOMID", newROOMIDValue)
              
                cmd.Parameters.AddWithValue("@STATUS", Status)
                cmd.ExecuteNonQuery()

                // ? Reset cmd Parameter List
                cmd.Parameters.Reset() ??
End Sub

//TODO 
// 1 Define a sub Procedure for getNewRID that given loopValue (eg 1..10) and The TextBox2.Text Value, returns a new (unique ?) RID 
// 2 Define a sub Procedure for getNewROOMID that given a loopValue (eg 1..10) and the ComboBox1.SelectedText Value, returns a new ROOMID





我不写VB - 所以只要用它作为一个想法你能做什么,不要逐字逐句地使用它,无论如何我怀疑它的合法VB - 祝你好运



I dont write VB - so just use this as an idea of what you could do, dont use it verbatim, I doubt any of its legal VB anyway - good luck


这篇关于根据用户选择添加多个房间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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