如何在N层数据应用程序中使用SQL Update

时间:2014-09-17 00:14:19

标签: c# mysql asp.net n-tier-architecture

我在本教程(click here)中获得了一些信息和想法,用asp.net创建我的第一个N层数据应用程序,但问题是当我尝试在DataAccessTier中使用更新查询时在DataService中收到1个错误。

错误:

Error   1   Cannot implicitly convert type 'int' to 'DataEntityTier.GW_UTADataSet.ActiveDirectory2DataTable'    C:\Users\BA-OJT\documents\visual studio 2010\Projects\GoActiveDirectory\DataService\Service1.cs 59  20  DataService

我该如何解决这个问题?

我的源代码:

项目:DataAccessTier 查询: UpdateBySAN

UPDATE [dbo].[ActiveDirectory2] 
SET [SamAccountName] = @SamAccountName, [Surname] = @Surname, [GivenName] = @GivenName, [EmailAddress] = @EmailAddress, [Enabled] = @Enabled, [Guid] = @Guid, [DateCreated] = @DateCreated, [DateModified] = @DateModified, [SID] = @SID, [EmployeeNumber] = @EmployeeNumber 
WHERE (SamAccountName = @SamAccountName);

SELECT ID, SamAccountName, Surname, GivenName, EmailAddress, Enabled, Guid, DateCreated, DateModified, SID, EmployeeNumber FROM ActiveDirectory2 
WHERE (SamAccountName = @SamAccountName)

项目:DataService 类: IService1.cs

[OperationContract]
DataEntityTier.GW_UTADataSet.ActiveDirectory2DataTable UpdateAccountBySAN(
        string SamAccountName, string Surname, string GivenName, string EmailAddress,
        bool Enabled, string Guid, string DateCreated, string DateModifide, string SID,
        string EmployeeNumber);

项目:DataService 类: Service1.cs

public DataEntityTier.GW_UTADataSet.ActiveDirectory2DataTable UpdateAccountBySAN(
        string SamAccountName, string Surname, string GivenName, string EmailAddress,
        bool Enabled, string Guid, string DateCreated, string DateModified, string SID,
        string EmployeeNumber)
    {
        DataAccessTier.GW_UTADataSetTableAdapters.ActiveDirectory2TableAdapter
            ActiveDirectory2TableAdapter1
                = new DataAccessTier.GW_UTADataSetTableAdapters.ActiveDirectory2TableAdapter();

        return ActiveDirectory2TableAdapter1.UpdateBySAN(
            SamAccountName, Surname, GivenName, EmailAddress, Enabled, Guid, DateCreated,
            DateModified, SID, EmployeeNumber); //ErrorRedLine@ ActiveDirectorr2TableAdapter1.UpdateBysan() Cannot implicitly convert type 'int' to 'DataEntityTier.GW_UTADataSet.ActiveDirectory2DataTable'
    }

1 个答案:

答案 0 :(得分:0)

在做了一些实验后,我发现了如何解决自己的问题。

对于那些关注本教程的人(click here)并希望在DataAccessTier中使用更新查询

要使用更新,您可以使用int而不是ActiveDirectory2DataTable,因为ActiveDirectory2DataTable仅用于从DataAccessTier接收表格,更新查询只会返回计数更新的行。


项目 DataAccessTier 名称: UpdateAccountBySAN(@parameterhere ...)

UPDATE [dbo].[ActiveDirectory2] SET [SamAccountName] = @SamAccountName, [Surname] = @Surname, [GivenName] = @GivenName, [EmailAddress] = @EmailAddress, [Enabled] = @Enabled, [Guid] = @Guid, [DateCreated] = @DateCreated, [DateModified] = @DateModified, [SID] = @SID, [EmployeeNumber] = @EmployeeNumber WHERE (SamAccountName = @SamAccountName);
SELECT ID, SamAccountName, Surname, GivenName, EmailAddress, Enabled, Guid, DateCreated, DateModified, SID, EmployeeNumber FROM ActiveDirectory2 WHERE SamAccountName = @SamAccountName

项目:DataService 类: IService1.cs

[OperationContract]
    int UpdateAccountBySAN( string SamAccountName, string Surname, string GivenName,string EmailAddress,bool Enabled, string Guid, string DateCreated, string DateModified,string SID, string EmployeeNumber);

项目:DataService 类: Service1.cs

public int UpdateAccountBySAN(string SamAccountName, string Surname, string GivenName, string EmailAddress,bool Enabled, string Guid, string DateCreated, string DateModified, string SID,string EmployeeNumber)
    {
        DataAccessTier.GW_UTADataSetTableAdapters.ActiveDirectory2TableAdapter
            ActiveDirectory2TableAdapter1
                = new DataAccessTier.GW_UTADataSetTableAdapters.ActiveDirectory2TableAdapter();
        return ActiveDirectory2TableAdapter1.UpdateBySAN(SamAccountName, Surname, GivenName,
        EmailAddress, Enabled, Guid, DateCreated, DateModified, SID, EmployeeNumber);
    }

并在PresentationTier中使用它。

<强> PresentationTier

DataServiceReference.Service1Client newService = new DataServiceReference.Service1Client();

newService.UpdateAccountBySAN(_SamAccountName,_Surename,_GivenName,_EmailAddress,_Enable,_Guid,_DateCreated,_DateModified,_SID,_EmployeeNumber);
//To know how many rows updated use: var _UpdatedRows = newService.UpdateAccountBySAN(<!--some parameter here-->);

这是我对我的问题的回答,我希望这能为未来的支持者提供一些帮助。谢谢。