【VS2012】通过VS连接postgresql

【WebGIS】通过VS连接postresql

编程小白,记录学习中遇到的问题,希望可以帮助到其他的人。

一、条件

1.Visual Studio2012版本
2.Npgsql组件包

二、使用

1.下载Npgsql组件包,并配置
具体参考:vs配置连接postgresql(以vs2010为例)
(作者写的非常详细,我的版本是VS2012,以为要新一点的版本才行,所以自己下载了4.0的版本,配置会出错,最后还是跟着作者下载了Npgsql-2.2.3,后可运行)

2.连接字符串:在配置文件.config中,配置如下语句

 <connectionStrings>
    <add  name="PostgresqlConnectionString"
    connectionString="Port=5432;Database=productinfo;Host = localhost;User ID=postgres;Password=123456"
        providerName="System.Data.SqlClient"/>
  </connectionStrings>

connectionString基本配置学习参考:[connectionString基本配置学习]https://www.cnblogs.com/lipanpan/p/3665779.html

3.1后端在数据库操作代码中获取配置文件中连接的字符串:

 public static readonly string connStr = WebConfigurationManager.ConnectionStrings ["PostgresqlConnectionString"].ConnectionString;

2.2建立连接

 NpgsqlConnection conn = new NpgsqlConnection(connStr);

2.3.使用DataAdapter查询,返回DataSet

  NpgsqlConnection conn = new NpgsqlConnection(connStr);

            try
            {
                //连接access数据库
                conn.Open();
                string sql = "select * from LwSearchTime where XZMC like '%" + LocationTextLW + "%' and allyear like '%" + YearLW + "%'";
                NpgsqlDataAdapter myadapter = new NpgsqlDataAdapter(sql, conn);
                DataSet ds = new DataSet();
                myadapter.Fill(ds, "points");
                conn.Close();
                foreach (DataRow dr in ds.Tables["points"].Rows)
                {
                    sb.Append("{" + string.Format(tpl, dr["XZMC"], dr["allyear"], dr["allmon"]) + "},");
                }

                context.Response.Write(sb.ToString().Substring(0, sb.ToString().Length - 1) + "]");

                //context.Response.Write("ok");
            }
            catch (Exception exe)
            {
                context.Response.Write("fail");//如果验证失败则返回fail 
            }
            context.Response.End();

3.前段利用ajax获取

//下拉框时间年份
            var lwYData;
            var lwYearStore = new Ext.data.JsonStore({
                fields: [
                    { name: 'XZMC' },
                    { name: 'allyear' },
                ],
            });

            function comYearLW() {
                Ext.Ajax.request({
                    url: "comLwYearSearch.ashx?LocationTextLW=" + LocationTextLW, //拿到的变量发送到后台                 
                    method: "get",
                    success: function (response) {//后台返回结果
                        var getData = response.responseText; //获取服务器数据                     
                        if (getData != "") {
                            console.log("查询年份数据" + JSON.stringify(getData));
                            lwYData = eval(getData); //将json字符串转换为array
                            lwYearStore.removeAll();//可能查询另外一个,所以要先清空
                            lwYearStore.loadData(lwYData);
                        }
                        else {
                            Ext.Msg.alert("警告", "加载失败!");
                        }
                    },
                    failure: function (response, options) {
                        Ext.Msg.alert("失败");
                    }
                });
            }

4.前端在按combox中绑定查询事件

 listeners: {
              'select': function () {
              LocationTextLW = Ext.getCmp("LocationTextLW").getRawValue();//得到查询的地名
              console.log("两违下拉框地点信息" + JSON.stringify(LocationTextLW));
              comYearLW();//年份查询函数
               }
            }
  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值