ArcGIS Engine - 操作PostgreSQL数据库

1.安装NpgSQL

使用C#访问本地数据库,特别是PostSQL的话,就用NpgSQL就Ok了,直接用NuGet为你的解决方案添加即可。右击选择“管理NuGet程序包”安装Npgsql。
在这里插入图片描述

2.连接数据库

		//配置数据库连接返回数据库连接 
        public static NpgSQLConnection Connection()
        {
            //配置数据库连接,Port安装默认端口号为5432
            string hostname = "127.0.0.1";
            string database = "postgres";
            string username = "postgres";
            string password = "*****";
            
            string connectionString= string.Format("PORT=5432;DATABASE={0};HOST={1};PASSWORD={2};USER ID={3};Pooling = false", database, hostname, password, username);  
            
            //配置数据库连接字符串
            try
            {
                NpgsqlConnection connection = new NpgsqlConnection(connectionString);
                connection.Open();
				return connection;
            }
            catch (Exception e)
            {
                return MessageBox.show(e.Message);
            } 
            
        }

3.操作数据库

		private static DataToDataBase()
		{
 			//连接到数据库
            NpgsqlConnection connection = Connection();

            //数据库操作
            try
            {
                string SQL = "";
                string TableName = "DataBase";
                string colunm = "";

                //新建表格
                SQL = "select * from pg_class where relname = " + "'" + TableName + "'";
                using (NpgsqlCommand cmd = new NpgsqlCommand(SQL, connection))
                {
                    object obj = cmd.ExecuteScalar();

                    //如果存在删除表格
                    if (obj != null)
                    {
                        SQL = "drop table " + TableName;
                        response = DatabaseHelper.ExecuteNonQuery(SQL, connection);
                        if (response.Code != Constant.Success) return response;
                    }

                    //创建表格
                    for (int i = 0; i < Exportable_Fields_List.Count; i++)
                    {
                        colunm += '"' + Exportable_Fields_List[i].Name  + '"' + " ";
                        colunm += DataType(Exportable_Fields_List[i].DataType) + ",";

                    }
                    SQL = "create table " + TableName + "(" + colunm.Substring(0, colunm.Length - 1) + ")";
                    response = DatabaseHelper.ExecuteNonQuery(SQL, connection);
                    if (response.Code != Constant.Success) return response;

                }

                //获取工程工作空间
                IWorkspace workspace = null;
                string projectPath = ProjectHelper.GetProjectPath();
                response = MapHelper.GetShapefileWorkspace(projectPath, out workspace);
                if (response.Code == Constant.Failure) return response;

                //检查并获取要素图层
                ILayer pLayer = null;
                response = CheckNTSLFeatureLayer(feature, workspace, out pLayer);
                if (response.Code == Constant.Failure) return response;

                //遍历所有要素
                IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
                IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
                IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, true);
                IFeature pFeature = pFeatureCursor.NextFeature();
                while (pFeature != null)
                {
                    string FilesName = "";
                    string Values = "";

                    //获取数据
                    for (int i = 0; i < Exportable_Fields_List.Count; i++)
                    {
                        int l = pFeature.Fields.FindField(Exportable_Fields_List[i].Name);
                        if (l != -1)
                        {
                            //获取对应字段的值
                            object Value = pFeature.get_Value(l);
                            
                            if (Value != null)
                            {

                                FilesName += '"' + Exportable_Fields_List[i].Name + '"' + ",";

                                Values += "'" + Value.ToString() + "'" + ",";
                                
                            }
                           
                        }

                    }

                    //数据入库
                    if (FilesName != "" && Values != "")
                    {
                        SQL = "insert into " + TableName + " (" + FilesName.Substring(0, FilesName.Length - 1) + ")" + " values " + "(" + Values.Substring(0, Values.Length - 1) + ")";
                        response = DatabaseHelper.ExecuteNonQuery(SQL, connection);
                        if (response.Code != Constant.Success) return response;
                    }
                    
                    pFeature = pFeatureCursor.NextFeature();
                }

                //断开数据库连接
                connection.Close();

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
	}

在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王八八。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值