【问题标题】:cfquery type interface for C# asp.netC# asp.net 的 cfquery 类型接口
【发布时间】:2010-02-14 04:17:38
【问题描述】:

我真的很喜欢coldfusion 中的cfquery,我想知道是否有人尝试为asp.net/C# 编写类似的东西。或者,有谁知道他们是如何在 cf 中使用 Java 做到这一点的?我想要一个与我的数据库类似的接口

QueryResult myObject = ObjectDatabase.Query("SELECT XXXXX","DataSource");
this.Var = myObject.VariableOne;

类似上面的东西,我可以查询数据库,它几乎可以即时创建我的变量。

【问题讨论】:

    标签: c# asp.net coldfusion


    【解决方案1】:

    如今,在 dot net 世界中,我们通常使用 ORM 或类似工具(NHibernate、Linq-to-Sql、SubSonic、Entity Framework 等)。

    但是,如果您真的想这样做,以下内容应该可以帮助您入门。您至少需要引用适当的程序集并添加 using 语句

    using System.Data;
    using System.Data.SqlClient;
    

    然后您可以使用此代码开始(未经测试和未编译,但足够接近)

    // connection string will be like "Server=(local);DataBase=Northwind;Integrated Security=SSPI"
    
    // instantiate and open connection
    using( var conn =  new SqlConnection(connectionString) )
    {
        conn.Open();
    
        var cmd = new SqlCommand("select * from Customers where city = @City", conn);
    
        // define parameters used in command object
        cmd.Parameters.Add( new SqlParameter{ ParameterName = "@City", Value = inputCity });
    
        using( var reader = command.ExecuteReader() )
        {
            // write each record
            while(reader.Read())
            {
                Console.WriteLine("{0}, {1}", reader["CompanyName"], reader["ContactName"]);
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      听起来你想要LINQ to SQL

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-22
        • 1970-01-01
        相关资源
        最近更新 更多