简体   繁体   English

我想使用 SQL Server 中的存储过程将数据从一个表同时插入到另一个表中

[英]I want to insert data from one table into another at same time using stored procedure in SQL server

First Issue table第一期表

BookID | BookName | DateIssue | ReturnDate | PersonID
       |          |           |            |
1      | Sqlserver|  4/4/2015 |  5/5/2015  |  22

I want to insert the values from tblIssue into Return table but, couldn't... second Return table我想将 tblIssue 中的值插入到返回表中,但是,不能...第二个返回表

BookID | BookName | DateIssue | ReturnDate | PersonID
       |          |           |            |
1      | Sqlserver|  4/4/2015 |  5/5/2015  |  22

query is running in sql server.. I thought, I have mistakes in sql server query查询正在 sql server 中运行.. 我想,我在 sql server 查询中有错误

Stored proc query:



'CREATE PROCEDURE inserttwo
(
@BookID int,
@BookName nvarchar(50),
@DateIssue datetime,
@ReturnDate datetime,
@PersonID int
)

as

insert into tblReturn(BookID,BookName,DateIssue,ReturnDate,PersonID)
values(@BookID,@BookName,@DateIssue,@ReturnDate,@PersonID)

select * from  tblIssue

error in c#: C#中的错误:

Procedure or function 'inserttwo' expects parameter '@BookID',过程或函数“inserttwo”需要参数“@BookID”,
which was not supplied.那是不供给的。

here it is pointing error: sda.Fill(dt);这里是指向错误:sda.Fill(dt);

C# code: 


 public void storedproc()
        {
      string w = ConfigurationManager.ConnectionStrings["LMS"].ConnectionString;
            SqlConnection conn = new SqlConnection(w);
            DataTable dt = new DataTable();
            SqlCommand cmd = new SqlCommand("inserttwo", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            conn.Open();
            sda.Fill(dt);
            metroGrid1.DataSource = dt;
            conn.Close(); }

you have to add the parameters你必须添加参数

string w = ConfigurationManager.ConnectionStrings["LMS"].ConnectionString;
using (SqlConnection conn = new SqlConnection(w))
{
    DataTable dt = new DataTable();
    using (SqlCommand cmd = new SqlCommand("inserttwo", conn))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@BookID", SqlDbType.Int).Value = BookID;
        cmd.Parameters.Add("@BookName", SqlDbType.NVarChar, 50).Value = BookName;
        cmd.Parameters.Add("@DateIssue", SqlDbType.DateTime).Value = Date;
        cmd.Parameters.Add("@ReturnDate", SqlDbType.DateTime).Value = ReturnDate;
        cmd.Parameters.Add("@PersonID", SqlDbType.Int).Value = PersonID;
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        conn.Open();
        sda.Fill(dt);
        metroGrid1.DataSource = dt;
    }
    conn.Close();
}

the procedure requires @BookID,@BookName,@DateIssue,@ReturnDate,@PersonID so you have to add them and fill them with values.该过程需要@BookID,@BookName,@DateIssue,@ReturnDate,@PersonID因此您必须添加它们并用值填充它们。

UPDATE To insert Data into 2 tables your procedure should look something like this without knowing your table structure it's impossible to give you a valid answer更新要将数据插入到 2 个表中,您的过程应该看起来像这样,而无需知道您的表结构,就不可能给您一个有效的答案

'CREATE PROCEDURE inserttwo
(
@BookID int,
@BookName nvarchar(50),
@DateIssue datetime,
@ReturnDate datetime,
@PersonID int
)

as

insert into tblReturn(BookID,BookName,DateIssue,ReturnDate,PersonID)
values(@BookID,@BookName,@DateIssue,@ReturnDate,@PersonID)

insert into tblIssue(BookID,BookName,DateIssue,ReturnDate,PersonID)
values(@BookID,@BookName,@DateIssue,@ReturnDate,@PersonID)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM