问题
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
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;
}
}
回答1:
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();
}
来源:https://stackoverflow.com/questions/40037878/multiple-textbox-insert-into-to-sql-table-in-separate-rows-using-linq