所以我想要做的就是完全不同我试图以这种格式在文本文件中插入一组部件号。
NTM-120
NTM-130
NTM-140
NTM-150
NTM-160
NTM-170
NTM-180
NTM-190
NTM-200
NTM-210
所有部件的插入数据都是相同的,这就是我目前对单个插件的处理方式。
//Inserts Feature 1
SqlConnection sqlCon2 = new SqlConnection("REMOVED");
SqlCommand sqlCmd2 = new SqlCommand();
sqlCmd2.CommandText = "INSERT INTO [Products].[Features] ([ProductID] ,[Title] ,[ViewOrder]) VALUES ('" + textBox15.Text + "', '" + textBox19.Text + "', NULL) ";
sqlCmd2.Connection = sqlCon2;
sqlCon2.Open();
sqlCmd2.ExecuteNonQuery();
sqlCon2.Close();
//Inserts Feature 2
SqlConnection sqlCon3 = new SqlConnection("REMOVED");
SqlCommand sqlCmd3 = new SqlCommand();
sqlCmd3.CommandText = "INSERT INTO [Products].[Features] ([ProductID] ,[Title] ,[ViewOrder]) VALUES ('" + textBox15.Text + "', '" + textBox20.Text + "', NULL) ";
sqlCmd3.Connection = sqlCon3;
我的主要目标是选择一个文本文件并让它为文本框中的每个部分插入来自某些文本框(功能)的相同数据,并且必须将部分编号插入到列中,它们也是productID
这可能吗?
请帮助谢谢。 :d
答案 0 :(得分:0)
List<string> _partNumbers = File.ReadLines(@"Text File Path").ToList();
或
List<string> _partNumbers = File.ReadAllLines(@"Text File Path").ToList();
遍历List
,即_partNumbers
,并将它们插入数据库。
答案 1 :(得分:0)
我不明白如何在你的问题中使用文件,但如果你想在同一篇文章(textbox15)中添加许多文本框值,你可以这样做
List<TextBox> texts = new List<TextBox> { textBox16, textBox17, textBox20 };
foreach (var textBox in texts)
{
using (SqlConnection sqlCon2 = new SqlConnection("REMOVED"))
{
using (SqlCommand sqlCmd2 = new SqlCommand {CommandText = "INSERT INTO [Products].[Features] ([ProductID] ,[Title] ,[ViewOrder]) VALUES ('" + textBox15.Text + "', '" + textBox.Text + "', NULL) ", Connection = sqlCon2})
{
sqlCon2.Open();
sqlCmd2.ExecuteNonQuery();
}
}
}
<强>更新强>
如果你想插入textFile中存在的值,NTM是你的第一部分,120是第二部分
using (StreamReader sr = new StreamReader(filePath))
{
while (!sr.EndOfStream)
{
var readLine = sr.ReadLine();
if (readLine != null)
{
string[] strings = readLine.Split('-');
using (SqlConnection sqlCon2 = new SqlConnection("REMOVED"))
{
using (SqlCommand sqlCmd2 = new SqlCommand { CommandText = "INSERT INTO [Products].[Features] ([ProductID] ,[Title] ,[ViewOrder]) VALUES ('" + strings[0] + "', '" + strings[1] + "', NULL) ", Connection = sqlCon2 })
{
sqlCon2.Open();
sqlCmd2.ExecuteNonQuery();
}
}
}
}
}