1

可以同时向两个表中插入数据吗?在我当前的页面中,只有一个命令只在一张表中插入数据,CarTab。

 SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
    SqlCommand cmd = new SqlCommand("Insert into CarTab(Manufacture,Model,Plate,Color,Year,Service,Roadtax) Values (@manufactures,@models,@plates,@colors,@years,@services,@roadtaxs)", conn);

    cmd.CommandType = CommandType.Text;
    cmd.Parameters.AddWithValue("@manufactures", Label1.Text);
    cmd.Parameters.AddWithValue("@models", Label2.Text);
    cmd.Parameters.AddWithValue("@plates", Label3.Text);
    cmd.Parameters.AddWithValue("@colors", Label4.Text);
    cmd.Parameters.AddWithValue("@years", Label5.Text);
    cmd.Parameters.AddWithValue("@services", Label6.Text);
    cmd.Parameters.AddWithValue("@roadtaxs", Label7.Text);


    conn.Open();
    cmd.ExecuteNonQuery();

如果我想放另一个表,TempTab 是什么命令?谢谢。

4

4 回答 4

2

You can write 2 insert queries inside text you passing to SQL command or you can write a store procedure and call it instead.

于 2012-09-11T05:36:01.740 回答
0

You repeat the same code as the one shown in your question but modify the query and parameters for the second table. Also I would recommend you wrapping IDisposable resources such as database connections and commands in a using statement.

于 2012-09-11T05:36:03.033 回答
0

Multi-table inserts are an extension to INSERT..SELECT. Syntax is of the following form:

INSERT ALL|FIRST
[WHEN condition THEN] INTO target [VALUES]
[WHEN condition THEN] INTO target [VALUES]
...
[ELSE] INTO target [VALUES]
SELECT ...
FROM   source_query;

Source: http://www.oracle-developer.net/display.php?id=209

于 2012-09-11T05:36:47.540 回答
0

There are many possible solution to your answer. First, I would suggest that you create a TRIGGER on every insert on table CarTab.

Second, why not change the CommandText of your command object and call ExecuteNonQuery() again?

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString))
{
    string QueryA = "Insert into CarTab(Manufacture,Model,Plate,Color,Year,Service,Roadtax) Values (@manufactures,@models,@plates,@colors,@years,@services,@roadtaxs";
    string QueryB = "Insert into TempTab(Manufacture,Model,Plate,Color,Year,Service,Roadtax) Values (@manufactures,@models,@plates,@colors,@years,@services,@roadtaxs";

    using (SqlCommand cmd = new SqlCommand("", conn))
    {
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = QueryA;
        cmd.Parameters.AddWithValue("@manufactures", Label1.Text);
        cmd.Parameters.AddWithValue("@models", Label2.Text);
        cmd.Parameters.AddWithValue("@plates", Label3.Text);
        cmd.Parameters.AddWithValue("@colors", Label4.Text);
        cmd.Parameters.AddWithValue("@years", Label5.Text);
        cmd.Parameters.AddWithValue("@services", Label6.Text);
        cmd.Parameters.AddWithValue("@roadtaxs", Label7.Text);

        try
        {
            conn.Open()
            cmd.ExecuteNonQuery();

            cmd.CommandText = QueryB;
            cmd.ExecuteNonQuery();
        }
        catch (SqlException e)
        {
            // put error here
        }
        finally
        {
            conn.Close();
        }
    }
}

and Lastly create a Stored Procedure

CREATE PROCEDURE InsertRecord
@manufactures varchar(50),
@models varchar(50),
@plates varchar(50),
@colors varchar(50),
@years varchar(50),
@services varchar(50),
@roadtaxs varchar(50)
AS
BEGIN
        Insert into CarTab(Manufacture,Model,Plate,Color,Year,Service,Roadtax) 
        Values (@manufactures,@models,@plates,@colors,@years,@services,@roadtaxs)

        Insert into CarTab(Manufacture,Model,Plate,Color,Year,Service,Roadtax) 
        Values (@manufactures,@models,@plates,@colors,@years,@services,@roadtaxs)
END
GO

and in your C# code,

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString))
{

    using (SqlCommand cmd = new SqlCommand("InsertRecord", conn))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = QueryA;
        cmd.Parameters.AddWithValue("@manufactures", Label1.Text);
        cmd.Parameters.AddWithValue("@models", Label2.Text);
        cmd.Parameters.AddWithValue("@plates", Label3.Text);
        cmd.Parameters.AddWithValue("@colors", Label4.Text);
        cmd.Parameters.AddWithValue("@years", Label5.Text);
        cmd.Parameters.AddWithValue("@services", Label6.Text);
        cmd.Parameters.AddWithValue("@roadtaxs", Label7.Text);

        try
        {
            conn.Open()
            cmd.ExecuteNonQuery();
        }
        catch (SqlException e)
        {
            // put error here
        }
        finally
        {
            conn.Close();
        }
    }
}
于 2012-09-11T05:37:50.320 回答