⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datacontext.aspx.cs

📁 数据库连接查询
💻 CS
字号:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections.Generic;


//[Table(Name = "NewCustomers")]
//public class Customer
//{
//    [Column(IsPrimaryKey = true)]
//    public string CustomerID { get; set; }

//    [Column(Name = "ContactName")]
//    public string Name { get; set; }

//    [Column]
//    public string City { get; set; }

//    internal object Skip(int p)
//    {
//        throw new NotImplementedException();
//    }
//}

//[Table(Name = "test")]
//public class test
//{
//    [Column(IsPrimaryKey = true, IsDbGenerated = true)]
//    public int ID { get; set; }

//    [Column(DbType="varchar(20)")]
//    public string Name { get; set; }
//}

//public partial class testContext : DataContext
//{
//    public Table<test> test;
//    public testContext(string connection) : base(connection) { }
//}

//public partial class NorthwindDataContext : DataContext
//{
//    public Table<Customer> Customers;
//    public NorthwindDataContext(IDbConnection connection) : base(connection) { }
//    public NorthwindDataContext(string connection) : base(connection) { }
//}


public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

        //IDbConnection conn = new SqlConnection("server=srv-devdbhost;database=Northwind;uid=sa;pwd=Abcd1234");
        //NorthwindDataContext ctx = new NorthwindDataContext(conn);
        //StreamWriter sw = new StreamWriter(Server.MapPath("log.txt"), true); // Append
        //ctx.Log = sw;
//var conn = new SqlConnection("server=srv-devdbhost;database=Northwind;uid=sa;pwd=Abcd1234");
//var ctx = new DataContext(conn);
//var cmd = new SqlCommand("select * from customers where CustomerID like 'A%'", conn);
//conn.Open(); 
//var reader = cmd.ExecuteReader();        
//GridView1.DataSource = ctx.Translate<Customer>(reader);
//GridView1.DataBind();
//conn.Close();
//GridView1.DataSource = from c in ctx.Customers where c.CustomerID.StartsWith("A") select new { 顾客ID = c.CustomerID, 顾客名 = c.Name, 城市 = c.City };

        //var select = from c in ctx.Customers where c.CustomerID.StartsWith("A") select new { 顾客ID = c.CustomerID, 顾客名 = c.Name, 城市 = c.City };
        //DbCommand cmd = ctx.GetCommand(select);
        //Response.Write(cmd.CommandText + "<br/>");
        //foreach (DbParameter parm in cmd.Parameters)
        //    Response.Write(string.Format("参数名:{0},参数值:{1}<br/>", parm.ParameterName, parm.Value));
        //Customer customer = ctx.Customers.First();
        //customer.Name = "zhuye";
        //IList<object> queryText = ctx.GetChangeSet().ModifiedEntities;
        //Response.Write(((Customer)queryText[0]).Name);

//NorthwindDataContext ctx = new NorthwindDataContext("server=srv-devdbhost;database=Northwind;uid=sa;pwd=Abcd1234");
//string newcity = "Shanghai";
//ctx.ExecuteCommand("update Customers set City={0} where CustomerID like 'A%'", newcity);
//IEnumerable<Customer> customers = ctx.ExecuteQuery<Customer>("select * from Customers where CustomerID like 'A%'");
//GridView1.DataSource = customers;
//GridView1.DataBind();
//        testContext ctx = new testContext("server=srv-devdbhost;database=testdb;uid=sa;pwd=Abcd1234");
//ctx.CreateDatabase();

//sw.Close();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -