如何使用C#从Texbox将数据插入dataBase中的两个单独列中 [英] How to insert the data into two seperate columns in dataBase from Texbox usingg C#

查看:87
本文介绍了如何使用C#从Texbox将数据插入dataBase中的两个单独列中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将数据插入到Texbox的dataBase中的两个单独的列中使用C#



i也希望将数据插入dataBase中的两个单独的列

这里我有2个多行文本框

如下





forBoxI1 for InSerailNo(InModel)

textBox2 for OutSerailNo(OutModel)



这里在textbox1中输入数据,如T0PQR,T0ABC

textbox2 i enter数据如T0123,T0783



当我点击插入按钮时,值应插入数据库表中,如下所示



DataBAse表

--------------



InSerial | OutSerail

----------------------

T0PQR | T0123

---------------------

T0ABC | T0783

----------------------

How to insert the data into two seperate columns in dataBase from Texbox usingg C#

i want too insert data into two seperate columns in dataBase
here i have 2 multiline Textboxs
namely as follows


textBox1 for InSerailNo(InModel)
textBox2 for OutSerailNo(OutModel)

here in textbox1 i enter data like T0PQR,T0ABC
textbox2 i enter data like T0123,T0783

when i click on Insert Button the values should be insert in database table as follows

DataBAse Table
--------------

InSerial | OutSerail
----------------------
T0PQR | T0123
---------------------
T0ABC | T0783
----------------------

推荐答案

如果在你将使用的文本框,意味着你的问题更简单。

使用Split字符串将字符串吐入数组,



这里例如:

if in your text box you will use , means your question is more simple.
Use the Split string to spit the strings into array by ","

here for example:
                string[] insert__Value1= Regex.Split(textBox1.Text , ",");
    string[] insert__Value2= Regex.Split(textBox2.Text , ",");

for(int i=o;i<insert__value1.length;i++)>
{
insert into your tablename (value1,value2) values('"+ insert__Value1[i] + "','"+insert__Value2[i]+"'");
}


看你需要根据列名传递两个texbox值

演示代码如下:

See you need to pass both of texbox value as per your column name
Demo code was given below.
protected void btnSubmit_Click(Object Sender, EventArgs e)
{
SqlConnection con=new SqlConnection(connectionStringParameter)
con.Open();
string query="insert into table_name(InSerial,OutSerail)values
('"+textbox1.Text+"','"+textbox2.Text+"')";
SqlCommand cmd=new SqlCommand(query,con);
cmd.ExecuteNonQuery();
con.Close();
}


这篇关于如何使用C#从Texbox将数据插入dataBase中的两个单独列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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