WinForms 使用Entity Framework 和MSSQL 存储过程数据绑定

作为一名全栈设计师,每天要面临的挑战真不少。在这个项目里面,我必须为windows服务器做一个定时批量修改MSSQL数据库里面的数据的应用,那么势必在VS 2015中新建一个Windows Forms项目,汇编成EXE在服务器中运行。

那么,下面的例子就是通过VS 2015,使用Entity Framework做数据绑定的一个实例。

新建项目

在VS 2015中新建一个项目,左窗格中选择Windows,右窗格中选择Windows 窗体应用程序,取名为:WinFormswithEF

WinFormswithEF
WinFormswithEF

安装Entity Framework NuGet软件包

在解决方案资源管理器中,右键单击WinFormswithEF项目,选择管理NuGet软件包...

在“管理NuGet软件包”对话框中,选择“EntityFramework”软件包。单击安装。

选择“EntityFramework”软件包
选择“EntityFramework”软件包

连接数据库

点击视图菜单,找到服务器资源管理器,右键单击数据连接,添加连接:

添加连接
添加连接

可以点击左下方的测试连接对数据库进行测试,如果显示连接成功就表示成功了。

逆向工程师模型

我们将使用Entity Framework Designer作为Visual Studio的一部分来创建我们的模型。
点击项目菜单 - >添加新项,选择ADO.NET 实体数据模型,取名为ProductModel

添加新项目
添加新项目

选择下一步:

来自数据库的EF设计器
来自数据库的EF设计器

选择从来自数据库的EF设计器,然后单击下一步

选择数据连接
选择数据连接

选择数据连接。

选择数据库对象和设置
选择数据库对象和设置

选择完毕之后,点击完成就可以了。

逆向工程一旦完成,新模型将被添加到您的项目中,并打开供您在实体框架设计器中查看。 App.config文件也已添加到您的项目与数据库的连接详细信息。

EF使用模板从您的模型生成代码,我们可以从代码中看到数据实体类TblProductList.cs

 using System;
    using System.Collections.Generic;
    
    public partial class TblProductList
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Category { get; set; }
        public Nullable<decimal> price { get; set; }
    }

