将组合框的连接文本保存到数据库中 [英] Saving a combobox's concatenated text into a database

查看:67
本文介绍了将组合框的连接文本保存到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天



我正在尝试将在组合框中输入的文本保存到数据库中。但是我在组合框中有一个连接字段,我不想将整个文本保存到数据库中。

以下是我的代码。



  private   void  frmEmployeesJobs_Load(对象发​​件人,EventArgs e)
{
string empSql = string .Format( 选择(CAST(employee_id AS VARCHAR(MAX))+' - '+ employee_name)作为员工来自员工);

DataTable empTable = FillTable(empSql);
cmbEmp.DisplayMember = employee;
cmbEmp.ValueMember = employee;
cmbEmp.DataSource = empTable;

string jobSql = string .Format( 选择(CAST(job_id AS VARCHAR(MAX))+' - '+ job_name)作为job,job_price FROM job);

DataTable jobTable = FillTable(jobSql);

cmbJob.DisplayMember = job;
cmbJob.ValueMember = job_price;
cmbJob.DataSource = jobTable;
}

private void cmbJob_SelectedIndexChanged( object sender,EventArgs e)
{
if (cmbJob.SelectedIndex == -1)
{
txtPrice.Text = string .Empty;
}
else
{
txtPrice.Text = cmbJob.SelectedValue.ToString();
}
}



关注

解决方案

您可以通过以下方式拆分所选文本分离器(如果有的话)然后只存储您要存储的那部分。

SQL Server 2008 - 循环/拆分分隔的字符串 [ ^ ]是一个简单的提示,允许您基于分割字符串分隔符。


如果你只想从下拉列表中保存不是Id的文本,那么分割下拉文本就像



string str = cmbJob.SelectedValue.ToString();

string [] arr = str.Split('' - '');

// in arr [0] job_Id将在那里。

在arr [1]中,job_name将在那里。

你可以保存你想要的东西。

试试这个...

 txtPrice。文字 = cmbJob.SelectedValue.Substring (cmbJob.SelectedValue.ToString()。IndexOf(  _)+ 1); 


Good Day

i am trying to save text entered in a combo box into the database. however i have a concatenated field in the combo box and i don t want to save the entire text into the database.
below is my code.

private void frmEmployeesJobs_Load(object sender, EventArgs e)
        {
            string empSql = string.Format("Select  (CAST(employee_id AS VARCHAR(MAX))+ ' - '+employee_name) as employee FROM employee");

            DataTable empTable = FillTable(empSql);
            cmbEmp.DisplayMember = "employee";
            cmbEmp.ValueMember = "employee";
            cmbEmp.DataSource = empTable;

            string jobSql = string.Format("Select  (CAST(job_id AS VARCHAR(MAX))+ ' - '+job_name) as job, job_price  FROM job");

            DataTable jobTable = FillTable(jobSql);
            
            cmbJob.DisplayMember = "job";
            cmbJob.ValueMember = "job_price";
            cmbJob.DataSource = jobTable;
        }

        private void cmbJob_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbJob.SelectedIndex == -1)
            {
                txtPrice.Text = string.Empty;
            }
            else
            {
                txtPrice.Text = cmbJob.SelectedValue.ToString();
            }
        }


Regards

解决方案

You can split the selected text via the separater (if you have one) and then store only that portion that you want to store.
SQL Server 2008 - Loop through/split a delimited string[^] is a simple tip that allows you to split a string based on a delimiter.


If you want to save only text not Id from drop down then split text of drop down like

string str=cmbJob.SelectedValue.ToString();
string[] arr= str.Split(''-'');
//in arr[0] job_Id will be there.
And in arr[1] job_name will be there.
you can save what u want.


try this...

txtPrice.Text = cmbJob.SelectedValue.Substring(cmbJob.SelectedValue.ToString().IndexOf("_")+1);


这篇关于将组合框的连接文本保存到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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