技术开发 频道

浅析ASP.NET中C++和J#的混合应用

  【IT168 技术文档】不想搞得太复杂,我仅仅演示实现 DataReader和DataSet,起个抛砖引玉的作用,也顾不得代码结构是否优美、性能是否良好,能够达到本文的目的即可。

  为了方便大家亲自动手实践,数据库使用的是ACCESS,文章末尾有下载链接。实际项目中要使用SqlServer和ORACLE的话,把几个OleDb函数换个名字即可,怎么换就不用我多说了。

  相同的功能,我用三种语言来实现,首先,给出.NET的“标准语言”——C#版本作为基本参照。

  以下是C#版本:

  main_cs.dll文件源码:

using System;
using System.Data;
using System.Data.OleDb;
using System.Text;
    
public class main_cs:System.Web.UI.Page
    {
        OleDbDataReader dr;
        OleDbCommand cmd;
        DataSet ds;
        OleDbDataAdapter adp;
        OleDbConnection conn;
        StringBuilder connStr;
        
public void Page_Load()
        {
            connStr
= new StringBuilder("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=");
            connStr.Append(Server.MapPath(
"image.mdb"));
            conn
= new OleDbConnection(connStr.ToString());
            cmd
= new OleDbCommand("SELECT title FROM image_data",conn);
            conn.Open();
            dr
= cmd.ExecuteReader();
            
while(dr.Read())
            {
                Response.Write(dr[
"title"]);
            }
            dr.Close();
            conn.Close();
            ds
= new DataSet();
            adp
= new OleDbDataAdapter("SELECT title FROM image_data",conn);
            adp.Fill(ds);
            Response.Write(ds.Tables[
0].Rows[0]["title"]);
        }
    }

 

0
相关文章