C#和SQL [英] C# and SQL
问题描述
我在6个蛋值在textBox1的(多)型 我有一个问题
我怎么得到它从查询到serialnumberdatas&NBSP打印出来的蛋值;
I type in 6 egg values in the textBox1(multiline).
I am having an issue of how do I get it to print out the egg values from the query to the serialnumberdatas
private void button1_Click(object sender, EventArgs e)
{
egg = textBox1.Text; //egg will show "EG216455\r\nEG216456\r\nEG216457\r\nEG216458\r\nEG216459\r\nEG216460"
int k = Convert.ToInt32(textBox2.Text);
for (int i = 0; i < k; i++)
{
serialnumberdata.DataSource = serialnumberdatas();
DataTable serialnumberdatas()
{
DataTable dtSerial = new DataTable();
string connString = ConfigurationManager.ConnectionStrings["pie"].ConnectionString;
using (SqlConnection con = new SqlConnection(connString))
{
using (var cmd = new SqlCommand("select eggcounter From [Product].[Counter01unit] where eggcounter = @egg", con))
{
cmd.Parameters.AddWithValue("@egg", egg);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
dtSerial.Load(reader);
}
con.Close();
}
return dtSerial;
}
}
推荐答案
嗨 hardheadedvs,
感谢您发布此处。
根据您的描述,我想你想搜索6个值
当 eggcounter匹配你在textBox1中输入的值。 k是您在textBox1中输入的值的计数。
According to your description, I think you want to search 6 values when eggcounter matches the value you input in the textBox1. k is the count of the values you input in textBox1.
如果是,请尝试以下代码。由于我没有您的数据,我使用自己的数据库进行测试。
If yes, please try the code below. Due to I do not have your data, I test with my own database.
private void button1_Click(object sender, EventArgs e)
{
string egg = textBox1.Text; //egg will show "EG216455\r\nEG216456\r\nEG216457\r\nEG216458\r\nEG216459\r\nEG216460"
string[] s = egg.Split(',');
int k = Convert.ToInt32(textBox2.Text);
DataTable dtSerial = new DataTable();
DataTable table = new DataTable();
for (int i = 0; i < k; i++)
{
//ConfigurationManager.ConnectionStrings["pie"].ConnectionString;
string connString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=Database.mdf;Integrated Security=True";
using (SqlConnection con = new SqlConnection(connString))
{
using (var cmd = new SqlCommand("SELECT ItemCode,ItemName FROM Table1 where ItemCode = @egg", con))//select eggcounter From [Product].[Counter01unit] where eggcounter = @egg
{
cmd.Parameters.AddWithValue("@egg", s[i]); //do not use egg, use s[i]
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
dtSerial.Load(reader);
table = dtSerial.Clone();
foreach (DataRow dr in dtSerial.Rows)
{
table.Rows.Add(dr.ItemArray);
}
}
con.Close();
}
}
dataGridView1.DataSource = dtSerial;
}
在输入&NBSP;的 EG216455\r\\\
EG216456\r\\\
EG216457\r\\\
EG216458\r\\\
EG216459\r\\\
EG216460在TextBox1中,您可以使用下面的代码进行拆分。
When you input EG216455\r\nEG216456\r\nEG216457\r\nEG216458\r\nEG216459\r\nEG216460 in TextBox1, you could use the code below to split.
Change:
string[] s = egg.Split(',');
int k = Convert.ToInt32(textBox2.Text);
To:
string[] stringSeparators = new string[] { "\\r\\n" };//EG216455\r\nEG216456\r\nEG216457\r\nEG216458\r\nEG216459\r\nEG216460
string[] s = egg.Split(stringSeparators, StringSplitOptions.None);
您将获得一个包含6个值的字符串数组。然后在textBox2 k值中输入5。
You will get a string array with 6 values. And then input 5 in textBox2 k value.
请在我的代码中用注释更改您的代码。
Please change your code with comments in my code.
最好的问候,
Wendy
这篇关于C#和SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!