在Windows Phone开发中,我有一些来自MS SQL数据库的数据。我从数据库发送一个列表,但我想将其转换为客户端中的字符串数组。但我不知道该怎么做。
答案 0 :(得分:1)
在C#中如果你有列表,那么你可以试试这个:
string[] str = lst.ToArray();
C#List有ToArray()
内置方法。
这是一段代码,假设您可能希望从C#连接到SQL Server,将select语句行转换为列表,然后将该列表转换为字符串数组:(填写?
标记根据您的系统/数据库使用适当的值。Here is MSDN articles: one和two.
using (SqlConnection CONN = new SqlConnection("server=?;database=?;Integrated Security=?")) {
//e.g. new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=dbname");
String queryString = "SELECT CustomerID, CompanyName FROM dbo.Customers";
SqlDataAdapter adapter = New SqlDataAdapter(queryString, CONN);
DataSet dset = New DataSet();
adapter.Fill(dset, "Customers");
List<string> lst = new List<string>();
//iterate through Dataset
foreach(DataRow row in dset.Tables["Customers"].Rows)
{
lst.Add(row["CompanyName"].ToString());
}
//to string array
string[] str = lst.ToArray();
}
答案 1 :(得分:0)
// New list here.
List<string> l = new List<string>();
l.Add("one");
l.Add("two");
l.Add("three");
l.Add("four");
l.Add("five");
// B.
string[] s = l.ToArray();