如何在数据库的单个列中存储多个文本框值? [英] how to store multiple textbox values in a single column of a database?
问题描述
先生/妈妈,
我有一个领域
电话号码,
该字段具有三个文本框,一个用于国家代码,另一个用于区域代码,最后一个用于电话号码.
现在,我如何将这些文本框值存储在数据库中?
当我们只有一个文本框时,我们通过
添加
SqlCommand comm.Parameters.AddWithValue("@emailadd", TextBox1.Text);
因为我有三个文本框,所以我的问题是如何将这些文本框值存储在数据库中?
谢谢,
嘿!
您是否尝试过将字段与"+"运算符一起添加?
SqlCommand comm.Parameters.AddWithValue(" ,TextBox1. Text + TextBox2.Text + TextBox3.Text);
上面的代码将起作用!但是,有一种更整洁的方法.如果要修改字符串,Microsoft建议使用StringBuilder
,它将更快地处理字符串操作.考虑下面的代码,例如:
StringBuilder phoneNumber = 新 StringBuilder(); phoneNumber.Append(" ); phoneNumber.Append(textBox1.Text); phoneNumber.Append(" ); phoneNumber.Append(textBox2.Text); phoneNumber.Append(" ); phoneNumber.Append(textBox3.Text); SqlCommand comm.Parameters.AddWithValue(" ,phoneNumber.ToString());
最后要注意的一点是:请注意,您正在使用@emailadd
参数插入电话号码,这可能是拼写错误?
祝你好运!
爱德华
我不明白您面临的实际问题是什么.
如果您有三个文本框,并且只想将数据存储在单个列中,那么首先要在本地变量中获取所有三个文本框的值,并根据自己的要求对其进行格式设置,然后将其作为参数传递给SQL
试试这个
string PhoneNumber= TextBox1.Text +" "+ TextBox2.Text +" "+ TextBox3.Text; SqlCommand comm.Parameters.AddWithValue("@PhoneNumber",PhoneNumber);
hi sir/mam,
i have a field
PhoneNumber,
this field has three textboxes,one for national code,other for region code and last one for phonenumber.
now,how would i store these textboxes value in database.
When we have single textbox then we add through
SqlCommand comm.Parameters.AddWithValue("@emailadd", TextBox1.Text);
Since,i have three textboxes,my problem is that how to store these textboxes value in database?
Thanku,
Hey!
Have you tried to add the fields together with the ''+'' operator?
SqlCommand comm.Parameters.AddWithValue("@emailadd", TextBox1.Text + TextBox2.Text + TextBox3.Text);
The code above will work! However, there''s a neater way. If you want to modify strings, Microsoft recommends using theStringBuilder
which will handle string operations way faster. Consider the code below for example :
StringBuilder phoneNumber = new StringBuilder(); phoneNumber.Append("("); phoneNumber.Append(textBox1.Text); phoneNumber.Append(") "); phoneNumber.Append(textBox2.Text); phoneNumber.Append(" "); phoneNumber.Append(textBox3.Text); SqlCommand comm.Parameters.AddWithValue("@emailadd", phoneNumber.ToString());
And then a final quick note : Please notice you''re using the@emailadd
parameter to insert a phone number, this may be a typo?
Good luck!
Eduard
I could not understand what is the actual problem you are facing.
If you have three textboxes and want to store the data in a single column only, then first get the value of all the three textboxes in a local variable and format it according your own requirement and pass it as a parameter to SQL
try this
string PhoneNumber= TextBox1.Text +" "+ TextBox2.Text +" "+ TextBox3.Text; SqlCommand comm.Parameters.AddWithValue("@PhoneNumber",PhoneNumber);
这篇关于如何在数据库的单个列中存储多个文本框值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!