我在两个表中输入和更新数据,在 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