首页 > 技术文章 > Linq To Sql

jiangshuai52511 2016-03-03 14:56 原文

1、什么是Linq to sql

Linq to sqlLINQ.NET语言集成查询)的一部分,全称基于关系数据的 .NET 语言集成查询,用于以对象形式管理关系数据,并提供了丰富的查询功能,它和Linq to xmlLinq to objectsLinq to datasetLinq to entities等组成了强大的LINQ

2、添加linq to sql

 

3、主界面

4、代码界面

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 LINQ
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Class1 c = new Class1();
            dataGridView1.DataSource = c.Select();
        }

        private void btInsert_Click(object sender, EventArgs e)
        {
            InsertForm insert = new InsertForm(this.dataGridView1);
            insert.ShowDialog();
        }

        private void btSelect_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = new Class1().Select();
        }

        private void btCancle_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count <= 0)
            { return; }
            foreach (DataGridViewRow item in dataGridView1.SelectedRows)
            {
                Fruit data = new Fruit();

                data = new Class1().SelectFruit(item.Cells[0].Value.ToString());
                new Class1().Delete(data);
            }
            dataGridView1.DataSource = new Class1().Select();
            
        }
    }
}

5、增删改查

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LINQ
{
    class Class1
    {
        private DataClasses1DataContext Context;
        public Class1()
        {
            Context = new DataClasses1DataContext();
        }
        public void Insert(Fruit data)
        {
            Context.Fruit.InsertOnSubmit(data);
            Context.SubmitChanges();//将改动提交到数据库
        }
        public void Updata(Fruit data)
        {    //先查询,再修改( => 叫做lamda表达式,r 代表一行数据)
            Fruit sdata = Context.Fruit.Where(r => r.Ids == data.Ids).First();
            if (sdata != null)
            {
                sdata.Ids = data.Ids;
                sdata.Image = data.Image;
                sdata.Name = data.Name;
                sdata.Numbers = data.Numbers;
                sdata.Price = data.Price;
                sdata.Source = data.Source;
                sdata.Stack = data.Stack;
            }
                Context.SubmitChanges();
        }
        public void Delete(Fruit data)
        {
            Fruit d = Context.Fruit.Where(r => r.Ids == data.Ids).Single();
            Context.Fruit.DeleteOnSubmit(d);
            Context.SubmitChanges();
        }
        public List<Fruit> Select()
        {
            return Context.Fruit.ToList();
        }
        public Fruit SelectFruit(string ids)
        {
            return Context.Fruit.Where(r => r.Ids == ids).First();
        }
        public List<Fruit> SelectName(string name)
        {
            return Context.Fruit.Where(r => r.Name.Contains(name)).ToList();
        }
        public bool Selctpass(string name, decimal price)//bool表达式返回true或false
        {
            return Context.Fruit.Where(r => r.Name == name && r.Price == price).Count() > 0;
        }
    }
}

 6、添加界面

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 LINQ
{
    public partial class InsertForm : Form
    {
        public InsertForm()
        {
            InitializeComponent();
        }
        public DataGridView dt;
        public InsertForm(DataGridView d)
        {
            InitializeComponent();
            dt = d;
        }
        
        private void btOk_Click(object sender, EventArgs e)
        {
            Fruit data = new Fruit();
            data.Ids = txtIds.Text;
            data.Image = txtImage.Text;
            data.Name = txtName.Text;
            if (txtNumbers.Text == "")
            {
                txtNumbers.Text = "0";
            }
            data.Numbers = int.Parse(txtNumbers.Text);
            if (!txtPrice.Text.Contains("."))
            {
                txtPrice.Text += ".00";
            }
            data.Price =decimal.Parse(txtPrice.Text);
            data.Source = txtSource.Text;
            data.Stack = txtStack.Text;
            new Class1().Insert(data);
            dt.DataSource = new Class1().Select();
            this.Close();
        }
    }
}

 

推荐阅读