首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将数据插入(第30列,第1行)

问将数据插入(第30列,第1行)
EN

Stack Overflow用户
提问于 2020-12-26 16:40:25
回答 3查看 90关注 0票数 0
代码语言:javascript
复制
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行,其中有许多空值

如下所示:

my table after run this code

我想把所有35列的值放到一行中。

EN

回答 3

Stack Overflow用户

发布于 2020-12-26 18:12:32

代码语言:javascript
复制
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();
            }
票数 0
EN

Stack Overflow用户

发布于 2020-12-26 19:00:38

代码语言:javascript
复制
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();
票数 -1
EN

Stack Overflow用户

发布于 2020-12-26 19:13:57

代码语言:javascript
复制
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();
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65454789

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档