C#ASPX连接与Npgsql到PostgreSQL [英] c# aspx connect to postgresql with Npgsql

查看:1621
本文介绍了C#ASPX连接与Npgsql到PostgreSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个挂钩C#aspx页面与Npgsql到PostgreSQL的恼人的任务。

I have the annoying task of hooking a c# aspx page up to postgresql with Npgsql.

这是我有是,当我尝试导入Npgsql,我得到以下ASPX错误信息返回的问题:

The problem that i have is that when i try to import Npgsql, i get the following aspx error message returned:

CS0246:类型或命名空间名称Npgsql'找不到(是否缺少using指令或程序集引用?)

CS0246: The type or namespace name 'Npgsql' could not be found (are you missing a using directive or an assembly reference?)

我有一个调用下面的aspx页面,以从DB获得数据的JavaScript函数:

I have a javascript function that calls the aspx page below to get data from the db:

<%@ language="C#"%>
<%@ Import Namespace="Npgsql" %>
<%

  // PostgeSQL-style connection string
  string connstring = String.Format("Server=localhost;Port=5432;UID=posgres;Password=pass;Database=postgres_db;";
  NpgsqlConnection conn = new NpgsqlConnection(connstring);      
  conn.Open();
%>

林假设我需要把它架在我的web.config,但我不能为我出来的生命。

Im assuming i need to set it up in my web.config, but i cannot for the life of me it out.

任何帮助将大大AP preciated!
谢谢你。

Any help would be greatly appreciated! Thanks.

推荐答案

您的问题是,是不是被引用Npgsql.dll。你只导入命名空间。

Your problem is the Npgsql.dll that is not being references. You only import the Namespace.

这不是砍SQL到一个ASP.NET文件最佳实践......解决办法是添加DLL到Web.config文件:

It's not best practice to hack SQL into a ASP.NET document... The solution would be adding the DLL to the web.config file:

 ...
  <system.web>         
    <compilation debug="true" targetFramework="4.5">                              
      <assemblies> 
        <add assembly="Npgsql, Version=2.0.11.93, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />  
  ...

但我建议创建一个类库项目或至少使用Visual Studio(前preSS版本是免费的)一个Web应用程序项目。然后右键点击指定的目录参考,并添加DLL那里。为什么? N层( http://en.wikipedia.org/wiki/Multitier_architecture ):更好的组织code与它的意图。更容易找到错误并修改/维持code ...

But I recommend to create a class library project or at least a Web Application project using Visual Studio (Express version is FREE). Then right click on the directory named references and add the DLL there. Why? N-Tier (http://en.wikipedia.org/wiki/Multitier_architecture): better organization of code by it's intent. Easier to find errors and change/maintain code...

这篇关于C#ASPX连接与Npgsql到PostgreSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