存储过程不是从代码后面执行的

时间:2016-12-12 06:20:51

标签: c# asp.net sql-server stored-procedures

我正在尝试从后面的ASP.Net代码调用存储过程,但它无法正常工作。当我在SQL Server Management Studio中执行时,相同的SP运行正常。代码是:

SqlConnection con = new   SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
            con.Open();
            SqlCommand cmd = new SqlCommand("sp_utmerge", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@userId", Txt_Userid.Text);
            cmd.Parameters.AddWithValue("@username", Txt_Uname.Text);
            cmd.Parameters.AddWithValue("@designation", Txt_Design.Text);
            cmd.Parameters.AddWithValue("@dept", Txt_Dept.Text);
            cmd.Parameters.AddWithValue("@contact_no", Txt_Cntct.Text);
            cmd.Parameters.AddWithValue("@email", Txt_Email.Text);
            cmd.Parameters.AddWithValue("@address", Txt_Addrs.Text);
            cmd.Parameters.AddWithValue("@dob", Txt_DOB.Text);
            cmd.Parameters.AddWithValue("@password", Txt_Pwd.Text);
            cmd.ExecuteNonQuery();
            con.close();

存储过程是:

 USE [ABC]
 GO 
 /****** Object:  StoredProcedure [dbo].[sp_utmerge]    Script         
Date:12/12/2016 11:20:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[sp_utmerge]@userId nvarchar(10),@username   nvarchar(50),@designation nvarchar(50),@dept nvarchar(20),@contact_no nvarchar(20),@email nvarchar(MAX),@address nvarchar(100),@dob varchar(20),@password nvarchar(20)
AS
BEGIN
SET NOCOUNT ON;
merge Teachermast as target
using (select userId, username, designation, dept, contact_no, email,  address, dob, password from UserMast where usertype = 3) 
as source(userId, username, designation, dept, contact_no, email, address,  dob, password)
on target.teacher_code = source.userId
when not matched by target then
insert (teacher_code, teacher_name, designation, dept_code, contact_no, email, address, dob, password)
values (source.userId, source.username, source.designation, source.dept, source.contact_no, source.email, source.address, source.dob, source.password);

END

SSMS结果如下:

USE [ABC]
GO

DECLARE @return_value int

EXEC    @return_value = [dbo].[sp_utmerge]
        @userId = N'T0006',
        @username = N'abc',
        @designation = N'Teacher ',
        @dept = N'EIC',
        @contact_no = N'9632563256',
        @email = N'abc@yahoo.com',
        @address = N'97 xyz street',
        @dob = N'31/08/1990',
        @password = N'12345'

SELECT  'Return Value' = @return_value

GO

返回值0

这个问题的可能解决方案是什么?

0 个答案:

没有答案