[英]How to add string values from multiple columns in List<string> c# windows forms
I'm trying to create List of strings from a SQL Server database table.我正在尝试从 SQL 服务器数据库表创建字符串列表。
Here is my stored procedure:这是我的存储过程:
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
And this is my method for query:这是我的查询方法:
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;
}
So, I'm expecting 8 strings in my list, but when I run this it returns only first string ( CountryName
).所以,我希望列表中有 8 个字符串,但是当我运行它时,它只返回第一个字符串(
CountryName
)。
Where is the problem, anyone?问题出在哪里,有人吗?
Create a class that corresponds- maps to your table structure.创建对应于您的表结构的 class。 Try, like:
试试,比如:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.