【问题标题】:Put List<> into a SQL database table column将 List<> 放入 SQL 数据库表列
【发布时间】:2016-04-03 11:50:52
【问题描述】:

我只是在这里遇到了类似的问题,我无法找出解决方案( Retrieve list from object in Database)。

我只想知道是否可以将列表插入 SQL 数据库以及如何使用查询将其取回。

【问题讨论】:

  • 你在使用 ORM 吗?您希望如何存储列表?
  • 在数据库中插入列表是什么意思。你能说得具体一点吗?
  • 我不需要数据库中的列表,我只需要两个表之间的关系。并且在使用表的主键从另一个表中获取属于第一个表的元素的项目列表之后:)
  • 使用 Join SQL 并使用 ID 连接两个表。见网页:technet.microsoft.com/en-us/library/aa213233(v=sql.80).aspx

标签: c# database jointable


【解决方案1】:

“SQL数据库”是带有EF驱动的SQL系列数据库吗?如果是这样,那么您可以先使用代码来保留您的列表。即:(此样本具有多对多关系)

string defaultConString = @"server=.\SQLExpress;Database=CodeFirstDbSample;Trusted_Connection=yes;";

void Main()
{
    CreateSampleCodeFirstData();
    ListData();
}

private void CreateSampleCodeFirstData()
{
    var ctx = new MyContext(defaultConString);
  for (int i = 0; i < 10; i++)
  {
    var c = new Customer { CustomerName="c" + i };
    ctx.Customers.Add( c );
  }

  for (int i = 0; i < 10; i++)
  {
    var p = new Product { ProductName="p" + i };
    ctx.Products.Add( p );
  }

  ctx.SaveChanges();

  for (int i = 0; i < 10; i++)
  {
    var customer = ctx.Customers.Single (c => c.CustomerName == "c"+i);
    var products = ctx.Products.OrderBy (p => Guid.NewGuid()).Take(3);
    customer.Products = new List<Product>();
    customer.Products.AddRange( products );
  }

  ctx.SaveChanges();
}

private void ListData()
{
  var ctx = new MyContext(defaultConString);
  Console.WriteLine ("By Customer");
  Console.WriteLine ("".PadRight(50,'-'));
  foreach (Customer c in ctx.Customers.Include("Products"))
  {
    Console.WriteLine ("{0}: {1}", c.CustomerId, c.CustomerName);
    Console.WriteLine ("\t\t{0}", 
      string.Join(",", c.Products.Select (p => p.ProductName)));
  }
  Console.WriteLine ("".PadRight(50,'='));
  Console.WriteLine ();

  Console.WriteLine ("By Product");
  Console.WriteLine ("".PadRight(50,'-'));
  foreach (Product p in ctx.Products.Include("Customers"))
  {
    Console.WriteLine ("{0}: {1}", p.ProductId, p.ProductName);
    Console.WriteLine ("\t\t{0}", 
       string.Join(",", p.Customers.Select (c => c.CustomerName) ));
  }
  Console.WriteLine ("".PadRight(50,'='));
  Console.WriteLine ();
}

public class MyContext : DbContext
{
  public MyContext(string connectionString)
     : base(connectionString)  {}
  public DbSet<Customer> Customers { get; set; }
  public DbSet<Product> Products { get; set; }
}

public class Product
{
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public virtual List<Customer> Customers { get; set; }

}

public class Customer
{
    public int CustomerId{ get; set; }
    public string CustomerName{ get; set; }
    public virtual List<Product> Products { get; set; }
}

【讨论】:

  • 为了提高性能,请务必设置这些配置:context.Configuration.AutoDetectChangesEnabled = false;context.Configuration.ValidateOnSaveEnabled = false;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-23
  • 1970-01-01
  • 1970-01-01
  • 2011-12-09
  • 1970-01-01
  • 1970-01-01
  • 2013-01-10
相关资源
最近更新 更多