2014-12-02 2 views
0

업데이트에 몇 가지 문제가 있습니다. 맞춤 선택 기능을 만들어야했기 때문에 동적 쿼리를 만들 수 없었습니다. 나는 아래의 코드를 사용하여 내 데이터베이스를 업데이트하기 위해 노력했지만, 난 항상이 오류를 얻을 : 나는 모든 인수, 더 이상, 더 적은은 무슨 일 없습니다 ....를 작성했습니다프로 시저에 지정된 인수가 너무 많습니다.

Procedure or function UpdateUser has too many arguments specified.

를?

ASPX 마크 업 : # 코드 숨김

<asp:SqlDataSource ID="AllUsers" runat="server" 
    ConnectionString="<%$ ConnectionStrings:LocalSQL %>" 
    SelectCommand="TodosOsUtilizadores" SelectCommandType="StoredProcedure"> 
</asp:SqlDataSource> 

C :

using (SqlDataSource ds = AllUsers) 
{ 
    Guid guid = new Guid(); 

    try 
    { 
     guid = Guid.Parse(Session["user_id"].ToString()); 
    } 
    catch 
    { 
     guid = Guid.NewGuid(); 
    } 

    ds.UpdateCommand = "UpdateUser"; 
    ds.ConflictDetection = ConflictOptions.OverwriteChanges; 
    ds.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure; 
    ds.UpdateParameters.Add("userid", guid.ToString()); 
    ds.UpdateParameters.Add("username", "Diogo"); 
    ds.UpdateParameters.Add("email", "[email protected]"); 
    ds.UpdateParameters.Add("isAnonimo", "0"); 
    ds.UpdateParameters.Add("isLocked", "0"); 
    ds.UpdateParameters.Add("roleId", ""); 

    ds.Update(); 
    DetailsView1.DataBind(); 
} 

저장 프로 시저가 :

CREATE PROCEDURE [dbo].UpdateUser 
    @userid nvarchar(max), 
    @username nvarchar(200), 
    @email nvarchar(500), 
    @isAnonimo bit, 
    @isLocked bit, 
    @roleid nvarchar(max) 
AS 
    UPDATE Memberships 
    SET Email = @email, 
     IsLockedOut = @isLocked 
    WHERE 
     UserId = CAST(@userid as uniqueidentifier); 

    UPDATE Users 
    SET UserName = @username, 
     IsAnonymous = @isAnonimo 
    WHERE 
     UserId = CAST(@userid AS uniqueidentifier); 

    IF (@roleid IS NULL) 
    BEGIN 
     UPDATE UsersInRoles 
     SET RoleId = CAST(roleid AS uniqueidentifier); 
    END; 
+2

는'@'를 포함하여 매개 변수를 지정하십시오? 예 : – Blorgbeard

+0

[낭비 가이드] (http://wasteaguid.info/) - 'Guid guid = Guid.Empty();' – asawyer

+0

검색 방법을 찾으십시오. 'Parameter.Add() 함수 대 Parameter.AddWithValue() 함수'를 사용하여 당신이 그것을 사용하는 방식으로'DataType'을 지정해야합니다. 데이터베이스가 처리 할 수있는 방식으로'AddWithValue'에'Add'를 변경합니다 – MethodMan

답변

0

문제는 어떻게 SqlDataSource 작품, 특히 ConflictDetection 함께.

MSDN says로 : 그래서 당신의 SelectCommand 반환하는 경우, 예를 들어

The ConflictDetection property determines whether parameters for old and new values are applied to the Update method. For example, if the command that is specified by the SelectCommand property returns a DataTable object with the columns Name and Number and the ConflictDetection property is set to the OverwriteChanges value, parameters are created for Name and Number for the Update method.

firstname이면 UpdateCommand에 이미 firstname 매개 변수가 있으며 그 원인이 오류입니다.

가 작동하는지 모르겠지만, 시도하는 간단한 일이 먼저 UpdateParameters 수집을 삭제 될 수 없음 :

using (SqlDataSource ds = AllUsers) 
{ 
    Guid guid = new Guid(); 
    try 
    { 
     guid = Guid.Parse(Session["user_id"].ToString()); 
    } 
    catch { guid = Guid.NewGuid(); } 

    ds.UpdateCommand = "UpdateUser"; 
    ds.ConflictDetection = ConflictOptions.OverwriteChanges; 
    ds.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure; 

    ds.UpdateParameters.Clear(); 

    ds.UpdateParameters.Add("userid", guid.ToString()); 
    ds.UpdateParameters.Add("username", "Diogo"); 
    ds.UpdateParameters.Add("email", "[email protected]"); 
    ds.UpdateParameters.Add("isAnonimo", "0"); 
    ds.UpdateParameters.Add("isLocked", "0"); 
    ds.UpdateParameters.Add("roleId", ""); 

    ds.Update(); 
    DetailsView1.DataBind(); 
} 
+0

'Rhumbori' 나는 당신이 뭔가있는 것 같아요 .. 만약 OP가'SqlConnection && SQLCommand Object's'를 사용하는 코드를 리팩터링하고 OP가 사용할 것이면 여전히 해결 될 수 있다고 생각합니다. Command.Parameters.Add 메서드 오버로드 기능이 제대로 작동합니다. – MethodMan

+0

시도했지만 작동하지 않았습니다! – Severiano

+1

뭘 시도하고 작동하지 않았습니까? Severiano가되기 위해 필요한 것보다 더 간단한 프로세스를 만들고 있습니다. 왜 AllUsers를이 방법으로 업데이트하려고하는지 모릅니다. C# 코드에서 정상'StoredProcedure'를 호출 한 다음 SqlDataClient를 사용하여 cmd.ExecuteNonQuery 명령을 실행하십시오. 내 생각에 바퀴를 돌리는 것보다 훨씬 쉽습니다. – MethodMan

관련 문제