如何将两个列表框项插入数据库c#窗口窗体的同一表的两列中
本文关键字:窗体 窗口 两列 插入 两个 列表 数据库 | 更新日期: 2023-09-27 18:03:53
我正试图在数据库的同一表的两列中插入两个列表框项,但我不能,请您帮助我。
请查看下面的代码:
public partial class Form1 : Form
{
public static string var = "";
SqlConnection con = new SqlConnection("Data Source=DESKTOP-6056TSF;Initial Catalog=LibararyManagement;Integrated Security=True");
Form2 f2 = new Form2();
public Form1()
{
InitializeComponent();
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("Select * From Attribute where Test_Id like '"+Form2.a+"' ", con);
DataTable dt = new DataTable();
sda.Fill(dt);
con.Close();
var arr = new Label[dt.Rows.Count];
for (var i = 0; i < dt.Rows.Count; i++)
{
var lab = new Label();
lab.Text = dt.Rows[i][1].ToString();
// Other properties sets for tbox
this.panel1.Controls.Add(lab);
arr[i] = lab;
lab.Location = new Point(32, 32 + (i * 32));
lab.Width = 62;
}
textbox();
}
void textbox()
{
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("Select * From Attribute Where Test_Id like '"+Form2.a+"' ", con);
DataTable dt = new DataTable();
sda.Fill(dt);
con.Close();
var arr = new TextBox[dt.Rows.Count];
for (var i = 0; i < dt.Rows.Count; i++)
{
var texbox = new TextBox();
//teexbox.Text = dt.Rows[i][0].ToString();
// Other properties sets for tbox
this.panel1.Controls.Add(texbox);
arr[i] = texbox;
texbox.Location = new Point(122,32 + (i * 32));
}
}
private void button1_Click(object sender, EventArgs e)
{
foreach (TextBox text in panel1.Controls.OfType<TextBox>())
{
listBox2.Items.Add(text.Text);
}
foreach (Label lbl in panel1.Controls.OfType<Label>())
{
listBox1.Items.Add(lbl.Text);
}
foreach (var b in listBox2.Items)
{
string msg = b.ToString();
foreach (var a in listBox1.Items)
{
string message = a.ToString();
con.Open();
string saveStaff = "INSERT into Reg_Test_Atrribute(RTA_Name, RTA_Value , TR_Id ) " +
" VALUES ('" + message + "', '" + msg + "' ,'" + Form2.a + "');";
SqlCommand cmd = new SqlCommand(saveStaff, con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Sucessfully Saved");
}
}
}
}
它正在工作,但如果dt.row.counts = 4,则嵌套循环运行16次
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace dyna
{
public partial class Form1 : Form
{
public static string var = "";
SqlConnection con = new SqlConnection("Data Source=DESKTOP-6056TSF;Initial Catalog=LibararyManagement;Integrated Security=True");
Form2 f2 = new Form2();
public Form1()
{
InitializeComponent();
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("Select * From Attribute where Test_Id like '"+Form2.a+"' ", con);
DataTable dt = new DataTable();
sda.Fill(dt);
con.Close();
var arr = new Label[dt.Rows.Count];
for (var i = 0; i < dt.Rows.Count; i++)
{
var lab = new Label();
lab.Text = dt.Rows[i][1].ToString();
// Other properties sets for tbox
this.panel1.Controls.Add(lab);
arr[i] = lab;
lab.Location = new Point(32, 32 + (i * 32));
lab.Width = 62;
}
textbox();
}
void textbox()
{
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("Select * From Attribute Where Test_Id like '"+Form2.a+"' ", con);
DataTable dt = new DataTable();
sda.Fill(dt);
con.Close();
var arr = new TextBox[dt.Rows.Count];
for (var i = 0; i < dt.Rows.Count; i++)
{
var texbox = new TextBox();
//teexbox.Text = dt.Rows[i][0].ToString();
// Other properties sets for tbox
this.panel1.Controls.Add(texbox);
arr[i] = texbox;
texbox.Location = new Point(122,32 + (i * 32));
}
}
private void button1_Click(object sender, EventArgs e)
{
foreach (TextBox text in panel1.Controls.OfType<TextBox>())
{
listBox2.Items.Add(text.Text);
}
foreach (Label lbl in panel1.Controls.OfType<Label>())
{
listBox1.Items.Add(lbl.Text);
}
foreach (var b in listBox2.Items)
{
string msg = b.ToString();
foreach (var a in listBox1.Items)
{
string message = a.ToString();
con.Open();
string saveStaff = "INSERT into Reg_Test_Atrribute(RTA_Name, RTA_Value , TR_Id ) " +
" VALUES ('" + message + "', '" + msg + "' ,'" + Form2.a + "');";
SqlCommand cmd = new SqlCommand(saveStaff, con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Sucessfully Saved");
}
}}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace dyna
{
public partial class Form1 : Form
{
public static string var = "";
SqlConnection con = new SqlConnection("Data Source=DESKTOP-6056TSF;Initial Catalog=LibararyManagement;Integrated Security=True");
Form2 f2 = new Form2();
public Form1()
{
InitializeComponent();
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("Select * From Attribute where Test_Id like '"+Form2.a+"' ", con);
DataTable dt = new DataTable();
sda.Fill(dt);
con.Close();
var arr = new Label[dt.Rows.Count];
for (var i = 0; i < dt.Rows.Count; i++)
{
var lab = new Label();
lab.Text = dt.Rows[i][1].ToString();
// Other properties sets for tbox
this.panel1.Controls.Add(lab);
arr[i] = lab;
lab.Location = new Point(32, 32 + (i * 32));
lab.Width = 62;
}
textbox();
}
void textbox()
{
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("Select * From Attribute Where Test_Id like '"+Form2.a+"' ", con);
DataTable dt = new DataTable();
sda.Fill(dt);
con.Close();
var arr = new TextBox[dt.Rows.Count];
for (var i = 0; i < dt.Rows.Count; i++)
{
var texbox = new TextBox();
//teexbox.Text = dt.Rows[i][0].ToString();
// Other properties sets for tbox
this.panel1.Controls.Add(texbox);
arr[i] = texbox;
texbox.Location = new Point(122,32 + (i * 32));
}
}
private void button1_Click(object sender, EventArgs e)
{
foreach (TextBox text in panel1.Controls.OfType<TextBox>())
{
listBox2.Items.Add(text.Text);
}
foreach (Label lbl in panel1.Controls.OfType<Label>())
{
listBox1.Items.Add(lbl.Text);
}
con.Open();
String query = "INSERT INTO Reg_Test_Atrribute (RTA_Name,RTA_Value,TR_Id) VALUES(@RTA_Name,@RTA_Value,@TR_Id)";
SqlCommand command = new SqlCommand(query,con);
command.Parameters.Add(new SqlParameter("@RTA_Name", SqlDbType.VarChar));
foreach (var b in listBox2.Items)
{
command.Parameters["@RTA_Name"].Value = b.ToString();
}
command.Parameters.Add(new SqlParameter("RTA_Value", SqlDbType.VarChar));
command.Parameters.Add(new SqlParameter("@TR_Id", SqlDbType.VarChar));
foreach (var a in listBox1.Items)
{
command.Parameters["RTA_Value"].Value = a.ToString();
command.Parameters["@TR_Id"].Value = Form2.a.ToString();
command.ExecuteNonQuery();
}
con.Close();
/*con.Open();
string saveStaff = "INSERT into Reg_Test_Atrribute(RTA_Name, RTA_Value , TR_Id ) " +
" VALUES ('" + message + "', '" + msg + "' ,'" + Form2.a + "');";
SqlCommand cmd = new SqlCommand(saveStaff, con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Sucessfully Saved");
*/
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}