2015-09-23 113 views
-2

第一期表在同一時間從一個表中的數據插入到另一個我想使用SQL Server存儲過程

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

我想從tblIssue值插入到返回表,但是,不能.. 。 第二回表

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

查詢在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 

誤差在c#:

Procedure or function 'inserttwo' expects parameter '@BookID',
which was not supplied.

這裏它指向錯誤: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(); } 

回答

3

你必須添加參數

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(); 
} 

的過程需要@BookID,@BookName,@DateIssue,@ReturnDate,@PersonID所以你必須添加它們,並與值填充它們。

UPDATE將數據插入到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) 
+0

它在給出的BookId在當前上下文中不存在錯誤後給出一個錯誤。 – Warda

+0

@Warda是的,你必須設置你想插入的書的價值 – fubo

+0

好吧告訴我...是否正確查詢插入兩個表一次? – Warda