我们还可以看到,数据上下文类是被放在ProductModel.edmx里面的。

  using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    
    public partial class webapi2Entities : DbContext
    {
        public webapi2Entities()
            : base("name=webapi2Entities")
        {
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
    
        public virtual DbSet<TblProductList> TblProductList { get; set; }
    }

因为我这个程序是需要批量修改的,在EF使用LINQ做UPDATE大家都会知道很难实现,那么只能再安装NuGet软件包:

Install-Package EntityFramework.Extended

完成代码

打开Form1设计窗口,将工具箱的Label1,timer1移到窗口里面,在后台进行代码编写,如下

using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using EntityFramework.Extensions;

namespace WinFormswithEF
{
    public partial class Form1 : Form
    {
        private int counter;
        public Form1()
        {
            InitializeComponent();
            counter = 0;
            timer1.Interval = 6000;
            timer1.Enabled = true;
            
        }

       
        private void timer1_Tick(object sender, EventArgs e)
        {
            var db = new webapi2Entities();
            
             if (counter >= 10)
            {
                // Exit loop code.
                timer1.Enabled = false;
                counter = 0;
            }
            else
            {
                db.TblProductList.Where(s => s.Name == "dddsa").Update(s => new TblProductList()
                {
                    Category = "oaa"
                });
                counter = counter + 1;
                label1.Text = "运行次数: " + counter.ToString();
            }
        }

       
    }
}

运行程序,测试通过。

注意

这个例程里面,是对已经存在的数据库进行逆向处理,让VS对已经存在的数据库进行生成代码。因为我的网站已经存在,我的数据库已经存在。我只是需要一个程序,常驻在服务器后台,使用计时器对数据库进行批量操作。

对桌面应用程序来说,使用EF并不是一个多好的主意。如果使用存储过程来批量处理,反而是个更有效率的工作。

下面是操作的步骤。

新建项目二

在VS 2015中新建一个项目,左窗格中选择Windows,右窗格中选择Windows 窗体应用程序,取名为:WinFormswithSQL

打开文件APP.CONFIG,修改一下,按照数据库的连接进行修改:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="WinFormswithSQL.Properties.Settings.webapi2ConnectionString"
            connectionString="Data Source=.;Initial Catalog=webapi2;Persist Security Info=True;User ID=sa;Password=password"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
</configuration>

在引用using System.Configuration;之前,点击解决资源管理器,右击引用,添加引用,将System.Configuration 引用进来。

新建一类,取名为conn.cs:

namespace WinFormswithSQL
{
    using System.Configuration;
    public class Conn
    {
        private static string dbconnedtionString;//缓存连接字符串
        private static string dbProviderName;//缓存数据提供器名称

        static Conn()
        {
            dbconnedtionString = ConfigurationManager.ConnectionStrings["WinFormswithSQL.Properties.Settings.webapi2ConnectionString"].ConnectionString;
            dbProviderName = ConfigurationManager.ConnectionStrings["WinFormswithSQL.Properties.Settings.webapi2ConnectionString"].ProviderName;
        }

        public static string DbConnectionString
        {
            get
            {
                return dbconnedtionString;
            }
        }

        /// <summary>
        /// 连接程序名和属性
        /// </summary>
        public static string DbProviderName
        {
            get
            {
                return dbProviderName;
            }
        }
    }
}

然后再新建类:DataACCess.cs


using System;
using System.Data;
using System.Data.Common;

    public class DataACCess
    {
        static DataACCess()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        public static DbCommand CreateComand()
        {
            string dataProviderName = Conn.DbProviderName;
            string connectionString = Conn.DbConnectionString;
            DbProviderFactory factory = DbProviderFactories.GetFactory(dataProviderName);
            DbConnection conn = factory.CreateConnection();
            conn.ConnectionString = connectionString;
            DbCommand comm = conn.CreateCommand();
            comm.CommandType = CommandType.StoredProcedure;
            return comm;
        }

        public static DataTable ExcuteselectCommmand(DbCommand command)
        {
            DataTable table;

            try
            {
                command.Connection.Open();
                DbDataReader reader = command.ExecuteReader();
                table = new DataTable();
                table.Load(reader);
                reader.Close();
            }
          
            finally
            {
                command.Connection.Close();
            }
            return table;

        }

    public static int ExecuteNonQuery(DbCommand command)
    {
        // 受影响行数
        int affectedRows = -1;
        // 执行该命令后确保关闭数据库
        try
        {
            // 打开数据库
            command.Connection.Open();
            // 执行命令并返回行数
            affectedRows = command.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            // 记录错误并且抛出他们。
            Utilities.LogError(ex);
            throw;
        }
        finally
        {
            // 关闭数据库
            command.Connection.Close();
        }
        // 返回结果
        return affectedRows;
    }
}


现在基本的手脚架已经设置完成了,我们对数据库的表新建一个取名为edit的存储过程(存储过程是由一些SQL语句和控制语句组成的被封装起来的过程,它驻留在数据库中,可以被客户应用程序调用,也可以从另一个过程或触发器调用。它的参数可以被传递和返回。与应用程序中的函数过程类似,存储过程可以通过名字来调用,而且它们同样有输入参数和输出参数。):

CREATE PROCEDURE [dbo].[edit]
    (@Category nvarchar(20),
    @Name nvarchar(20))
        AS
            BEGIN
                UPDATE TblProductList SET Category=@Category WHERE Name=@Name
            END

最后,新建一个连接的类Access.cs就可以了


using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;


    public class Access
    {
        static Access()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 直接运行存储过程文件返回DATATABLE
        /// </summary>
        /// <param name="commtext">存储过程文件名</param>
        /// <returns></returns>
        public static DataTable GETdaleis(string commtext)
        {
            DbCommand comm = DataACCess.CreateComand();
            comm.CommandText = commtext;
            return DataACCess.ExcuteselectCommmand(comm);
        }


        /// <summary>
        /// 修改类别
        /// </summary>
        /// <param name="CategoryID">ID</param>
        /// <param name="NAME">需要修改的名称</param>
        /// <returns></returns>
        public static int edit_ctegory(string Category, string Name)
        {
            DbCommand comm = DataACCess.CreateComand();
            comm.CommandText = "edit";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@Name";
            param.Value = Name;  
            param.Size = 20;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@Category";
            param.Value = Category;
            param.DbType = DbType.String;
            param.Size = 20;
            comm.Parameters.Add(param);

            int result = -1;
            try
            {
                //执行存储过程
                result = DataACCess.ExecuteNonQuery(comm);
            }
            catch { }
            //result = 1即执行成功
            return (result);
        }

    }

打开Form1设计窗口,将工具箱的Label1,timer1移到窗口里面,在后台进行代码编写,如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormswithSQL
{
    public partial class Form1 : Form
    {
        private int counter;
        public Form1()
        {
            InitializeComponent();
            counter = 0;
            timer1.Interval = 6000;
            timer1.Enabled = true;
        }

        private void timer1_Tick_1(object sender, EventArgs e)
        {
            if (counter >= 10)
            {
                // Exit loop code.
                timer1.Enabled = false;
                counter = 0;
            }
            else
            {
                if (Access.edit_ctegory("ccoo1", "dddsa"))
                {
                    if (Access.edit_ctegory("ccoo1", "dddsa")) {
                    counter = counter + 1;
                    label1.Text = "运行次数: " + counter.ToString();
                    }
                }
            }
        }
    }
}

运行结果:

运行程序
运行程序

源代码

WinFormswithSQL 的源代码我放到了Github上面:https://github.com/alexzeng2014/WinFormswithSQL

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,470评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,393评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,577评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,176评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,189评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,155评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,041评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,903评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,319评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,539评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,703评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,417评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,013评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,664评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,818评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,711评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,601评论 2 353

推荐阅读更多精彩内容