在一个查询中插入并选择

时间:2010-03-03 16:12:41

标签: c# sql

我有一个查询

 insert into carsdescription
 (description,idCar) 
 value (@description,@idCar)
 where idCar=@idCar;
 select nameOfCar
 from Cars 
 where idCar=@idCar";

如何在一个sqlCommand中执行这个??

using(SqlConnection conn=new SqlConnection(connString))
{
    using(sqlCommand cmd=new SqlCommand())
    {
        cmd.Parameteres, conn etc... //this is a peanut

        // your proposition    
    }
}

表架构:

Carsdescription:

 ID (PK, int, autoincrement)
idcar(FK, int)
Description(varchar)

汽车

Id(int, PK, autoincrement)
Name(nvarchar(255)

7 个答案:

答案 0 :(得分:3)

您是否尝试将数据从一个表复制到另一个表?

这样的东西
INSERT INTO CarsDescription (description)
    SELECT nameOfCar
        FROM Cars
        WHERE idCar = @idCar

这会将所有'nameOfCar'值复制到CarsDescription表中。如果您不想重复,请将 SELECT 更改为 SELECT DISTINCT

答案 1 :(得分:2)

insert into carsdescription (description) value (@description) where idCar=@idCar;

INSERT ... WHERE ??

(不是我知道你真正要做的是什么,但该查询首先不起作用)

答案 2 :(得分:2)

你做不到。不在一个声明中。你为什么甚至想要?你的想法是什么,或者你的要求是什么?

首先必须使用cmd.ExecuteNonQuery调用来插入新数据,然后需要使用cmd.ExecuteReader进行第二次调用,或者使用SqlDataAdapter填充DataTable以再次检索数据。

using(SqlConnection conn = new SqlConnection(connString))
{
    conn.Open();

    string cmdInsert = "insert into carsdescription(description) value (@description)";

    using(sqlCommand cmd = new SqlCommand(cmdInsert, conn))
    {
        cmd.Parameters.AddWithValue("@description", description);
        cmd.ExecuteNonQuery();
    }

    string selectStmt = "select nameOfCar from dbo.Cars where idCar = @idCar";

    using(sqlCommand cmd2 = new SqlCommand(selectStmt, conn))
    {
        cmd2.Parameters.AddWithValue("@idCar", idCar);

        string resultValue = cmd2.ExecuteScalar().ToString();
    }

    conn.Close();
}

答案 3 :(得分:2)

创建一个存储过程,将Description和IDCar作为参数,并返回标量NameOfCar或Cars表中的结果集。然后,您可以使用C#代码中的单个命令调用该存储过程。

答案 4 :(得分:2)

这在单个呼叫中执行完全有效。可能不推荐,但它是可能的。

using (SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=JunkBox;Integrated Security=SSPI;"))
            {
                SqlCommand cmd = new SqlCommand("INSERT INTO JunkSO(Id, Name) VALUES(@Id, @Description)  SELECT * FROM JunkSO", conn);
                cmd.Parameters.AddWithValue("@Id", 10);
                cmd.Parameters.AddWithValue("@Description", "TestDescription");
                conn.Open();
                using (SqlDataReader rd = cmd.ExecuteReader())
                {
                    if (rd.HasRows)
                    {
                        while (rd.Read())
                        {
                            MessageBox.Show(rd[0].ToString() + "  " + rd[1].ToString());
                        }
                    }
                }
            }

答案 5 :(得分:0)

    using (SqlCommand cmd = conn.CreateCommand())
    {
        cmd.CommandText = your_query;
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.Add(new SqlParameter("@description", description));
        cmd.Parameters.Add(new SqlParameter("@idCar", idCar));
        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            //reader here
        }
    }

答案 6 :(得分:0)

嗯,我也找到了一种方法,我知道它很乱,但我也是初学者,所以我自己做了如下。

SqlConnection add = new SqlConnection(ConfigurationManager.ConnectionStrings["sismanager"].ConnectionString);
        SqlCommand additem = new SqlCommand("INSERT INTO Godown(Gname, Gstock) VALUES(@Gname, @Gstock)", add);
        additem.Parameters.AddWithValue("@Gname", textBox1.Text.Trim());
        additem.Parameters.AddWithValue("@Gstock", textBox2.Text.Trim());
        try
        {               
            add.Open();
            additem.ExecuteNonQuery();              
        }
        catch
        {

        }
        finally
        {
            add.Close();

            dataGridView1.Columns.Clear();
            SqlConnection setcon = new SqlConnection(ConfigurationManager.ConnectionStrings["sismanager"].ConnectionString);
            using (SqlCommand getdata = new SqlCommand("SELECT Gid, Gname, Gstock FROM Godown ORDER BY Gname ASC", setcon))
            {
                setcon.Open();
                SqlDataAdapter da = new SqlDataAdapter(getdata);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    comboBox1.DisplayMember = "Gname";
                    comboBox1.ValueMember = "Gid";
                    comboBox1.DataSource = dt;

                    dataGridView1.DataSource = dt;
                    dataGridView1.Columns[0].HeaderText = "Item Id";
                    dataGridView1.Columns[1].HeaderText = "Item";
                    dataGridView1.Columns[2].HeaderText = "Item Stock";

                    DataGridViewButtonColumn deletebtn = new DataGridViewButtonColumn();
                    dataGridView1.Columns.Add(deletebtn);
                    deletebtn.HeaderText = "Remove";
                    deletebtn.Text = "Remove";
                    deletebtn.Name = "Dbtn";
                    deletebtn.UseColumnTextForButtonValue = true;
                }
                else
                {
                    MessageBox.Show("No Stock Items to Show", "Stock Details", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }    

然而,这个答案与你的问题无关,但我已经尝试了自己的解决方案。