我想从一个表插入数据到另一个在同一时间使用存储过程在SQL服务器
本文关键字:同一时间 另一个 服务器 SQL 存储过程 数据 插入 一个 | 更新日期: 2023-09-27 18:14:02
首发表
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#中的错误:
过程或函数'inserttwo'期望参数'@BookID',
指向错误: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(); }
你必须添加参数
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)
传递参数命令对象。https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters (v = vs.110) . aspx