//最近学习了一下Linq To Sql 在这里记录一下,备用
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
using LinqSpace;
using System.Data.Linq;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
//System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=DATABASE-VPC\\SQL2000;uid=Jiang;pwd=admin;database=Jiang");
//conn.Open();
UserDataContext db = new UserDataContext("server=DATABASE-VPC\\SQL2000;uid=Jiang;pwd=admin;database=Jiang");
var users = from u in db.Users select u;//db.UsersInfo where u.UserName == s.UserName select u;
foreach (var a in users)
this.listBox1.Items.Add(a.Uid);
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
UserDataContext db = new UserDataContext("server=DATABASE-VPC\\SQL2000;uid=Jiang;pwd=admin;database=Jiang");
System.IO.StreamWriter sw = new System.IO.StreamWriter(Application.StartupPath + (@"/abc.txt"), true);
db.Log = sw;
int aaa = (int)this.listBox1.SelectedItem;
//var s = from u in db.Users from p in u.UsersInfo where u.Uid == aaa select p;
var users = from u in db.Users from i in u.UsersInfo where u.Uid == aaa select i;
this.listBox2.Items.Clear();
foreach (var a in users)
this.listBox2.Items.Add(a.UserName);
sw.Close();
}
private void button1_Click(object sender, EventArgs e)
{
UserDataContext db = new UserDataContext("server=DATABASE-VPC\\SQL2000;uid=Jiang;pwd=admin;database=Jiang");
this.listBox1.Items.Clear();
var Eidt = db.Users.Single(x => x.Uid == 2);
Eidt.UserName = "蒋金颖123";
db.SubmitChanges();
var users = from s in db.Users select s;
foreach (var a in users)
this.listBox2.Items.Add(a.UserName.ToString() + " " + a.Uid.ToString());
}
}
public class UserDataContext : DataContext
{
public Table<Users> Users;
//public Table<UsersInfo> UsersInfo;
public UserDataContext(IDbConnection conn) : base(conn) { }
public UserDataContext(string conn) : base(conn) { }
}
}
namespace LinqSpace
{
using System.Data.Linq.Mapping;
using System.Data.Linq;
[Table(Name = "cdb_members")]
public class Users
{
[Column(IsPrimaryKey = true)]
public int Uid;
[Column]
public string UserName;
[Column]
public string PASSWORD;
private EntitySet<UsersInfo> usersinfo;
[Association(Storage = "usersinfo", OtherKey = "Uid")]
public EntitySet<UsersInfo> UsersInfo
{
get { return this.usersinfo; }
set { this.usersinfo.Assign(value); }
}
}
[Table(Name = "cdb_membersinfo")]
public class UsersInfo
{
[Column(IsPrimaryKey=true)]
public int Fid;
[Column]
public int Uid;
[Column]
public string UserName;
[Column]
public string Sex;
[Column]
public int Age;
[Column]
public string ADDRESS;
[Column]
public string Tel;
[Column]
public string Phone;
private EntityRef<Users> users;
[Association(Storage = "users", ThisKey = "Uid")]
public Users Users
{
get { return this.users.Entity; }
set { this.users.Entity = value; }
}
}
}