我想将sql查询结果传递给C#中的变量。 例如。我有表名TEST_TABLE,我想将其值传递给一个名为“Customer_details_values”的c#变量。我这样做了
String Customer_details_values;
StringBuilder var1 = new StringBuilder();
String var1 = "";
var1 = var1 +"SELECT name, "+"\n";
var1 = var1 +" account_type, "+"\n";
var1 = var1 +" city, "+"\n";
var1 = var1 +" phone, "+"\n";
var1 = var1 +" account_details, "+"\n";
var1 = var1 +" gender "+"\n";
var1 = var1 +"FROM test_table ";
Customer_details_values= var1.ToString()
它没有给我任何错误,但即使它没有将任何值传递给变量。
答案 0 :(得分:1)
使用dapper(officially和officially使用SO的形式之一非常容易:-)):
class Test
{
public string name { get; set; }
public string account_type { get; set; }
public string city { get; set; }
public string phone { get; set; }
public string account_details { get; set; }
public string gender { get; set; }
}
var items = connection.Query<Test>(var1.ToString());
// items is now an IEnumerable<Test>, you can do:
foreach (var item in items)
{
Console.WriteLine("{0} ({1})", item.name, item.phone);
}
A ......和StringBuilder
以这种方式使用:
StringBuilder var1 = new StringBuilder();
var1.Append("SELECT name, "+"\n");
var1.Append(" account_type, "+"\n");
var1.Append(" city, "+"\n");
var1.Append(" phone, "+"\n");
var1.Append(" account_details, "+"\n");
var1.Append(" gender "+"\n");
var1.Append("FROM test_table ");
string query = var1.ToString();