1
我輸入和更新兩個表中的數據,使用SQl服務器中的SP工作創建查詢都可以,但要在C#中的窗體上諮詢我會生成一個錯誤。存儲過程在SQL管理工作室中運行,但不在代碼中
這裏是我的表定義:
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 [email protected])
begin
insert into nationality(nationality)values(@nationality)
end
declare @id int
select @id=nationalitycode from nationality where [email protected]
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 [email protected])
begin
insert into nationality(nationality)values(@nationality)
end
update user
SET name= @name,[email protected],nationalitycode=(select nationalitycode from nationality where [email protected])
where code = @code
出色的響應,現在我有另一個小問題 來更新表格上的表格,只顯示在文本字段中。 在外地代碼 記錄的數字我可以刪除代碼數字和更新記錄..任何想法來解決它? – Andreu
@Andreu:你的後續問題看起來超出了原始問題的範圍 - 你可能想要把它作爲一個單獨的問題。 (您可能還想重新翻譯,因爲我不知道您在問什麼;)請記住在問題中將問題標記爲「已接受」(Accepted)。 –
好吧,沒問題..你是個天才 – Andreu