简体   繁体   English

C#使用Dapper和SQL Server标量变量

[英]C# using Dapper and sql server scalar variable

I am facing a problem which is familiar in declaring a scalar variable. 我遇到一个在声明标量变量时很熟悉的问题。 I tried so many tricks and searched in this forum with no use. 我尝试了很多技巧,却没有在这个论坛中进行搜索。

Problem 问题

I am using Dapper in my application and after connecting to the SQL Server I'm calling a stored procedure to insert a company into the company table. 我在应用程序中使用Dapper,并且在连接到SQL Server之后,我正在调用存储过程以将公司插入到Company表中。

The stored procedure works fine in SSMS when executing it manually, but in the application it keeps giving me error you must declare scalar variable in @comp_Addre . 手动执行存储过程时,该存储过程在SSMS中可以正常工作,但是在应用程序中,它总是给我错误,您必须在@comp_Addre声明标量变量。 All other variables working fine except this one and I really don't understand why. 除此变量外,所有其他变量都可以正常工作,我真的不明白为什么。

Code

public void insertComp(string compID, string compName,  string comp_Addressing, string teleComp, string faxComp, string foundDate, string ownername, string license_ID, string taxingID, string licenseExpiring, string cb_country)
{
    using (IDbConnection Connecting = new System.Data.SqlClient.SqlConnection(connectHelper.CnnVal("hrDB")))
    {
        List<companyModel> companies = new List<companyModel>();
        companies.Add(new companyModel { companyID = compID, companyName = compName,  company_Address=comp_Addressing,telephoneNumber = teleComp, faxNumber = faxComp, foundationDate = foundDate, ownerName = ownername, licenseID = license_ID, taxID = taxingID, licenseExpire = licenseExpiring, country=cb_country });
        Connecting.Execute("dbo.InsertComp @CompanyID, @CompanyName,  @comp_Addre, @TelephoneNumber, @FaxNumber, @FoundationDate, @OwnerName, @LicenseID, @TaxID, @LicenseExpire, @Country", companies);
    }
}

I don't have a working Dapper & C# solution on my machine right now, but just by formatting your code a bit better you can see the error/typo in the code. 我的机器上目前没有可正常使用的Dapper&C#解决方案,但是通过更好地格式化代码,您可以在代码中看到错误/错误。

The code below should work, because I have changed the comp_Addre to company_Address in the parameterized call to the stored procedure. 下面的代码应该可以工作,因为在存储过程的参数化调用中,我已将comp_Addre更改为company_Address

Of course, you can also keep the comp_Addre in the stored procedure and change the property of companyModel to company_Address . 当然,你也可以保持comp_Addre在存储过程和的属性更改companyModelcompany_Address

public void insertComp(string compID, string compName,  string comp_Addressing, string teleComp, string faxComp, string foundDate, string ownername, string license_ID, string taxingID, string licenseExpiring, string cb_country)
{
    using (IDbConnection Connecting = new System.Data.SqlClient.SqlConnection(connectHelper.CnnVal("hrDB")))
    {
        List<companyModel> companies = new List<companyModel>();
        companies.Add(
            new companyModel { 
                companyID = compID, 
                companyName = compName,  
                company_Address = comp_Addressing,
                telephoneNumber = teleComp, 
                faxNumber = faxComp, 
                foundationDate = foundDate, 
                ownerName = ownername, 
                licenseID = license_ID, 
                taxID = taxingID, 
                licenseExpire = licenseExpiring, 
                country = cb_country });

        Connecting.Execute("dbo.InsertComp 
            @CompanyID, 
            @CompanyName,  
            @company_Address, 
            @TelephoneNumber, 
            @FaxNumber, 
            @FoundationDate, 
            @OwnerName, 
            @LicenseID, 
            @TaxID, 
            @LicenseExpire, 
            @Country", companies);
    }
}

Aside from naming the properties the same on both models, I would advice you to check out the naming conventions of the C# language. 除了在两个模型上都对属性进行命名之外,我建议您检查一下C#语言的命名约定。 Reading (and enforcing) them will make your code easier to understand across multiple people/teams and less typo/error prone. 阅读(并强制执行)它们将使您的代码在多个人员/团队中更容易理解,并且不会出现错字/错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM