如何使用LINQ链接数据库?举个例子(C#.NET)谢谢

如何使用LINQ链接数据库?举个例子(C#.NET)谢谢,第1张

1、在App_Code下面添加新项“LINQ to SQL 类”

2、打开该类,设置Name属性(例如MyLinqDB)和Connection属性

3、服务器资源管理器内添加数据库连接

4、将数据库内需要 *** 作的表拖入LINQ to SQL 类的设计视图,并保存

5、新建数据绑定控件例如ListView1

6、后台Page_Load编写代码:

var DB = new MyLinqDB()

var query = from t in DB.T_Users select new {t.ID, t.UserName, t.Password}

ListView1.DataSource = query.Where(t =>t.ID >0).Skip(3 * 20).Take(20)//每页20,第四页

ListView1.DataBind()

之前在远标做过用JAVA连接数据库主要有两种方式,一是用JDBC-ODBC桥来连接,二是用相关厂商提供的相应驱动程序来连接,首先谈谈第一种连接。

JDBC-ODBC桥接器是用JdbcOdbc.Class和一个用于访问ODBC驱动程序的本地库实现的。对于WINDOWS平台,该本地库是一个动态连接库DLL(JDBCODBC.DLL)。

由于JDBC在设计上与ODBC很接近。在内部,这个驱动程序把JDBC的方法映射到ODBC调用上,这样,JDBC就可以和任何可用的ODBC驱动程序进行交互了。这种桥接器的优点是,它使JDBC目前有能力访问几乎所有的数据库。通行方式如图所示:

应用程序---JDBC API---JDBC-ODBC---ODBC API---ODBC层---数据源

具体 *** 作方法为:

首先打开控制面板的管理工具,打开数据源(ODBC),在用户DSN里面添加数据源(即你要连接的数据库的名字),在这里假定连接SQL SERVER 2000的GoodsSupply数据库。名称填写你要连接的数据库的名称(GoodsSupply),然后逐步设置,如果选用了使用SQL-SERVER密码认证的话,就要输入相应的用户名及密码连接到数据库。一路下一步设置完成。

在JAVA里面编写程序进行测试,在这里我的程序是让用户输入任意的表名与与列名,把该列的所有数据输出。源代码如下:

import java.io.BufferedReader

import java.io.InputStreamReader

import java.sql.*

public class ODBCBridge {

public static void main(String[] args) {

String url="jdbc:odbc:GoodsSupply"

Statement sm=null

String command=null

ResultSet rs=null

String tableName=null

String cName=null

String result=null

BufferedReader input=new BufferedReader(new InputStreamReader(System.in))

try {

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")//加载驱动

}catch(ClassNotFoundException e){

System.out.println("Can not load Jdbc-Odbc Bridge Driver")

System.err.print("ClassNotFoundException:")

System.err.println(e.getMessage())

}

Connection con=DriverManager.getConnection(url,"USER","PASSWORD")//使用SQL-SERVER2000认证

DatabaseMetaData dmd=con.getMetaData()//DMD为连接的相应情况

System.out.println("连接的数据库:"+dmd.getURL())

System.out.println("驱动程序:"+dmd.getDriverName())

sm=con.createStatement()

System.out.println("输入表名")

tableName=input.readLine()

while(true) {

System.out.println("输入列名(为空时程序结束):")

cName=input.readLine()

if(cName.equalsIgnoreCase(""))

break

command="select "+cName+" from "+tableName

rs=sm.executeQuery(command)//执行查询

if(!rs.next())

System.out.println("表名或列名输入有误")

else {

System.out.println("查询结果为:")

do

{

result=rs.getString(cName)

//数据库语言设置为中文,不用转换编码

//result=new String(result.getBytes("ISO-8859-1"),"GB2312")

System.out.println(result)

}while(rs.next())

}

}

}catch(SQLException ex) {

System.out.println("SQLException:")

while(ex!=null) {

System.out.println("Message:"+ex.getMessage())

ex=ex.getNextException()

}

}catch(Exception e) {

System.out.println("IOException")

}

}

}

使用join来连接

Enumerable.Join<TOuter, TInner, TKey, TResult>方法 (IEnumerable<TOuter>, IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>)

发送反馈

基于匹配键对两个序列元素进行关联。

使用默认的相等比较器对键进行比较。

命名空间: System.Linq

程序集: System.Core(在 System.Core.dll 中)

语法

--------------------------------------------------------------------------------

VBC#C++F#JScript以带有颜色区分的格式查看复制到剪贴板打印<ExtensionAttribute>_

Public Shared Function Join(Of TOuter, TInner, TKey, TResult) ( _

outer As IEnumerable(Of TOuter), _

inner As IEnumerable(Of TInner), _

outerKeySelector As Func(Of TOuter, TKey), _

innerKeySelector As Func(Of TInner, TKey), _

resultSelector As Func(Of TOuter, TInner, TResult) _

) As IEnumerable(Of TResult)

<ExtensionAttribute>_

Public Shared Function Join(Of TOuter, TInner, TKey, TResult) ( _

outer As IEnumerable(Of TOuter), _

inner As IEnumerable(Of TInner), _

outerKeySelector As Func(Of TOuter, TKey), _

innerKeySelector As Func(Of TInner, TKey), _

resultSelector As Func(Of TOuter, TInner, TResult) _

) As IEnumerable(Of TResult)

public static IEnumerable<TResult>Join<TOuter, TInner, TKey, TResult>(

this IEnumerable<TOuter>outer,

IEnumerable<TInner>inner,

Func<TOuter, TKey>outerKeySelector,

Func<TInner, TKey>innerKeySelector,

Func<TOuter, TInner, TResult>resultSelector

)

public static IEnumerable<TResult>Join<TOuter, TInner, TKey, TResult>(

this IEnumerable<TOuter>outer,

IEnumerable<TInner>inner,

Func<TOuter, TKey>outerKeySelector,

Func<TInner, TKey>innerKeySelector,

Func<TOuter, TInner, TResult>resultSelector

)

[ExtensionAttribute]

public:

generic<typename TOuter, typename TInner, typename TKey, typename TResult>

static IEnumerable<TResult>^ Join(

IEnumerable<TOuter>^ outer,

IEnumerable<TInner>^ inner,

Func<TOuter, TKey>^ outerKeySelector,

Func<TInner, TKey>^ innerKeySelector,

Func<TOuter, TInner, TResult>^ resultSelector

)

[ExtensionAttribute]

public:

generic<typename TOuter, typename TInner, typename TKey, typename TResult>

static IEnumerable<TResult>^ Join(

IEnumerable<TOuter>^ outer,

IEnumerable<TInner>^ inner,

Func<TOuter, TKey>^ outerKeySelector,

Func<TInner, TKey>^ innerKeySelector,

Func<TOuter, TInner, TResult>^ resultSelector

)

static member Join :

outer:IEnumerable<'TOuter>*

inner:IEnumerable<'TInner>*

outerKeySelector:Func<'TOuter, 'TKey>*

innerKeySelector:Func<'TInner, 'TKey>*

resultSelector:Func<'TOuter, 'TInner, 'TResult>->IEnumerable<'TResult>

static member Join :

outer:IEnumerable<'TOuter>*

inner:IEnumerable<'TInner>*

outerKeySelector:Func<'TOuter, 'TKey>*

innerKeySelector:Func<'TInner, 'TKey>*

resultSelector:Func<'TOuter, 'TInner, 'TResult>->IEnumerable<'TResult>

类型参数

TOuter

第一个序列中的元素的类型。

TInner

第二个序列中的元素的类型。

TKey

键选择器函数返回的键的类型。

TResult

结果元素的类型。

参数

outer

类型: System.Collections.Generic.IEnumerable <TOuter>

要联接的第一个序列。

inner

类型: System.Collections.Generic.IEnumerable <TInner>

要与第一个序列联接的序列。

outerKeySelector

类型: System.Func <TOuter, TKey>

用于从第一个序列的每个元素提取联接键的函数。

innerKeySelector

类型: System.Func <TInner, TKey>

用于从第二个序列的每个元素提取联接键的函数。

resultSelector

类型: System.Func <TOuter, TInner, TResult>

用于从两个匹配元素创建结果元素的函数。

返回值

类型: System.Collections.Generic.IEnumerable <TResult>

一个具有 TResult 类型元素的 IEnumerable<T>,这些元素是通过对两个序列执行内部联接得来的。

使用说明

在 Visual Basic 和 C# 中,可以在 IEnumerable<TOuter>类型的任何对象上将此方法作为实例方法来调用。当使用实例方法语法调用此方法时,请省略第一个参数。有关更多信息,请参见 扩展方法 (Visual Basic)或 扩展方法(C# 编程指南)。

异常

--------------------------------------------------------------------------------

异常 条件

ArgumentNullException

outer 或 inner 或 outerKeySelector 或 innerKeySelector 或 resultSelector 为 null。

备注

--------------------------------------------------------------------------------

此方法通过使用延迟执行实现。

即时返回值为一个对象,该对象存储执行 *** 作所需的所有信息。

只有通过直接调用对象的 GetEnumerator 方法或使用 Visual C# 中的 foreach(或 Visual Basic 中的 For Each)来枚举该对象时,才执行此方法表示的查询。

使用默认的相等比较器 Default 对键进行哈希处理和比较。

联接是指基于某个公共键使两个信息源的元素相关联的 *** 作。

Join 在一个方法调用中产生两个信息源和使它们相匹配的密钥。

这与 SelectMany 的用法不同,后者需要一个以上的方法调用才可以执行相同的 *** 作。

Join 保留 outer 中的元素的顺序,并且对于这些元素中的每一个元素,保留 inner 中的匹配元素的顺序。

在查询表达式语法中, join (Visual C#) 或 Join (Visual Basic) 子句转换为 Join 的一个调用。

在关系数据库术语中, Join 方法实现内部同等联接。'

“内部”表示结果中仅包含在另一序列中具有匹配项的元素。

“同等联接”是在其中比较键是否相等的联接。

左外部联接 *** 作没有专用的标准查询运算符,但可以使用 GroupJoin 方法执行此 *** 作。

请参见 联接运算。

示例

--------------------------------------------------------------------------------

下面的代码示例演示如何使用 Join<TOuter, TInner, TKey, TResult>(IEnumerable<TOuter>, IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>) 基于公共键对两个序列执行内部联接。

VBC#C++F#JScript以带有颜色区分的格式查看复制到剪贴板打印Structure Person

Public Name As String

End Structure

Structure Pet

Public Name As String

Public Owner As Person

End Structure

Sub JoinEx1()

Dim magnus As New Person With {.Name = "Hedlund, Magnus"}

Dim terry As New Person With {.Name = "Adams, Terry"}

Dim charlotte As New Person With {.Name = "Weiss, Charlotte"}

Dim barley As New Pet With {.Name = "Barley", .Owner = terry}

Dim boots As New Pet With {.Name = "Boots", .Owner = terry}

Dim whiskers As New Pet With {.Name = "Whiskers", .Owner = charlotte}

Dim daisy As New Pet With {.Name = "Daisy", .Owner = magnus}

Dim people As New List(Of Person)(New Person() {magnus, terry, charlotte})

Dim pets As New List(Of Pet)(New Pet() {barley, boots, whiskers, daisy})

' Create a list of Person-Pet pairs, where each element is an

' anonymous type that contains a Pet's name and the name of the

' Person that owns the Pet.

Dim query = _

people.Join(pets, _

Function(person) person, _

Function(pet) pet.Owner, _

Function(person, pet) _

New With {.OwnerName = person.Name, .Pet = pet.Name})

Dim output As New System.Text.StringBuilder

For Each obj In query

output.AppendLine(obj.OwnerName &" - " &obj.Pet)

Next

' Display the output.

MsgBox(output.ToString)

End Sub

' This code produces the following output:

'

' Hedlund, Magnus - Daisy

' Adams, Terry - Barley

' Adams, Terry - Boots

' Weiss, Charlotte - Whiskers

Structure Person

Public Name As String

End Structure

Structure Pet

Public Name As String

Public Owner As Person

End Structure

Sub JoinEx1()

Dim magnus As New Person With {.Name = "Hedlund, Magnus"}

Dim terry As New Person With {.Name = "Adams, Terry"}

Dim charlotte As New Person With {.Name = "Weiss, Charlotte"}

Dim barley As New Pet With {.Name = "Barley", .Owner = terry}

Dim boots As New Pet With {.Name = "Boots", .Owner = terry}

Dim whiskers As New Pet With {.Name = "Whiskers", .Owner = charlotte}

Dim daisy As New Pet With {.Name = "Daisy", .Owner = magnus}

Dim people As New List(Of Person)(New Person() {magnus, terry, charlotte})

Dim pets As New List(Of Pet)(New Pet() {barley, boots, whiskers, daisy})

' Create a list of Person-Pet pairs, where each element is an

' anonymous type that contains a Pet's name and the name of the

' Person that owns the Pet.

Dim query = _

people.Join(pets, _

Function(person) person, _

Function(pet) pet.Owner, _

Function(person, pet) _

New With {.OwnerName = person.Name, .Pet = pet.Name})

Dim output As New System.Text.StringBuilder

For Each obj In query

output.AppendLine(obj.OwnerName &" - " &obj.Pet)

Next

' Display the output.

MsgBox(output.ToString)

End Sub

' This code produces the following output:

'

' Hedlund, Magnus - Daisy

' Adams, Terry - Barley

' Adams, Terry - Boots

' Weiss, Charlotte - Whiskers


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/6790218.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-03-28
下一篇2023-03-28

发表评论

登录后才能评论

评论列表(0条)

    保存