从SQL Server中批量读取数据到Revit(Revit二次开发)

在进行Revit的二次开发中,经常会从数据库中提取数据,来辅助插件的运行,下面就以批量创建墙为例,说说到底该如何从SQL Sever中取出数据。

一、在数据库中创建表

对于需要从数据库中读取的数据,我们先在数据库中创建对应的表:
在这里插入图片描述
并且把表的列设计好:
在这里插入图片描述

二、在类库中创建对应的类

在我们写的插件中,创建一个单独的类,来和数据库的各项对应起来:

public class WallInformation
    {
        //墙的ID
        public int WallID { get; set; }
        //墙高
        public double WallHeight { get; set; }
        //墙底部偏移
        public double WallOffset { get; set; }
        //起点坐标
        public double StartPointX { get; set; }
        public double StartPointY { get; set; }
        public double StartPointZ { get; set; }
        //终点坐标
        public double EndPointX { get; set; }
        public double EndPointY { get; set; }
        public double EndPointZ { get; set; }


        //计算出来的值
        //面积
        public double Area { get; set; }
        //长度
        public double Length { get; set; }

    }

三、在插件程序中调用

首先要确保数据库打开了远程访问,具体方法可以参考我的另外一篇文章:SQL Server 2019 开启数据库远程访问

这里我是写了一个方法来获取对应类的List ,需要注意的是,因为建立链接和执行SQL命令会占用内存,所以我将他们写在using中:

private List<WallInformation> GetFromSQL()
        {
            List<WallInformation> wallInformationList = new List<WallInformation>();
            //1 编写连接字符串与sql语句
            string connString = @"Server=.;DataBase=WallDB;Uid=sa;Pwd=zh123456";
            string sql = $"select * from WallInformation ";
            //2 建立连接
            using (SqlConnection conn = new SqlConnection(connString))
            {
                try
                {
                    //3 打开连接
                    conn.Open();
                    //4 执行命令
                    using (SqlCommand cmd = new SqlCommand(sql, conn))
                    {
                        //5 读取返回值
                        SqlDataReader sqlDataReader = cmd.ExecuteReader();
                        while (sqlDataReader.Read())
                        {
                            WallInformation wallInformation = new WallInformation();
                            wallInformation.WallID = Convert.ToInt32(sqlDataReader["WallID"]);
                            wallInformation.WallHeight = Convert.ToDouble(sqlDataReader["WallHeight"]);
                            wallInformation.WallOffset = Convert.ToDouble(sqlDataReader["WallOffset"]);
                            wallInformation.StartPointX = Convert.ToDouble(sqlDataReader["StartPointX"]);
                            wallInformation.StartPointY = Convert.ToDouble(sqlDataReader["StartPointY"]);
                            wallInformation.StartPointZ = Convert.ToDouble(sqlDataReader["StartPointZ"]);
                            wallInformation.EndPointX = Convert.ToDouble(sqlDataReader["EndPointX"]);
                            wallInformation.EndPointY = Convert.ToDouble(sqlDataReader["EndPointY"]);
                            wallInformation.EndPointZ = Convert.ToDouble(sqlDataReader["EndPointZ"]);
                            wallInformationList.Add(wallInformation);
                        }
                        sqlDataReader.Close();
                    }
                }
                catch (Exception ex)
                {
                    TaskDialog.Show("1", ex.Message);
                }
                finally
                {
                    conn.Close();
                }
            }
            return wallInformationList;
        }

四、将获取到的数据批量上传到数据库

同样,在数据库创建一个接收数据用的表,并且设计好对应的字段:
在这里插入图片描述
在这里插入图片描述

下面是将获取到的数据上传到数据库的方法:

private void FillSQL()
        {
            WallList = CreateWall.GetList(Doc, BuiltInCategory.OST_Walls, typeof(Wall)).ToList().ConvertAll(x => x as Wall);
            if (WallInformationList.Count() != WallList.Count())
            {
                TaskDialog.Show("1", "生成的族实例有问题,无法传入");
            }
            int result = 0;
            //将墙的附加信息传入实体类
            for (int i = 0; i < WallInformationList.Count(); i++)
            {
                WallInformationList[i].Area = UnitUtils.Convert(WallList[i].LookupParameter("面积").AsDouble(), DisplayUnitType.DUT_SQUARE_FEET, DisplayUnitType.DUT_SQUARE_METERS);
                WallInformationList[i].Length = UnitUtils.Convert(WallList[i].LookupParameter("长度").AsDouble(), DisplayUnitType.DUT_DECIMAL_FEET, DisplayUnitType.DUT_MILLIMETERS);
            }
            //将实体类中的数据写入数据库
            for (int i = 0; i < WallInformationList.Count(); i++)
            {
                //1 编写连接字符串与SQL语句
                string connString = @"Server=.;DataBase=WallDB;Uid=sa;Pwd=zh123456";
                string sqlInsert = $"INSERT INTO WallOtherAttributes (WallID,Area,Length) VALUES({WallInformationList[i].WallID}, {WallInformationList[i].Area}, {WallInformationList[i].Length}); ";
                //2创建连接
                using (SqlConnection connInsert = new SqlConnection(connString))
                {
                    try
                    {
                        connInsert.Open();
                        using (SqlCommand cmdInsert = new SqlCommand(sqlInsert, connInsert))
                        { 
                            //执行插入并返回结果
                            result += cmdInsert.ExecuteNonQuery();
                        }
                    }
                    catch (Exception ex)
                    {
                        TaskDialog.Show("1", ex.Message);
                    }
                    finally
                    {
                        connInsert.Close();
                    }
                }
            }
            TaskDialog.Show("1", "数据库插入" + result + "条");
        }

因为博文的文件比较大,没法将所有的都贴出来,博文中的文件可以在这里免费下载:Revit与SQLServer数据库的交互(免费下载)

欢迎大家交流!

  • 1
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值