WYDC项目开发

一、 开发环境搭建

Vs2010 + postgresql9.0 + Npgsql.dll(Mono.Security.dll)+AjaxPro.2.dll+EasyUI

二、下载Npgsql.dll和Mono.Security.dll(官网下载)(官网

Npgsql是PostgreSQL的一个.NET数据提供程序,它可以自由获取。如果只引用了Npgsql.dll,那么程序运行起来后会报错,提示找不到对Mono.Security.dll引用的依赖项。引用后还需要在代码中添加using即可以使用.net来访问postgresql数据库了。

下面是示例代码

1、   在Web.config中添加

<add key="ConnectionString"value="Server=127.0.0.1;Port=5432;User  Id=postgres;Password=postgres;Database=gis;Encoding=UNICODE"/>

2、   web.config中读取信息

Using Npsql;
using System.Web.Configuration;
public static string GetAppSetting(string key)
{
        string value =WebConfigurationManager.AppSettings[key];
        if(value == null)
        {
            throw newConfigurationErrorsException("Error!");
        }
        return value;
}
public static string ConnectionString
{
     get
       {
           returnGetAppSetting("ConnectionString");
       }
}

3、   连接数据库

protected NpgsqlConnection GetDBConnection()
{
    NpgsqlConnection connect;
    connect = new NpgsqlConnection(ConnectionString);
    connect.Open();
    return connect;
}

以后用的时候直接NpgsqlConnection  conn=GetDBConnection();

///
/// 测试获取数据(使用要加try、catch)
///
private void TestGetData()
{
   using (NpgsqlConnection dbcon= new NpgsqlConnection(StrConnection)){
   dbcon.Open();
   NpgsqlCommand dbcmd =dbcon.CreateCommand();
   dbcmd.CommandText = "SELECT * FROM ContainerObj.TableObj limit 5";
   NpgsqlDataReader dr =dbcmd.ExecuteReader();
   string strResult =string.Empty;
   while (dr.Read())
   {
      stringstrRecord_Time_Int = dr[0].ToString();
       strResult +="record_time_int: " + strRecord_Time_Int;
   }
   dr.Close();
   dr = null;
   lblRes.Text = strResult;
}

注意:进行数据库连接的这段代码一定要加上try和catch,一来出错可以捕捉查看错误信息,二来程序不至于崩溃。

[封装好的Npgsql数据库操作类可参考此连接]

三、  Ajaxpro

1、  简介

Ajax(asynchronose javascript + xml)应用可以仅向服务器发送并取回必须的数据,它使用SOAP或者其他基于XML的web service接口,在客户端采用javascript处理来自服务器的响应。其优势在于以下几点:

l  通过异步模式,提升了用户体验

l  优化了浏览器和服务器之间的传输,减少了不必要的数据往返,减少了带宽的占用

l  Ajax引擎在客户端运行,承担了一部分本来由服务器承担的工作,从而减少了大用户量下的服务器负载。

2、  下载AjaxPro.2.dll并添加引用

Ajax.net有AjaxPro.dll和Ajax.dll。[源码下载][官网]

AjaxPro是ASP.NET平台上著名的AJAX框架,全名叫Ajax.NETProfessional。

3、  配置web.config[参考]

<system.web>
    <httpHandlers>
      <add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro.2"/>
    </httpHandlers>
</system.web>

4、  在.net方法中向客户端注册javascript,并将要调用的.NET方法添加AjaxMethod属性。

以下为引用的内容:

namespace MyDemo
{
public class _Default
{
    protected voidPage_Load(object sender, EventArgs e)
    {
     AjaxPro.Utility.RegisterTypeForAjax(typeof(_Default));
    }
    [AjaxPro.AjaxMethod]
    public DateTimeGetServerTime()
    {
      return DateTime.Now;
    }
}
}

5、  在客户端用js调用服务器端C#方法

function getServerTime()
{
<span style="white-space:pre">	</span>MyDemo._Default.GetServerTime().value; // asynchronous call
}

6、  解决AjaxPro2中core.ashx 407缺少对象的问题[源码下载地址]

下载源码,给core.js中报错代码段加上try和catch,运行build.bat(根据提示加上release)得到修改后的AjaxPro.2.dll

7、  this.onTimeout is not a function 的Bug解决方案(参考

在wydc项目中引用的AjaxPro.2.dll是经过此修改后冲编译的

timeout: function() {
try {
this.duration = new Date().getTime() - this.__start;
var r = this.onTimeout(this.duration, this);
if(typeof r == "undefined" || r != false) {
this.abort();
} else {
this.timeoutTimer = setTimeout(this.timeout.bind(this),
AjaxPro.timeoutPeriod);
}
} catch(error) {
// Statements that execute in the event of an exception
} finally {
// Statements that execute afterward either way
}

8、  参考示例1(环境搭建、接收返回值、方法的属性、缓存结果、访问session、传送DATATABLE、传送数组类型数据)

在C#中使用PowerShell时,如果遇到了Pipeline缺少using的错误,可能是因为没有正确地导入PowerShell相关的命名空间或没有正确创建和管理PowerShell对象。以下是一些基本步骤和代码示例,来帮助你解决这个问题: 首先,确保在C#代码文件的顶部添加了正确的using语句,以便能够引用PowerShell类。这通常需要添加System.Management.Automation的命名空间引用: ```csharp using System.Management.Automation; using System.Management.Automation.Runspaces; ``` 接下来,在C#代码中,你需要创建一个`Runspace`对象,然后通过这个`Runspace`来创建一个`Pipeline`对象。在这个`Pipeline`对象中,你可以添加命令,并执行这些命令。以下是一个简单的示例代码: ```csharp using System; using System.Management.Automation; using System.Management.Automation.Runspaces; class Program { static void Main() { using (Runspace myRunSpace = RunspaceFactory.CreateRunspace()) { myRunSpace.Open(); using (Pipeline myPipeline = myRunSpace.CreatePipeline()) { // 添加命令到Pipeline myPipeline.Commands.AddScript("Get-Process"); // 执行Pipeline Collection<PSObject> results = myPipeline.Invoke(); // 处理结果 foreach (PSObject result in results) { Console.WriteLine(result.ToString()); } } myRunSpace.Close(); } } } ``` 在上面的代码中,`using`语句确保了`Runspace`和`Pipeline`对象在使用完毕后能够被正确地关闭和释放资源。 如果你依然遇到“Pipeline缺少using”的错误,可能是由于PowerShell模块没有正确安装,或者C#项目缺少必要的引用。请检查你的开发环境和项目配置是否正确设置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值