将C#中的多个值插入SQL表中的一行

将C#中的多个值插入SQL表中的一行,c#,sql,visual-studio-2010,C#,Sql,Visual Studio 2010,我有一个表,我想把所有的输出存储在一行中,用空格隔开(例如“textbox1 textbox2 textbox3)。我该怎么做?目前它试图将它们放入不同的列中。给我一个错误,说明我使用的列更少 } using (SqlCommand cmd = new SqlCommand("INSERT INTO [user] (solution) VALUES (@textbox1, @textbox2, @textbox3)", cn))

我有一个表,我想把所有的输出存储在一行中,用空格隔开(例如“textbox1 textbox2 textbox3)。我该怎么做?目前它试图将它们放入不同的列中。给我一个错误,说明我使用的列更少

            }
            using (SqlCommand cmd = new SqlCommand("INSERT INTO [user] (solution) VALUES (@textbox1, @textbox2, @textbox3)", cn))
            {
                cmd.Parameters.AddWithValue("@textbox1", textBox1.Text);
                cmd.Parameters.AddWithValue("@textbox2", textBox2.Text);
                cmd.Parameters.AddWithValue("@textbox3", textBox3.Text);
                cmd.ExecuteNonQuery(); // or int affected = cmd.ExecuteNonQuery() 
                MessageBox.Show("Success!");

            }
        }
试试这个:

using (SqlCommand cmd = new SqlCommand("INSERT INTO [user] (solution) VALUES (@text)", cn))
            {
                cmd.Parameters.AddWithValue("@text", textBox1.Text + " " + textBox2.Text + " " + textBox3.Text);

                cmd.ExecuteNonQuery(); // or int affected = cmd.ExecuteNonQuery() 
                MessageBox.Show("Success!");

            }
试试这个:

using (SqlCommand cmd = new SqlCommand("INSERT INTO [user] (solution) VALUES (@text)", cn))
            {
                cmd.Parameters.AddWithValue("@text", textBox1.Text + " " + textBox2.Text + " " + textBox3.Text);

                cmd.ExecuteNonQuery(); // or int affected = cmd.ExecuteNonQuery() 
                MessageBox.Show("Success!");

            }

您在指定的列中插入的数据比您指定的多,因此我认为您在该部分中遇到了错误。因此,请在代码中进行一些修改以删除错误:

   }
            using (SqlCommand cmd = new SqlCommand("INSERT INTO [user] (solution) VALUES (@textbox1+' '+ @textbox2+' '+ @textbox3)", cn))
            {
                cmd.Parameters.AddWithValue("@textbox1", textBox1.Text);
                cmd.Parameters.AddWithValue("@textbox2", textBox2.Text);
                cmd.Parameters.AddWithValue("@textbox3", textBox3.Text);
                cmd.ExecuteNonQuery(); // or int affected = cmd.ExecuteNonQuery() 
                MessageBox.Show("Success!");

        }
    }

您在指定的列中插入的数据比您指定的多,因此我认为您在该部分中遇到了错误。因此,请在代码中进行一些修改以删除错误:

   }
            using (SqlCommand cmd = new SqlCommand("INSERT INTO [user] (solution) VALUES (@textbox1+' '+ @textbox2+' '+ @textbox3)", cn))
            {
                cmd.Parameters.AddWithValue("@textbox1", textBox1.Text);
                cmd.Parameters.AddWithValue("@textbox2", textBox2.Text);
                cmd.Parameters.AddWithValue("@textbox3", textBox3.Text);
                cmd.ExecuteNonQuery(); // or int affected = cmd.ExecuteNonQuery() 
                MessageBox.Show("Success!");

        }
    }

是“nt问题”[user](solution)”?是否检查了连接字符串?问题是在值集中使用逗号(,)。在SQL查询中,逗号(,)用于分隔变量(参数)要在查询中使用。因此,请将其删除,并在查询中使用之前尝试将文本框值合并。并在查询中使用连接的字符串值。一条建议:不要这样做!迟早,您将不得不再次解析该列,并将值从逗号分隔的列表中分离出来。数据库应该有一个单元格,one value-不要开始将多个值的逗号分隔列表放在一列中-这甚至违反了数据库设计的第一种正常形式。不要这样做-找到其他方法来存储此数据。将三个值存储在三个单独的列(或行)中有什么错?“nt问题”[用户](解决方案)?是否检查了连接字符串?问题是在值集中使用逗号(,)。在SQL查询中,逗号(,)用于分隔变量(参数)要在查询中使用。因此,请将其删除,并在查询中使用之前尝试将文本框值合并。并在查询中使用连接的字符串值。一条建议:不要这样做!迟早,您将不得不再次解析该列,并将值从逗号分隔的列表中分离出来。数据库应该有一个单元格,one value-不要开始将多个值的逗号分隔列表放在一列中-这甚至违反了数据库设计的第一种正常形式。只是不要这样做-找到其他方法来存储此数据。将三个值存储在三个单独的列(或行)中有什么错?有点讨厌做concat服务器端,IMOHO…我确实相信这会起作用,但是…所以+1有点讨厌做concat服务器端,IMOHO…我确实相信这会起作用,但是…所以+1