0

我想将 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()

它没有给我任何错误,但即使它没有将任何值传递给变量。

4

1 回答 1

1

使用dapper非常容易(SO的官方官方使用的 ORM 之一 :-)):

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);
}

啊... aStringBuilder是这样使用的:

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();
于 2013-08-13T19:28:10.243 回答