• Winform中使用Castle操作SQL2008实现添加记录


    开发环境:VS2010+NET4.0+SQLSEREVER2008R2+Castle.Facilities.ActiveRecordIntegration-1.1.2.RC

    ActiveRecord相关DLL下载地址:http://sourceforge.net/projects/castleproject/files/ActiveRecord%20Integration/1.1/

    打开VS2010,新建一个名称为“CastleManager”,NET4。0支持的Winform工程,

    1、添加相关引用:找到Castle.Facilities.ActiveRecordIntegration-1.1.2.RC.zip解压缩后的Castle.Facilities.ActiveRecordIntegration-1.1.2.RC\net40目录下的所有DLL文件。

    2、设置app.config文件:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" />
      </configSections>
      <activerecord>
        <config>
          <add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
          <add key="dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
          <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
          <add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" />
          <add key="connection.connection_string" value="UID=sa;Password=123456;Initial Catalog=CastleDB;Data Source=192.168.1.28" />
        </config>
      </activerecord> 
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
      </startup>
    </configuration>

    3、创建实体类:

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

    namespace CastleManager
    {
        /// <summary>
        /// User 的摘要说明。
        /// </summary>
        [ActiveRecord("Users")]
        public class User : ActiveRecordBase
        {
            private int _id;

            private string _name;

            private string _password;

            private string _emailAddress;

            private DateTime _lastLogon;

            [PrimaryKey(PrimaryKeyType.Identity, "LogonID")]
            public int Id
            {
                get { return _id; }
                set { _id = value; }
            }

            [Property("LogonName")]
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }

            [Property("Password")]
            public string Password
            {
                get { return _password; }
                set { _password = value; }
            }

            [Property("EmailAddress")]
            public string Address
            {
                get { return _emailAddress; }
                set { _emailAddress = value; }
            }

            [Property("LastLogon")]
            public DateTime LastLogon
            {
                get { return _lastLogon; }
                set { _lastLogon = value; }
            }

            public static void DeleteAll()
            {
                DeleteAll(typeof(User));
            }

            public static IList FindAll()
            {
                return (IList)FindAll(typeof(User));
            }

            public static User Find(int id)
            {
                return (User)FindByPrimaryKey(typeof(User), id);
            }
        }
    }

    4、编写窗体代码:打开窗体Form1,窗体上拖放一个按钮控件,双击按钮,输入代码如下:

    //添加引用

    using Castle.ActiveRecord.Framework;
    using Castle.ActiveRecord;
    using System.Configuration;
    using System.Collections;
    using Castle.ActiveRecord.Framework.Config;


            private void button1_Click(object sender, EventArgs e)
            {
                AddUser();
            }
            /// <summary>
            /// 使用ActiveRecord添加用户记录
            /// </summary>
            public void AddUser()
            {
                try
                {
                    IConfigurationSource source = (IConfigurationSource)ConfigurationManager.GetSection("activerecord");
                    ActiveRecordStarter.Initialize(source, typeof(User));
                    User user = new User();

                    user.Name = "测试名字";
                    user.Password = "测试密码";
                    user.Address = "test@google.com";
                    user.LastLogon = DateTime.Now;

                    user.Create();
                    MessageBox.Show("添加成功");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("添加失败!"+ex.Message);
                }

            }

    5、创建数据库和数据表:

    打开SQLSERVER2008 R2。新建一名称为CastleDB的数据库,然后创建数据表Users

    USE [CastleDB]
    GO

    /****** Object:  Table [dbo].[Users]    Script Date: 01/11/2013 12:50:50 ******/
    SET ANSI_NULLS ON
    GO

    SET QUOTED_IDENTIFIER ON
    GO

    SET ANSI_PADDING ON
    GO

    CREATE TABLE [dbo].[Users](
     [LogonID] [int] IDENTITY(1,1) NOT NULL,
     [LogonName] [varchar](40) NULL,
     [Password] [varchar](20) NULL,
     [EmailAddress] [varchar](40) NULL,
     [LastLogon] [datetime] NULL,
     CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED
    (
     [LogonID] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]

    GO

    SET ANSI_PADDING OFF
    GO


     6、程序测试:在VS2010中按F5运行程序,点击窗体按钮,然后查看数据库中对应的数据记录。证明添加成功。

    备注:项目源码请到我的资源中去下载。

  • 相关阅读:
    兼容Linux和Windows下获取视频截图
    webSocket通信
    解决IE浏览器弹出下载框的问题
    jsp的九大内置对象和四大作用域(转)
    如何确定神经网络的层数和隐藏层神经元数量
    Mac 修改系统默认Java版本
    Weka的使用
    在Mac OS系统中安装oracle
    P & NP & NPC & NP-HARD
    Dynamic programming——自下而上 VS. 自上而下
  • 原文地址:https://www.cnblogs.com/xqf222/p/3306738.html
Copyright © 2020-2023  润新知