使用LINQ将多个文本框插入到SQL表中的单独行中 [英] Multiple textbox insert into to sql table in separate rows using LINQ
问题描述
将文本框的值插入sql表的单个行的特定列usign LINQ没什么大不了,但是如何在新行中插入每个文本框的内容,例如,如果我们有5个文本框,则应创建sql表中有5个新行.下面是我如何将文本框插入单行的不同列中的方法:
Inserting values of text boxes into specific columns of a single row of a sql table usign LINQ is not a big deal but how to insert content of each textbox in new row, for example if we have 5 text boxes so it should create 5 new rows in sql table. Below is how I insert text boxes into different columns of a single row :
Table Name: tblModulRelayConfig
index fltCe fltUT1 fltUT2 fltUT3 fltUT4 fltUT5 fltUT6
1 75 Text1 Text2 Text3 Text4 Text5 Text6
2 76 Text1 Text2 Text3 Text4 Text5 Text6
using (myDataContext mydc= new myDataContext())
{
tblModulRelayConfig tblModuler = (from CE in mydc.tblCEs
where CE.fltCE == 75
select CE).FirstOrDefault();
if(tblModuler != null)//in order to update table
{
nyModul.fltUT1 = TextBox1.Text;
nyModul.fltUT2 = TextBox2.Text;
nyModul.fltUT3 = TextBox3.Text;
nyModul.fltUT4 = TextBox4.Text;
nyModul.fltUT5 = TextBox5.Text;
nyModul.fltUT6 = TextBox6.Text;
}
else // if it does no exist already
{
tblModulRelayConfig nyModul = new tblModulRelayConfig();
nyModul.fltUT1 = TextBox1.Text;
nyModul.fltUT2 = TextBox2.Text;
nyModul.fltUT3 = TextBox3.Text;
nyModul.fltUT4 = TextBox4.Text;
nyModul.fltUT5 = TextBox5.Text;
nyModul.fltUT6 = TextBox6.Text;
mydc.tblModulRelayConfigs.InsertOnSubmit(nyModul);
mydc.SubmitChanges();
}
}
但是如何如下创建表,我的意思是不是将文本框值插入单行中,而是要在单个按钮单击中的新行中插入每个文本框的数据:
But how to create a table as follow, I mean instead of inserting text boxe values in single line, I want to insert data of each text boxe in new rows in single button click:
tblModulRelayConfig
index fltCe fltUTNr fltUT
1 75 1 Text1
2 75 2 Text2
3 75 3 Text3
4 75 4 Text4
5 75 5 Text5
6 75 6 Text6
我尝试了以下代码,但没有中断循环,它仅将最后一个文本框数据添加到表中,而中断则仅添加第一个文本框值. 我尝试使用的代码来自以下链接: 在单独的文本框中显示Linq-to-SQL表行
I tried the following code but without break in loop it only add the last textboxe data into table and with break it only add the first textboxe value. The code I try to use is from below link : Show Linq-to-SQL table rows in separate text boxes
var textbox = GetAll(this, typeof(TextBox));
tblModulRelayConfig nyKanal = new tblModulRelayConfig ();
foreach (Control c in textbox)
{
textNr++;
if (c is TextBox)
{
var tx = ((TextBox)c);
nyKanal.fltUTNr = textNr;
nyKanal.fltUT=tx.Text;
sls.tblModulRelayConfig .InsertOnSubmit(nyKanal);
sls.SubmitChanges();
break;
}
}
推荐答案
您可以尝试此解决方案.我已经用包含文本框名称的fltUTName
替换了fltUTNr
.通过这种方式,您与ui的绑定更加牢固:
You may try this solution. I've replaced the fltUTNr
with fltUTName
that contains the name of the textboxes. In this way you have a stronger binding with your ui:
index fltCe fltUTName fltUT
1 75 TexBox1 Text1
2 75 TexBox2 Text2
3 75 TexBox3 Text3
4 75 TexBox4 Text4
5 75 TexBox5 Text5
6 75 TexBox6 Text6
int fltCE_item = 75;
using (myDataContext mydc= new myDataContext()){
List<tblModulRelayConfig> tblModuler = (
from CE in mydc.tblCEs
where CE.fltCE == fltCE_item
select CE
);
foreach(TextBox tx in GetAll(this, typeof(TextBox)){
if(tblModuler.Count > 0){ //update table
foreach(var CE in tblModuler)
if(CE.fltUTName.Equals(tx.Name))
CE.fltUT = tx.Text;
}else{ //insert: index should be autoseeded into the db;
tblModulRelayConfig nyModul = new tblModulRelayConfig();
nyModul.fltCe = fltCE_item;
nyModul.fltUTName = tx.Name;
nyModul.fltUT = tx.Text;
mydc.tblModulRelayConfigs.InsertOnSubmit(nyModul);
}
}
mydc.SubmitChanges();
}
这篇关于使用LINQ将多个文本框插入到SQL表中的单独行中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!