con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
string[] col = {
"name","dr", "WBC", "Lympho", "Mono", "Gran", "RBC",
"Hb", "PCV", "Platelet", "ESR","BloodGroupRh",
"Striglycride","SChol","SHDL","SLDL","BloodUrea",
"SCreatenin","SUA","SCa","RBSugar","HBA1c",
"SGOT","SGPT","ALP","TSB","SerumBhCG","Myoglobin",
"CK","Trobonin","CRP","AntiCCP","STyphoied IgG",
"STyphoied IgM", "RoseBengal","me","HCV","HBS"
};
Control[] val = {
textBox1, comboBox1, txt1, txt2, txt3, txt4, txt5, txt6,
txt7, txt8, txt9, txt10, txt11, txt12, txt13, txt14, txt15,
txt16, txt17, txt18, txt19, txt20, txt21, txt22, txt23, txt24,
txt25, txt26, txt27, txt28, txt29, txt30, txt31, txt32, txt33,
txt34, txt35, txt36
};
for (int x = 0; x < col.Length;x++)
{
cmd.CommandText += "INSERT INTO [Table] (" + col[x] +") VALUES('" + val[x].Text +"')";
cmd.ExecuteNonQuery();
}
con.Close();作为显示我大约有35列的代码,我试图使用for循环插入它,但是当我运行这段代码时,他在单元格中插入了35行,其中有许多空值
如下所示:
我想把所有35列的值放到一行中。
发布于 2020-12-26 18:12:32
SqlConnection con = new SqlConnection(yourConnectionString);
using (con)
{
con.Open();
string[] col = {
"name","dr", "WBC", "Lympho", "Mono", "Gran", "RBC",
"Hb", "PCV", "Platelet", "ESR","BloodGroupRh",
"Striglycride","SChol","SHDL","SLDL","BloodUrea",
"SCreatenin","SUA","SCa","RBSugar","HBA1c",
"SGOT","SGPT","ALP","TSB","SerumBhCG","Myoglobin",
"CK","Trobonin","CRP","AntiCCP","STyphoied IgG",
"STyphoied IgM", "RoseBengal","me","HCV","HBS"
};
Control[] val = {
textBox1, comboBox1, txt1, txt2, txt3, txt4, txt5, txt6,
txt7, txt8, txt9, txt10, txt11, txt12, txt13, txt14, txt15,
txt16, txt17, txt18, txt19, txt20, txt21, txt22, txt23, txt24,
txt25, txt26, txt27, txt28, txt29, txt30, txt31, txt32, txt33,
txt34, txt35, txt36
};
string col_query = null;
string columns_in_query = null;
for (int x = 0; x < col.Length; x++)
{
col_query += x < col.Length - 1 ? +"@" + col[x] + ","+ :+"@" + col[x];
columns_in_query += x < col.Length - 1 ? col[x] +","+ : col[x];
}
SqlCommand cmd = new SqlCommand("Insert into Table(" + columns_in_query + ") values(" + col_query + ")", con);
for (int x = 0; x < col.Length; x++)
{
string value="";
if(val[x] is TextBox){
value=((TextBox)val[x]).Text.Trim();
}
if(val[x] is ComboBox){
value=((ComboBox)val[x]).SelectedItem.ToStrin().Trim();
}
cmd.Parameters.AddWithValue("@" + col[x], value);
}
cmd.ExecuteNonQuery();
}发布于 2020-12-26 19:00:38
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
string[] col = {
"name","dr", "WBC", "Lympho", "Mono", "Gran", "RBC",
"Hb", "PCV", "Platelet", "ESR","BloodGroupRh",
"Striglycride","SChol","SHDL","SLDL","BloodUrea",
"SCreatenin","SUA","SCa","RBSugar","HBA1c",
"SGOT","SGPT","ALP","TSB","SerumBhCG","Myoglobin",
"CK","Trobonin","CRP","AntiCCP","STyphoied IgG",
"STyphoied IgM", "RoseBengal","me","HCV","HBS"
};
Control[] val = {
textBox1, comboBox1, txt1, txt2, txt3, txt4, txt5, txt6,
txt7, txt8, txt9, txt10, txt11, txt12, txt13, txt14, txt15,
txt16, txt17, txt18, txt19, txt20, txt21, txt22, txt23, txt24,
txt25, txt26, txt27, txt28, txt29, txt30, txt31, txt32, txt33,
txt34, txt35, txt36
};
for (int x = 0; x < col.Length;x++)
{
cmd.CommandText += "INSERT INTO [Table] (" + col[x] +") VALUES('" + val[x].Text +"')";
cmd.ExecuteNonQuery();
}
con.Close();发布于 2020-12-26 19:13:57
string[] col = {
"name", "dr", "WBC", "Lympho", "Mono", "Gran", "RBC",
"Hb", "PCV", "Platelet", "ESR", "BloodGroupRh",
"Striglycride", "SChol", "SHDL", "SLDL", "BloodUrea",
"SCreatenin", "SUA", "SCa", "RBSugar", "HBA1c",
"SGOT", "SGPT", "ALP", "TSB", "SerumBhCG", "Myoglobin",
"CK", "Trobonin", "CRP", "AntiCCP", "STyphoied IgG",
"STyphoied IgM", "RoseBengal", "me", "HCV", "HBS"
};
Control[] val = {
textBox1, comboBox1, txt1, txt2, txt3, txt4, txt5, txt6,
txt7, txt8, txt9, txt10, txt11, txt12, txt13, txt14, txt15,
txt16, txt17, txt18, txt19, txt20, txt21, txt22, txt23, txt24,
txt25, txt26, txt27, txt28, txt29, txt30, txt31, txt32, txt33,
txt34, txt35, txt36
};
List < string > values = new List<string>();
foreach(Control c in val)
{
if (c.GetType().Name == "TextBox")
values.Add(((TextBox)c).Text);
else if (c.GetType().Name == "ComboBox")
values.Add(((ComboBox)c).Text); // or get value whatever you want
}
cmd.CommandText += "INSERT INTO [Table] (" + string.Join(",", col) + ") VALUES('" + string.Join("','", values.ToArray()) + "')";
cmd.ExecuteNonQuery();https://stackoverflow.com/questions/65454789
复制相似问题