如何解决将数据插入第 30 列,第 1 行
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 列值放在一行中。
解决方法
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();
}
,
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
string[] col = {
"name","HBS"
};
Control[] val = {
textBox1,txt36
};
for (int x = 0; x < col.Length;x++)
{
cmd.CommandText += "INSERT INTO [Table] (" + col[x] +") VALUES('" + val[x].Text +"')";
cmd.ExecuteNonQuery();
}
con.Close();
,
string[] col = {
"name","HBS"
};
Control[] val = {
textBox1,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();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。