我正在输入和更新两个表中的数据,在SQl服务器中使用SP工作创建查询是可以的,但是在C#中查询表单我会生成错误。
以下是我的表格定义:
CREATE TABLE [dbo].[nationality](
[nationalitycode] [int] IDENTITY(1,1) NOT NULL,
[nationality] [nvarchar](50) NULL,
CONSTRAINT [PK_nationality] PRIMARY KEY CLUSTERED
(
[nationalitycode] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[user]
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-------------------------------------------------------------------------------------------
CREATE TABLE [dbo].[user](
[code] [int] IDENTITY(1,1) NOT NULL,
[name] [nvarchar](50) NULL,
[lastname] [nvarchar](50) NULL,
[nationalitycode] [int] NULL,
CONSTRAINT [PK_user] PRIMARY KEY CLUSTERED
(
[code] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
当我运行以下代码编辑数据时,我得到:
Sqlexception未处理过程或函数'sp_update'需要 参数'@lastName',未提供。
private void btnRec_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("", cn);
cmd.CommandType = CommandType.StoredProcedure;
cn.Open();
int fila = 0;
switch (menup)
{
case menu.New:
cmd.CommandText = "sp_insertdata";
cmd.Parameters.AddWithValue("code", 0);
cmd.Parameters.AddWithValue("name", txtName.Text);
cmd.Parameters.AddWithValue("lastName", txtlastName.Text);
cmd.Parameters.AddWithValue("nationality", txtNationality.Text);
cmd.Parameters["code"].Direction = ParameterDirection.Output;
fila = cmd.ExecuteNonQuery();
if (fila!=0)
{
MessageBox.Show("Good Registry" + cmd.Parameters["code"].Value.ToString());
}
break;
这是我的编辑代码:
case menu.Edit:
cmd.CommandText = "sp_update";
cmd.Parameters.AddWithValue("code", txtCode.Text);
cmd.Parameters.AddWithValue("name", txtName.Text);
cmd.Parameters.AddWithValue("lastname", txtLastName.Text);
cmd.Parameters.AddWithValue("nationality", txtNationality.Text);
fila = cmd.ExecuteNonQuery();
if (fila!=0)
{
MessageBox.Show("Update ok" + cmd.Parameters["code"].Value.ToString());
}
break;
当我运行编辑代码时,我得到:
Sqlexception未处理过程或函数'sp_update'需要 参数'@lastName',未提供。
以下是存储过程定义:
ALTER proc [dbo].[sp_insertdata]
@name nvarchar(50),
@lastname nvarchar(50),
@nationality nvarchar(50)
as
if not exists(select * from nationality where nationality=@nationality)
begin
insert into nationality(nationality)values(@nationality)
end
declare @id int
select @id=nationalitycode from nationality where nationality=@nationality
insert into usuario(name,lastname,nationalitycode) values (@name,@lastname,@id)
/ * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * * /
ALTER proc [dbo].[sp_update]
@code int,
@name varchar(50),
@lastname varchar(50),
@nationality varchar(50)
as
if not exists (select * from nationality where nationality=@nationality)
begin
insert into nationality(nationality)values(@nationality)
end
update user
SET name= @name,lastname=@lastname,nationalitycode=(select nationalitycode from nationality where nationality=@nationality)
where code = @code
答案 0 :(得分:0)
您在插入存储过程和插入代码中遇到问题。存储过程没有代码参数,但您将其作为输入传递。您的代码应为:
case menu.New:
cmd.CommandText = "sp_insertdata";
cmd.Parameters.AddWithValue("@name", txtName.Text);
cmd.Parameters.AddWithValue("@lastName", txtlastName.Text);
cmd.Parameters.AddWithValue("@nationality", txtNationality.Text);
cmd.Parameters["code"].Direction = ParameterDirection.Output;
fila = cmd.ExecuteNonQuery();
Insert存储过程应包含代码作为输出:
ALTER proc [dbo].[sp_insertdata]
@name nvarchar(50),
@lastname nvarchar(50),
@nationality nvarchar(50),
@code int output
as
if not exists(select * from nationality where nationality=@nationality)
begin
insert into nationality(nationality)values(@nationality)
end
declare @id int
select @id=nationalitycode from nationality where nationality=@nationality
insert into usuario(name,lastname,nationalitycode) values (@name,@lastname,@id)
SET @code=SCOPE_IDENTITY()
对于您的更新代码,我将更改为:
cmd.CommandText = "sp_update";
cmd.Parameters.AddWithValue("@code", txtCode.Text);
cmd.Parameters.AddWithValue("@name", txtName.Text);
cmd.Parameters.AddWithValue("@lastname", txtLastName.Text);
cmd.Parameters.AddWithValue("@nationality", txtNationality.Text);