我有三个表:
我正在尝试将记录插入dbo.RequestLine
表中。但是,我收到一条错误消息:
System.Data.SqlClient.SqlException:由于“ RequestLine”是表对象,因此对过程“ RequestLine”的请求失败。
这是三个表的DDL语句:
CREATE TABLE [dbo].[Request]
(
[REQ_ID] [int] IDENTITY(10000005,1) NOT NULL,
[REQ_STAT] [varchar](12) NULL,
[REQ_CAT] [varchar](15) NULL,
[ADMIN_ID] [smallint] NULL,
[COOR_ID] [smallint] NULL,
CONSTRAINT [PK_Request]
PRIMARY KEY CLUSTERED ([REQ_ID] ASC)
)
ALTER TABLE [dbo].[Request] WITH CHECK
ADD CONSTRAINT [FK_Request_Admin]
FOREIGN KEY([ADMIN_ID]) REFERENCES [dbo].[Admin] ([ADMIN_ID])
GO
ALTER TABLE [dbo].[Request] CHECK CONSTRAINT [FK_Request_Admin]
GO
ALTER TABLE [dbo].[Request] WITH CHECK
ADD CONSTRAINT [FK_Request_Coordinator]
FOREIGN KEY([COOR_ID]) REFERENCES [dbo].[Coordinator] ([COOR_ID])
GO
ALTER TABLE [dbo].[Request] CHECK CONSTRAINT [FK_Request_Coordinator]
GO
CREATE TABLE [dbo].[Student]
(
[STUD_ID] [int] IDENTITY(1000009,1) NOT NULL,
[STUD_NAM] [varchar](30) NOT NULL,
[STUD_EMAIL] [varchar](50) NULL,
[CAMP_NAM] [varchar](50) NULL,
[CAMP_ZIP] [int] NULL,
[STUD_TYP] [char](5) NOT NULL,
CONSTRAINT [PK_Student]
PRIMARY KEY CLUSTERED ([STUD_ID] ASC)
)
GO
ALTER TABLE [dbo].[Student] WITH CHECK
ADD CONSTRAINT [FK_Student_Campus]
FOREIGN KEY([CAMP_NAM], [CAMP_ZIP]) REFERENCES [dbo].[Campus] ([CAMP_NAM], [CAMP_ZIP])
GO
ALTER TABLE [dbo].[Student] CHECK CONSTRAINT [FK_Student_Campus]
GO
CREATE TABLE [dbo].[RequestLine]
(
[STUD_ID] [int] NOT NULL,
[REQ_ID] [int] NOT NULL,
[REQ_DATE] [date] NOT NULL,
CONSTRAINT [PK_RequestLine]
PRIMARY KEY CLUSTERED ([STUD_ID] ASC, [REQ_ID] ASC)
)
GO
ALTER TABLE [dbo].[RequestLine] WITH CHECK
ADD CONSTRAINT [FK_RequestLine_Request]
FOREIGN KEY([REQ_ID]) REFERENCES [dbo].[Request] ([REQ_ID])
GO
ALTER TABLE [dbo].[RequestLine] CHECK CONSTRAINT [FK_RequestLine_Request]
GO
ALTER TABLE [dbo].[RequestLine] WITH CHECK
ADD CONSTRAINT [FK_RequestLine_Student]
FOREIGN KEY([STUD_ID]) REFERENCES [dbo].[Student] ([STUD_ID])
GO
ALTER TABLE [dbo].[RequestLine] CHECK CONSTRAINT [FK_RequestLine_Student]
GO
这是我拥有的三个存储过程:
ALTER PROCEDURE [dbo].[Req_InsertNew]
(@REQ_STAT varchar(12),
@REQ_CAT varchar(15),
@ADMIN_ID smallint,
@COOR_ID smallint)
AS
BEGIN
SET NOCOUNT ON
INSERT INTO dbo.Request (REQ_STAT, REQ_CAT, ADMIN_ID, COOR_ID)
VALUES (@REQ_STAT, @REQ_CAT, @ADMIN_ID, @COOR_ID);
DECLARE @REQ_ID INT
SET @REQ_ID = SCOPE_IDENTITY()
SELECT @REQ_ID
END
ALTER PROCEDURE [dbo].[Stud_InsertNew2]
(@STUD_NAM varchar(30),
@STUD_EMAIL varchar(50),
@CAMP_NAM varchar(50),
@CAMP_ZIP int,
@STUD_TYP char(5))
AS
BEGIN
DECLARE @temptable TABLE (newSTUDID int, STUDTYPE char);
SET NOCOUNT ON
INSERT INTO dbo.Student(STUD_NAM, STUD_EMAIL, CAMP_NAM, CAMP_ZIP, STUD_TYP)
VALUES (@STUD_NAM, @STUD_EMAIL, @CAMP_NAM, @CAMP_ZIP, @STUD_TYP);
DECLARE @STUD_ID INT
SET @STUD_ID = SCOPE_IDENTITY()
SELECT @STUD_ID
END
ALTER PROCEDURE [dbo].[ReqLine_Insert]
(@STUD_ID int,
@REQ_ID int,
@REQ_DATE date)
AS
BEGIN
INSERT INTO dbo.RequestLine (STUD_ID, REQ_ID, REQ_DATE)
VALUES (@STUD_ID, @REQ_ID, @REQ_DATE)
END
我正在尝试使用ExecuteScalar()
来检索REQ_ID
和STUD_ID
并将它们放入RequestLine
表中。
这是C#代码:
int campzip = int.Parse(ddlCamp.SelectedValue.Trim());
int adminid = int.Parse(ddlAdmin.SelectedValue.Trim());
int coorid = int.Parse(txtCoorID.Text.Trim());
DateTime ReqDate = DateTime.ParseExact(txtReqDate.Text, "yyyy-MM-dd", null);
using (var connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("cis-laredoConnectionString")))
{
connection.Open();
var cmd = new SqlCommand("dbo.Stud_InsertNew2", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@STUD_NAM", txtStdName.Text);
cmd.Parameters.AddWithValue("@STUD_EMAIL", txtStdEmail.Text);
cmd.Parameters.AddWithValue("@CAMP_NAM", ddlCamp.SelectedItem.ToString());
cmd.Parameters.AddWithValue("@CAMP_ZIP", campzip);
cmd.Parameters.AddWithValue("@STUD_TYP", "Other");
int STUD_ID = (int)cmd.ExecuteScalar();
cmd.Dispose();
connection.Close();
connection.Open();
var cmd1 = new SqlCommand("dbo.Req_InsertNew", connection);
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.AddWithValue("@REQ_STAT", "Submitted");
cmd1.Parameters.AddWithValue("@REQ_CAT", "Uniform");
cmd1.Parameters.AddWithValue("@ADMIN_ID", adminid);
cmd1.Parameters.AddWithValue("@COOR_ID", coorid);;
int REQ_ID = (int)cmd1.ExecuteScalar();
cmd1.Dispose();
connection.Close();
// post back info. to dbo.RequestLine table in sql server
connection.Open();
var cmd2 = new SqlCommand("dbo.ReqLine_Insert", connection);
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.Parameters.AddWithValue("@STUD_ID", STUD_ID);
cmd2.Parameters.AddWithValue("@REQ_ID", REQ_ID);
cmd2.Parameters.Add("@REQ_DATE", SqlDbType.Date).Value = ReqDate;
cmd2.ExecuteNonQuery();
cmd2.Dispose();
connection.Close();
}
任何帮助将不胜感激!
答案 0 :(得分:2)
您应该更改此设置:
var cmd2 = new SqlCommand("dbo.RequestLine", connection);
与此:
var cmd2 = new SqlCommand("dbo.ReqLine_Insert", connection);
对象dbo.RequestLine
是一个表,而您需要存储过程dbo.ReqLine_Insert
才能将数据插入到该表中。