我正在尝试从SQL Server数据库表创建字符串列表。
下面是我的存储过程:
ALTER PROCEDURE [dbo].[spCountry_GetCountryDataById]
@CountryId int
AS
BEGIN
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT
CountryName, Continent, Population, Capital,
Language, Religion, Currency, DishName
FROM
Country
WHERE
id = @CountryId
END
这是我查询的方法:
public static List<string> GetCountryDataByCountryId(int countryId)
{
List<string> countryData = new List<string>();
using (IDbConnection connection = new SqlConnection(CnnString("MathemaKids")))
{
DynamicParameters p = new DynamicParameters();
p.Add("CountryId", countryId);
countryData = connection.Query<string>("spCountry_GetCountryDataById", p, commandType: CommandType.StoredProcedure).ToList();
}
return countryData;
}
因此,我希望列表中有8个字符串,但是当我运行它时,它只返回第一个字符串(countryname
)。
问题出在哪里,有人吗?
创建一个对应于表结构的类。试试,比如:
public class Country
{
//I have defined all as strings, better to define them corresponding to your table datatypes
public string CountryName {get;set;}
public string Continent {get;set;}
public string Population {get;set;}
public string Capital {get;set;}
public string Language {get;set;}
public string Religion {get;set;}
public string Currency {get;set;}
public string DishName {get;set;}
}
public static List<Country> GetCountryDataByCountryId(int countryId)
{
List<Country> countryData = new List<Country>();
using (IDbConnection connection = new SqlConnection(CnnString("MathemaKids")))
{
DynamicParameters p = new DynamicParameters();
p.Add("CountryId", countryId);
countryData = connection.Query<string>("spCountry_GetCountryDataById", p, commandType: CommandType.StoredProcedure).ToList();
}
return countryData;
}