如何从sql server数据库中的各种控件保存多个值? [英] How to save multiple values from various controls in sql server database?

查看:85
本文介绍了如何从sql server数据库中的各种控件保存多个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用3层架构设计Windows应用程序。我的一个表单包含动态生成的控件。我的问题是如何从数据库中的控件中保存多个值。我使用了return语句,但由于它只返回一个值,因此我的数据库只存储了每个控件的一个值。基本上我正在创建BILL FORM。



我为每个控件创建了8个方法,并在业务逻辑层的函数中传递它们的值,但不是保存多个值而是保存如上所述,每个控件只有一个值。我也尝试使用元组但是它给了我转换异常



我的8种方法之一是:



I am designing windows application using 3-tier architecture. One of my forms contain dynamically generated controls. My question is that How can I save multiple values from controls in my database. I have used return statement but since it returns only one value my database stored only one value from each of the control. Basically I am creating BILL FORM.

I have created 8 methods one for each control and pass their values in business logic layer's function, but instead of saving multiple values it saved only one value of each control as mentioned above. I tried to use Tuples also but then it gave me "CONVERSION EXCEPTION"

One of my 8 methods is:

public string combo_box_1()
            {
                foreach (Control ctrl in this.Controls)
                {
                    if (ctrl is ComboBox)
                    {
                        if (ctrl.Name.Equals("combo_box_1"))
                        {
                            string main_category = ctrl.Text;
                            string[] arr1 = { main_category };
                            foreach (string alph in arr1)
                            {
                                MessageBox.Show(alph);
                                return alph;
                            }
                            return main_category;
                            
                        }
                    }
                }
                return null;



}



这是元组版本:




}

It's Tuple version:

public Tuple<string> combo_box_1()
           {
               foreach (Control ctrl in this.Controls)
               {
                   if (ctrl is ComboBox)
                   {
                       if (ctrl.Name.Equals("combo_box_1"))
                       {
                           string main_category = ctrl.Text;
                           Tuple<string> txt2 = new Tuple<string>(main_category);
                           return txt2;
                       }
                   }
               }
               return null;
           }





我的业务层功能(元组版):





My business layer function(Tuple Version):

public bool save_bill(Tuple<string> CB1, Tuple<string> CB2, Tuple<string> CB3, Tuple<string> CB4, Tuple<string> NUD1, Tuple<string> CB5, Tuple<string> TB1, Tuple<string> TB2)
            {
                try
                {
                    DAL obj = new DAL();
                    obj.OpenConnection();
                    obj.LoadSpParameters("save_bill", CB1, CB2, CB3, CB4, NUD1, CB5, TB1, TB2);
                    obj.ExecuteQuery();
                    obj.UnLoadSpParameters();
                    obj.CloseConnection();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }





我的保存按钮后面的编程(元组版本):









Programming behind my save button(Tuple Version):



private void save_button_Click(object sender, EventArgs e)
           {
               BLL obj = new BLL();
               bool obj2 = obj.save_bill(combo_box_1(), combo_box_2(), combo_box_3(), combo_box_4(), numeric_up_down_1(), combo_box_5(), txt_box_1(), txt_box_2());
               if (obj2 == true)
               {
                   MessageBox.Show("bill saved");
               }

               else
               {
                   MessageBox.Show("bill cannot be saved");
               }

           }

推荐答案

您可以创建如下的用户功能。



You Can create user function as below.

create FUNCTION fn_split(@MYSTR VARCHAR(500), @DELIMITER CHAR(1))
RETURNS @MYTBL  TABLE (idx smallint, value varchar(8000))
AS 
BEGIN
 DECLARE @RET VARCHAR(500)
 DECLARE @INDEX INT
 DECLARE @COUNTER smallint
 
 --Get the first position of delimiter in the main string
 SET @INDEX = CHARINDEX(@DELIMITER,@MYSTR)
 SET @COUNTER = 0
 
 --Loop if delimiter exists in the main string
 WHILE @INDEX > 0
 BEGIN
  --extract the result substring before the delimiter found
  SET @RET = SUBSTRING(@MYSTR,1, @INDEX-1 )
  --set mainstring right part after the delimiter found
  SET @MYSTR = SUBSTRING(@MYSTR,@INDEX+1 , LEN(@MYSTR) - @INDEX )
  --increase the counter
  SET @COUNTER = @COUNTER  + 1 
  --add the result substring to the table
  INSERT INTO @MYTBL (idx, value)
  VALUES (@COUNTER, @RET)
  --Get the next position of delimiter in the main string
  SET @INDEX = CHARINDEX(@DELIMITER,@MYSTR)
 END
 
 --if no delimiter is found then simply add the mainstring to the table
 IF @INDEX = 0 
 BEGIN
  SET @COUNTER = @COUNTER  + 1
  INSERT INTO @MYTBL (idx, value)
  VALUES (@COUNTER, @MYSTR)
 END 
 RETURN   
END





并调用函数获取像




And call the function to get the values like

select * from fn_split('test1,test2,test3',',')





在您的代码中,您可以指定分隔符代替','。



In your code you can specify your delimiter in place of ','.


这篇关于如何从sql server数据库中的各种控件保存多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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